8000 [Config][DependencyInjection] Add configuration builder for writing PHP config by Nyholm · Pull Request #40600 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Config][DependencyInjection] Add configuration builder for writing PHP config #40600

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 155 additions & 0 deletions src/Symfony/Component/Config/Builder/ClassBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Config\Builder;

/**
* Build PHP classes to generate config.
*
* @internal
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class ClassBuilder
{
/** @var string */
private $namespace;

/** @var string */
private $name;

/** @var Property[] */
private $properties = [];

/** @var Method[] */
private $methods = [];
private $require = [];
private $implements = [];

public function __construct(string $namespace, string $name)
{
$this->namespace = $namespace;
$this->name = ucfirst($this->camelCase($name)).'Config';
}

public function getDirectory(): string
{
return str_replace('\\', \DIRECTORY_SEPARATOR, $this->namespace);
}

public function getFilename(): string
{
return $this->name.'.php';
}

public function build(): string
{
$rootPath = explode(\DIRECTORY_SEPARATOR, $this->getDirectory());
$require = '';
foreach ($this->require as $class) {
// figure out relative path.
$path = explode(\DIRECTORY_SEPARATOR, $class->getDirectory());
$path[] = $class->getFilename();
foreach ($rootPath as $key => $value) {
if ($path[$key] !== $value) {
break;
}
unset($path[$key]);
}
$require .= sprintf('require_once __DIR__.\'%s\';', \DIRECTORY_SEPARATOR.implode(\DIRECTORY_SEPARATOR, $path))."\n";
}

$implements = [] === $this->implements ? '' : 'implements '.implode(', ', $this->implements);
$body = '';
foreach ($this->properties as $property) {
$body .= ' '.$property->getContent()."\n";
}
foreach ($this->methods as $method) {
$lines = explode("\n", $method->getContent());
foreach ($lines as $i => $line) {
$body .= ' '.$line."\n";
}
}

$content = strtr('<?php

namespace NAMESPACE;

REQUIRE

/**
* This class is automatically generated to help creating config.
*
* @experimental in 5.3
*/
class CLASS IMPLEMENTS
{
BODY
}
', ['NAMESPACE' => $this->namespace, 'REQUIRE' => $require, 'CLASS' => $this->getName(), 'IMPLEMENTS' => $implements, 'BODY' => $body]);

return $content;
}

public function addRequire(self $class)
{
$this->require[] = $class;
}

public function addImplements(string $interface)
{
$this->implements[] = '\\'.ltrim($interface, '\\');
}

public function addMethod(string $name, string $body, array $params = []): void
{
$this->methods[] = new Method(strtr($body, ['NAME' => $this->camelCase($name)] + $params));
}

public function addProperty(string $name, string $classType = null): Property
{
$property = new Property($name, $this->camelCase($name));
if (null !== $classType) {
$property->setType($classType);
}
$this->properties[] = $property;
$property->setContent(sprintf('private $%s;', $property->getName()));

return $property;
}

public function getProperties(): array
{
return $this->properties;
}

private function camelCase(string $input): string
{
$output = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $input))));

return preg_replace('#\W#', '', $output);
}

public function getName(): string
{
return $this->name;
}

public function getNamespace(): string
{
return $this->namespace;
}

public function getFqcn()
{
return '\\'.$this->namespace.'\\'.$this->name;
}
}
Loading
0