ContainerConfigurator.php 3.5 KB

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