ContainerConfigurator.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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\DependencyInjection\Argument\AbstractArgument;
  12. use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
  13. use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
  14. use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
  15. use Symfony\Component\DependencyInjection\ContainerBuilder;
  16. use Symfony\Component\DependencyInjection\Definition;
  17. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  18. use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
  19. use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
  20. use Symfony\Component\ExpressionLanguage\Expression;
  21. /**
  22. * @author Nicolas Grekas <p@tchwork.com>
  23. */
  24. class ContainerConfigurator extends AbstractConfigurator
  25. {
  26. public const FACTORY = 'container';
  27. private $container;
  28. private $loader;
  29. private $instanceof;
  30. private $path;
  31. private $file;
  32. private $anonymousCount = 0;
  33. public function __construct(ContainerBuilder $container, PhpFileLoader $loader, array &$instanceof, string $path, string $file)
  34. {
  35. $this->container = $container;
  36. $this->loader = $loader;
  37. $this->instanceof = &$instanceof;
  38. $this->path = $path;
  39. $this->file = $file;
  40. }
  41. final public function extension(string $namespace, array $config)
  42. {
  43. if (!$this->container->hasExtension($namespace)) {
  44. $extensions = array_filter(array_map(function (ExtensionInterface $ext) { return $ext->getAlias(); }, $this->container->getExtensions()));
  45. 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'));
  46. }
  47. $this->container->loadFromExtension($namespace, static::processValue($config));
  48. }
  49. final public function import(string $resource, string $type = null, $ignoreErrors = false)
  50. {
  51. $this->loader->setCurrentDir(\dirname($this->path));
  52. $this->loader->import($resource, $type, $ignoreErrors, $this->file);
  53. }
  54. final public function parameters(): ParametersConfigurator
  55. {
  56. return new ParametersConfigurator($this->container);
  57. }
  58. final public function services(): ServicesConfigurator
  59. {
  60. return new ServicesConfigurator($this->container, $this->loader, $this->instanceof, $this->path, $this->anonymousCount);
  61. }
  62. /**
  63. * @return static
  64. */
  65. final public function withPath(string $path): self
  66. {
  67. $clone = clone $this;
  68. $clone->path = $clone->file = $path;
  69. return $clone;
  70. }
  71. }
  72. /**
  73. * Creates a parameter.
  74. */
  75. function param(string $name): string
  76. {
  77. return '%'.$name.'%';
  78. }
  79. /**
  80. * Creates a service reference.
  81. *
  82. * @deprecated since Symfony 5.1, use service() instead.
  83. */
  84. function ref(string $id): ReferenceConfigurator
  85. {
  86. trigger_deprecation('symfony/dependency-injection', '5.1', '"%s()" is deprecated, use "service()" instead.', __FUNCTION__);
  87. return new ReferenceConfigurator($id);
  88. }
  89. /**
  90. * Creates a reference to a service.
  91. */
  92. function service(string $serviceId): ReferenceConfigurator
  93. {
  94. return new ReferenceConfigurator($serviceId);
  95. }
  96. /**
  97. * Creates an inline service.
  98. *
  99. * @deprecated since Symfony 5.1, use inline_service() instead.
  100. */
  101. function inline(string $class = null): InlineServiceConfigurator
  102. {
  103. trigger_deprecation('symfony/dependency-injection', '5.1', '"%s()" is deprecated, use "inline_service()" instead.', __FUNCTION__);
  104. return new InlineServiceConfigurator(new Definition($class));
  105. }
  106. /**
  107. * Creates an inline service.
  108. */
  109. function inline_service(string $class = null): InlineServiceConfigurator
  110. {
  111. return new InlineServiceConfigurator(new Definition($class));
  112. }
  113. /**
  114. * Creates a service locator.
  115. *
  116. * @param ReferenceConfigurator[] $values
  117. */
  118. function service_locator(array $values): ServiceLocatorArgument
  119. {
  120. return new ServiceLocatorArgument(AbstractConfigurator::processValue($values, true));
  121. }
  122. /**
  123. * Creates a lazy iterator.
  124. *
  125. * @param ReferenceConfigurator[] $values
  126. */
  127. function iterator(array $values): IteratorArgument
  128. {
  129. return new IteratorArgument(AbstractConfigurator::processValue($values, true));
  130. }
  131. /**
  132. * Creates a lazy iterator by tag name.
  133. */
  134. function tagged_iterator(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null, string $defaultPriorityMethod = null): TaggedIteratorArgument
  135. {
  136. return new TaggedIteratorArgument($tag, $indexAttribute, $defaultIndexMethod, false, $defaultPriorityMethod);
  137. }
  138. /**
  139. * Creates a service locator by tag name.
  140. */
  141. function tagged_locator(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null): ServiceLocatorArgument
  142. {
  143. return new ServiceLocatorArgument(new TaggedIteratorArgument($tag, $indexAttribute, $defaultIndexMethod, true));
  144. }
  145. /**
  146. * Creates an expression.
  147. */
  148. function expr(string $expression): Expression
  149. {
  150. return new Expression($expression);
  151. }
  152. /**
  153. * Creates an abstract argument.
  154. */
  155. function abstract_arg(string $description): AbstractArgument
  156. {
  157. return new AbstractArgument($description);
  158. }