ContainerConfigurator.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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\IteratorArgument;
  12. use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
  13. use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  15. use Symfony\Component\DependencyInjection\Definition;
  16. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  17. use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
  18. use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
  19. use Symfony\Component\ExpressionLanguage\Expression;
  20. /**
  21. * @author Nicolas Grekas <p@tchwork.com>
  22. */
  23. class ContainerConfigurator extends AbstractConfigurator
  24. {
  25. const FACTORY = 'container';
  26. private $container;
  27. private $loader;
  28. private $instanceof;
  29. private $path;
  30. private $file;
  31. private $anonymousCount = 0;
  32. public function __construct(ContainerBuilder $container, PhpFileLoader $loader, array &$instanceof, string $path, string $file)
  33. {
  34. $this->container = $container;
  35. $this->loader = $loader;
  36. $this->instanceof = &$instanceof;
  37. $this->path = $path;
  38. $this->file = $file;
  39. }
  40. final public function extension(string $namespace, array $config)
  41. {
  42. if (!$this->container->hasExtension($namespace)) {
  43. $extensions = array_filter(array_map(function (ExtensionInterface $ext) { return $ext->getAlias(); }, $this->container->getExtensions()));
  44. 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'));
  45. }
  46. $this->container->loadFromExtension($namespace, static::processValue($config));
  47. }
  48. final public function import(string $resource, string $type = null, $ignoreErrors = false)
  49. {
  50. $this->loader->setCurrentDir(\dirname($this->path));
  51. $this->loader->import($resource, $type, $ignoreErrors, $this->file);
  52. }
  53. final public function parameters(): ParametersConfigurator
  54. {
  55. return new ParametersConfigurator($this->container);
  56. }
  57. final public function services(): ServicesConfigurator
  58. {
  59. return new ServicesConfigurator($this->container, $this->loader, $this->instanceof, $this->path, $this->anonymousCount);
  60. }
  61. }
  62. /**
  63. * Creates a service reference.
  64. */
  65. function ref(string $id): ReferenceConfigurator
  66. {
  67. return new ReferenceConfigurator($id);
  68. }
  69. /**
  70. * Creates an inline service.
  71. */
  72. function inline(string $class = null): InlineServiceConfigurator
  73. {
  74. return new InlineServiceConfigurator(new Definition($class));
  75. }
  76. /**
  77. * Creates a service locator.
  78. *
  79. * @param ReferenceConfigurator[] $values
  80. */
  81. function service_locator(array $values): ServiceLocatorArgument
  82. {
  83. return new ServiceLocatorArgument(AbstractConfigurator::processValue($values, true));
  84. }
  85. /**
  86. * Creates a lazy iterator.
  87. *
  88. * @param ReferenceConfigurator[] $values
  89. */
  90. function iterator(array $values): IteratorArgument
  91. {
  92. return new IteratorArgument(AbstractConfigurator::processValue($values, true));
  93. }
  94. /**
  95. * Creates a lazy iterator by tag name.
  96. *
  97. * @deprecated since Symfony 4.4, to be removed in 5.0, use "tagged_iterator" instead.
  98. */
  99. function tagged(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null): TaggedIteratorArgument
  100. {
  101. @trigger_error(__NAMESPACE__.'\tagged() is deprecated since Symfony 4.4 and will be removed in 5.0, use '.__NAMESPACE__.'\tagged_iterator() instead.', E_USER_DEPRECATED);
  102. return new TaggedIteratorArgument($tag, $indexAttribute, $defaultIndexMethod);
  103. }
  104. /**
  105. * Creates a lazy iterator by tag name.
  106. */
  107. function tagged_iterator(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null, string $defaultPriorityMethod = null): TaggedIteratorArgument
  108. {
  109. return new TaggedIteratorArgument($tag, $indexAttribute, $defaultIndexMethod, false, $defaultPriorityMethod);
  110. }
  111. /**
  112. * Creates a service locator by tag name.
  113. */
  114. function tagged_locator(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null): ServiceLocatorArgument
  115. {
  116. return new ServiceLocatorArgument(new TaggedIteratorArgument($tag, $indexAttribute, $defaultIndexMethod, true));
  117. }
  118. /**
  119. * Creates an expression.
  120. */
  121. function expr(string $expression): Expression
  122. {
  123. return new Expression($expression);
  124. }