-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Config][DependencyInjection] Add configuration builder for writing P…
…HP config
- Loading branch information
commit 460b46f7302ec7319b8334a43809523363bfef39
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
< 8000 td class="blob-code blob-code-addition js-file-line"> } | ||
|
||
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; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.