4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Loader\Configurator;
14 use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
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\Loader\PhpFileLoader;
20 use Symfony\Component\ExpressionLanguage\Expression;
23 * @author Nicolas Grekas <p@tchwork.com>
25 class ContainerConfigurator extends AbstractConfigurator
27 const FACTORY = 'container';
35 public function __construct(ContainerBuilder $container, PhpFileLoader $loader, array &$instanceof, $path, $file)
37 $this->container = $container;
38 $this->loader = $loader;
39 $this->instanceof = &$instanceof;
44 final public function extension($namespace, array $config)
46 if (!$this->container->hasExtension($namespace)) {
47 $extensions = array_filter(array_map(function ($ext) { return $ext->getAlias(); }, $this->container->getExtensions()));
48 throw new InvalidArgumentException(sprintf(
49 'There is no extension able to load the configuration for "%s" (in %s). Looked for namespace "%s", found %s',
53 $extensions ? sprintf('"%s"', implode('", "', $extensions)) : 'none'
57 $this->container->loadFromExtension($namespace, static::processValue($config));
60 final public function import($resource, $type = null, $ignoreErrors = false)
62 $this->loader->setCurrentDir(\dirname($this->path));
63 $this->loader->import($resource, $type, $ignoreErrors, $this->file);
67 * @return ParametersConfigurator
69 final public function parameters()
71 return new ParametersConfigurator($this->container);
75 * @return ServicesConfigurator
77 final public function services()
79 return new ServicesConfigurator($this->container, $this->loader, $this->instanceof);
84 * Creates a service reference.
88 * @return ReferenceConfigurator
92 return new ReferenceConfigurator($id);
96 * Creates an inline service.
98 * @param string|null $class
100 * @return InlineServiceConfigurator
102 function inline($class = null)
104 return new InlineServiceConfigurator(new Definition($class));
108 * Creates a lazy iterator.
110 * @param ReferenceConfigurator[] $values
112 * @return IteratorArgument
114 function iterator(array $values)
116 return new IteratorArgument(AbstractConfigurator::processValue($values, true));
120 * Creates a lazy iterator by tag name.
124 * @return TaggedIteratorArgument
126 function tagged($tag)
128 return new TaggedIteratorArgument($tag);
132 * Creates an expression.
134 * @param string $expression an expression
138 function expr($expression)
140 return new Expression($expression);