vendor/symfony/dependency-injection/Loader/Configurator/ContainerConfigurator.php line 150

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\DependencyInjection\Loader\Configurator;
  11. use Symfony\Component\Config\Loader\ParamConfigurator;
  12. use Symfony\Component\DependencyInjection\Argument\AbstractArgument;
  13. use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
  14. use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
  15. use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
  16. use Symfony\Component\DependencyInjection\ContainerBuilder;
  17. use Symfony\Component\DependencyInjection\Definition;
  18. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  19. use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
  20. use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
  21. use Symfony\Component\ExpressionLanguage\Expression;
  22. /**
  23.  * @author Nicolas Grekas <p@tchwork.com>
  24.  */
  25. class ContainerConfigurator extends AbstractConfigurator
  26. {
  27.     public const FACTORY 'container';
  28.     private ContainerBuilder $container;
  29.     private PhpFileLoader $loader;
  30.     private array $instanceof;
  31.     private string $path;
  32.     private string $file;
  33.     private int $anonymousCount 0;
  34.     private ?string $env;
  35.     public function __construct(ContainerBuilder $containerPhpFileLoader $loader, array &$instanceofstring $pathstring $filestring $env null)
  36.     {
  37.         $this->container $container;
  38.         $this->loader $loader;
  39.         $this->instanceof = &$instanceof;
  40.         $this->path $path;
  41.         $this->file $file;
  42.         $this->env $env;
  43.     }
  44.     final public function extension(string $namespace, array $config)
  45.     {
  46.         if (!$this->container->hasExtension($namespace)) {
  47.             $extensions array_filter(array_map(function (ExtensionInterface $ext) { return $ext->getAlias(); }, $this->container->getExtensions()));
  48.             throw new InvalidArgumentException(sprintf('There is no extension able to load the configuration for "%s" (in "%s"). Looked for namespace "%s", found "%s".'$namespace$this->file$namespace$extensions implode('", "'$extensions) : 'none'));
  49.         }
  50.         $this->container->loadFromExtension($namespace, static::processValue($config));
  51.     }
  52.     final public function import(string $resourcestring $type nullbool|string $ignoreErrors false)
  53.     {
  54.         $this->loader->setCurrentDir(\dirname($this->path));
  55.         $this->loader->import($resource$type$ignoreErrors$this->file);
  56.     }
  57.     final public function parameters(): ParametersConfigurator
  58.     {
  59.         return new ParametersConfigurator($this->container);
  60.     }
  61.     final public function services(): ServicesConfigurator
  62.     {
  63.         return new ServicesConfigurator($this->container$this->loader$this->instanceof$this->path$this->anonymousCount);
  64.     }
  65.     /**
  66.      * Get the current environment to be able to write conditional configuration.
  67.      */
  68.     final public function env(): ?string
  69.     {
  70.         return $this->env;
  71.     }
  72.     final public function withPath(string $path): static
  73.     {
  74.         $clone = clone $this;
  75.         $clone->path $clone->file $path;
  76.         $clone->loader->setCurrentDir(\dirname($path));
  77.         return $clone;
  78.     }
  79. }
  80. /**
  81.  * Creates a parameter.
  82.  */
  83. function param(string $name): ParamConfigurator
  84. {
  85.     return new ParamConfigurator($name);
  86. }
  87. /**
  88.  * Creates a reference to a service.
  89.  */
  90. function service(string $serviceId): ReferenceConfigurator
  91. {
  92.     return new ReferenceConfigurator($serviceId);
  93. }
  94. /**
  95.  * Creates an inline service.
  96.  */
  97. function inline_service(string $class null): InlineServiceConfigurator
  98. {
  99.     return new InlineServiceConfigurator(new Definition($class));
  100. }
  101. /**
  102.  * Creates a service locator.
  103.  *
  104.  * @param ReferenceConfigurator[] $values
  105.  */
  106. function service_locator(array $values): ServiceLocatorArgument
  107. {
  108.     return new ServiceLocatorArgument(AbstractConfigurator::processValue($valuestrue));
  109. }
  110. /**
  111.  * Creates a lazy iterator.
  112.  *
  113.  * @param ReferenceConfigurator[] $values
  114.  */
  115. function iterator(array $values): IteratorArgument
  116. {
  117.     return new IteratorArgument(AbstractConfigurator::processValue($valuestrue));
  118. }
  119. /**
  120.  * Creates a lazy iterator by tag name.
  121.  */
  122. function tagged_iterator(string $tagstring $indexAttribute nullstring $defaultIndexMethod nullstring $defaultPriorityMethod nullstring|array $exclude = []): TaggedIteratorArgument
  123. {
  124.     return new TaggedIteratorArgument($tag$indexAttribute$defaultIndexMethodfalse$defaultPriorityMethod, (array) $exclude);
  125. }
  126. /**
  127.  * Creates a service locator by tag name.
  128.  */
  129. function tagged_locator(string $tagstring $indexAttribute nullstring $defaultIndexMethod nullstring $defaultPriorityMethod nullstring|array $exclude = []): ServiceLocatorArgument
  130. {
  131.     return new ServiceLocatorArgument(new TaggedIteratorArgument($tag$indexAttribute$defaultIndexMethodtrue$defaultPriorityMethod, (array) $exclude));
  132. }
  133. /**
  134.  * Creates an expression.
  135.  */
  136. function expr(string $expression): Expression
  137. {
  138.     return new Expression($expression);
  139. }
  140. /**
  141.  * Creates an abstract argument.
  142.  */
  143. function abstract_arg(string $description): AbstractArgument
  144. {
  145.     return new AbstractArgument($description);
  146. }
  147. /**
  148.  * Creates an environment variable reference.
  149.  */
  150. function env(string $name): EnvConfigurator
  151. {
  152.     return new EnvConfigurator($name);
  153. }
  154. /**
  155.  * Creates a closure service reference.
  156.  */
  157. function service_closure(string $serviceId): ClosureReferenceConfigurator
  158. {
  159.     return new ClosureReferenceConfigurator($serviceId);
  160. }
  161. /**
  162.  * Creates a closure.
  163.  */
  164. function closure(string|array|ReferenceConfigurator|Expression $callable): InlineServiceConfigurator
  165. {
  166.     return (new InlineServiceConfigurator(new Definition('Closure')))
  167.         ->factory(['Closure''fromCallable'])
  168.         ->args([$callable]);
  169. }