vendor/symfony/config/Builder/ClassBuilder.php line 122

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Config\Builder;
  11. /**
  12.  * Build PHP classes to generate config.
  13.  *
  14.  * @internal
  15.  *
  16.  * @author Tobias Nyholm <tobias.nyholm@gmail.com>
  17.  */
  18. class ClassBuilder
  19. {
  20.     private string $namespace;
  21.     private string $name;
  22.     /** @var Property[] */
  23.     private array $properties = [];
  24.     /** @var Method[] */
  25.     private array $methods = [];
  26.     private array $require = [];
  27.     private array $use = [];
  28.     private array $implements = [];
  29.     private bool $allowExtraKeys false;
  30.     public function __construct(string $namespacestring $name)
  31.     {
  32.         $this->namespace $namespace;
  33.         $this->name ucfirst($this->camelCase($name)).'Config';
  34.     }
  35.     public function getDirectory(): string
  36.     {
  37.         return str_replace('\\'\DIRECTORY_SEPARATOR$this->namespace);
  38.     }
  39.     public function getFilename(): string
  40.     {
  41.         return $this->name.'.php';
  42.     }
  43.     public function build(): string
  44.     {
  45.         $rootPath explode(\DIRECTORY_SEPARATOR$this->getDirectory());
  46.         $require '';
  47.         foreach ($this->require as $class) {
  48.             // figure out relative path.
  49.             $path explode(\DIRECTORY_SEPARATOR$class->getDirectory());
  50.             $path[] = $class->getFilename();
  51.             foreach ($rootPath as $key => $value) {
  52.                 if ($path[$key] !== $value) {
  53.                     break;
  54.                 }
  55.                 unset($path[$key]);
  56.             }
  57.             $require .= sprintf('require_once __DIR__.\DIRECTORY_SEPARATOR.\'%s\';'implode('\'.\DIRECTORY_SEPARATOR.\''$path))."\n";
  58.         }
  59.         $use $require "\n" '';
  60.         foreach (array_keys($this->use) as $statement) {
  61.             $use .= sprintf('use %s;'$statement)."\n";
  62.         }
  63.         $implements = [] === $this->implements '' 'implements '.implode(', '$this->implements);
  64.         $body '';
  65.         foreach ($this->properties as $property) {
  66.             $body .= '    '.$property->getContent()."\n";
  67.         }
  68.         foreach ($this->methods as $method) {
  69.             $lines explode("\n"$method->getContent());
  70.             foreach ($lines as $line) {
  71.                 $body .= ($line '    '.$line '')."\n";
  72.             }
  73.         }
  74.         $content strtr('<?php
  75. namespace NAMESPACE;
  76. REQUIREUSE
  77. /**
  78.  * This class is automatically generated to help in creating a config.
  79.  */
  80. class CLASS IMPLEMENTS
  81. {
  82. BODY
  83. }
  84. ', ['NAMESPACE' => $this->namespace'REQUIRE' => $require'USE' => $use'CLASS' => $this->getName(), 'IMPLEMENTS' => $implements'BODY' => $body]);
  85.         return $content;
  86.     }
  87.     public function addRequire(self $class): void
  88.     {
  89.         $this->require[] = $class;
  90.     }
  91.     public function addUse(string $class): void
  92.     {
  93.         $this->use[$class] = true;
  94.     }
  95.     public function addImplements(string $interface): void
  96.     {
  97.         $this->implements[] = '\\'.ltrim($interface'\\');
  98.     }
  99.     public function addMethod(string $namestring $body, array $params = []): void
  100.     {
  101.         $this->methods[] = new Method(strtr($body, ['NAME' => $this->camelCase($name)] + $params));
  102.     }
  103.     public function addProperty(string $namestring $classType nullstring $defaultValue null): Property
  104.     {
  105.         $property = new Property($name'_' !== $name[0] ? $this->camelCase($name) : $name);
  106.         if (null !== $classType) {
  107.             $property->setType($classType);
  108.         }
  109.         $this->properties[] = $property;
  110.         $defaultValue null !== $defaultValue sprintf(' = %s'$defaultValue) : '';
  111.         $property->setContent(sprintf('private $%s%s;'$property->getName(), $defaultValue));
  112.         return $property;
  113.     }
  114.     public function getProperties(): array
  115.     {
  116.         return $this->properties;
  117.     }
  118.     private function camelCase(string $input): string
  119.     {
  120.         $output lcfirst(str_replace(' '''ucwords(str_replace('_'' '$input))));
  121.         return preg_replace('#\W#'''$output);
  122.     }
  123.     public function getName(): string
  124.     {
  125.         return $this->name;
  126.     }
  127.     public function getNamespace(): string
  128.     {
  129.         return $this->namespace;
  130.     }
  131.     public function getFqcn(): string
  132.     {
  133.         return '\\'.$this->namespace.'\\'.$this->name;
  134.     }
  135.     public function setAllowExtraKeys(bool $allowExtraKeys): void
  136.     {
  137.         $this->allowExtraKeys $allowExtraKeys;
  138.     }
  139.     public function shouldAllowExtraKeys(): bool
  140.     {
  141.         return $this->allowExtraKeys;
  142.     }
  143. }