ContainerConfigurator.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 $container;
  29. private $loader;
  30. private $instanceof;
  31. private $path;
  32. private $file;
  33. private $anonymousCount = 0;
  34. private $env;
  35. public function __construct(ContainerBuilder $container, PhpFileLoader $loader, array &$instanceof, string $path, string $file, ?string $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 $resource, ?string $type = null, $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. /**
  73. * @return static
  74. */
  75. final public function withPath(string $path): self
  76. {
  77. $clone = clone $this;
  78. $clone->path = $clone->file = $path;
  79. $clone->loader->setCurrentDir(\dirname($path));
  80. return $clone;
  81. }
  82. }
  83. /**
  84. * Creates a parameter.
  85. */
  86. function param(string $name): ParamConfigurator
  87. {
  88. return new ParamConfigurator($name);
  89. }
  90. /**
  91. * Creates a service reference.
  92. *
  93. * @deprecated since Symfony 5.1, use service() instead.
  94. */
  95. function ref(string $id): ReferenceConfigurator
  96. {
  97. trigger_deprecation('symfony/dependency-injection', '5.1', '"%s()" is deprecated, use "service()" instead.', __FUNCTION__);
  98. return new ReferenceConfigurator($id);
  99. }
  100. /**
  101. * Creates a reference to a service.
  102. */
  103. function service(string $serviceId): ReferenceConfigurator
  104. {
  105. return new ReferenceConfigurator($serviceId);
  106. }
  107. /**
  108. * Creates an inline service.
  109. *
  110. * @deprecated since Symfony 5.1, use inline_service() instead.
  111. */
  112. function inline(?string $class = null): InlineServiceConfigurator
  113. {
  114. trigger_deprecation('symfony/dependency-injection', '5.1', '"%s()" is deprecated, use "inline_service()" instead.', __FUNCTION__);
  115. return new InlineServiceConfigurator(new Definition($class));
  116. }
  117. /**
  118. * Creates an inline service.
  119. */
  120. function inline_service(?string $class = null): InlineServiceConfigurator
  121. {
  122. return new InlineServiceConfigurator(new Definition($class));
  123. }
  124. /**
  125. * Creates a service locator.
  126. *
  127. * @param ReferenceConfigurator[] $values
  128. */
  129. function service_locator(array $values): ServiceLocatorArgument
  130. {
  131. return new ServiceLocatorArgument(AbstractConfigurator::processValue($values, true));
  132. }
  133. /**
  134. * Creates a lazy iterator.
  135. *
  136. * @param ReferenceConfigurator[] $values
  137. */
  138. function iterator(array $values): IteratorArgument
  139. {
  140. return new IteratorArgument(AbstractConfigurator::processValue($values, true));
  141. }
  142. /**
  143. * Creates a lazy iterator by tag name.
  144. */
  145. function tagged_iterator(string $tag, ?string $indexAttribute = null, ?string $defaultIndexMethod = null, ?string $defaultPriorityMethod = null): TaggedIteratorArgument
  146. {
  147. return new TaggedIteratorArgument($tag, $indexAttribute, $defaultIndexMethod, false, $defaultPriorityMethod);
  148. }
  149. /**
  150. * Creates a service locator by tag name.
  151. */
  152. function tagged_locator(string $tag, ?string $indexAttribute = null, ?string $defaultIndexMethod = null, ?string $defaultPriorityMethod = null): ServiceLocatorArgument
  153. {
  154. return new ServiceLocatorArgument(new TaggedIteratorArgument($tag, $indexAttribute, $defaultIndexMethod, true, $defaultPriorityMethod));
  155. }
  156. /**
  157. * Creates an expression.
  158. */
  159. function expr(string $expression): Expression
  160. {
  161. return new Expression($expression);
  162. }
  163. /**
  164. * Creates an abstract argument.
  165. */
  166. function abstract_arg(string $description): AbstractArgument
  167. {
  168. return new AbstractArgument($description);
  169. }
  170. /**
  171. * Creates an environment variable reference.
  172. */
  173. function env(string $name): EnvConfigurator
  174. {
  175. return new EnvConfigurator($name);
  176. }
  177. /**
  178. * Creates a closure service reference.
  179. */
  180. function service_closure(string $serviceId): ClosureReferenceConfigurator
  181. {
  182. return new ClosureReferenceConfigurator($serviceId);
  183. }