Chris@14
|
1 <?php
|
Chris@14
|
2
|
Chris@14
|
3 /*
|
Chris@14
|
4 * This file is part of the Symfony package.
|
Chris@14
|
5 *
|
Chris@14
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@14
|
7 *
|
Chris@14
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@14
|
9 * file that was distributed with this source code.
|
Chris@14
|
10 */
|
Chris@14
|
11
|
Chris@14
|
12 namespace Symfony\Component\DependencyInjection\Loader\Configurator;
|
Chris@14
|
13
|
Chris@14
|
14 use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
|
Chris@14
|
15 use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
|
Chris@14
|
16 use Symfony\Component\DependencyInjection\ContainerBuilder;
|
Chris@14
|
17 use Symfony\Component\DependencyInjection\Definition;
|
Chris@14
|
18 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
Chris@14
|
19 use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
|
Chris@14
|
20 use Symfony\Component\ExpressionLanguage\Expression;
|
Chris@14
|
21
|
Chris@14
|
22 /**
|
Chris@14
|
23 * @author Nicolas Grekas <p@tchwork.com>
|
Chris@14
|
24 */
|
Chris@14
|
25 class ContainerConfigurator extends AbstractConfigurator
|
Chris@14
|
26 {
|
Chris@14
|
27 const FACTORY = 'container';
|
Chris@14
|
28
|
Chris@14
|
29 private $container;
|
Chris@14
|
30 private $loader;
|
Chris@14
|
31 private $instanceof;
|
Chris@14
|
32 private $path;
|
Chris@14
|
33 private $file;
|
Chris@14
|
34
|
Chris@14
|
35 public function __construct(ContainerBuilder $container, PhpFileLoader $loader, array &$instanceof, $path, $file)
|
Chris@14
|
36 {
|
Chris@14
|
37 $this->container = $container;
|
Chris@14
|
38 $this->loader = $loader;
|
Chris@14
|
39 $this->instanceof = &$instanceof;
|
Chris@14
|
40 $this->path = $path;
|
Chris@14
|
41 $this->file = $file;
|
Chris@14
|
42 }
|
Chris@14
|
43
|
Chris@14
|
44 final public function extension($namespace, array $config)
|
Chris@14
|
45 {
|
Chris@14
|
46 if (!$this->container->hasExtension($namespace)) {
|
Chris@14
|
47 $extensions = array_filter(array_map(function ($ext) { return $ext->getAlias(); }, $this->container->getExtensions()));
|
Chris@14
|
48 throw new InvalidArgumentException(sprintf(
|
Chris@14
|
49 'There is no extension able to load the configuration for "%s" (in %s). Looked for namespace "%s", found %s',
|
Chris@14
|
50 $namespace,
|
Chris@14
|
51 $this->file,
|
Chris@14
|
52 $namespace,
|
Chris@14
|
53 $extensions ? sprintf('"%s"', implode('", "', $extensions)) : 'none'
|
Chris@14
|
54 ));
|
Chris@14
|
55 }
|
Chris@14
|
56
|
Chris@14
|
57 $this->container->loadFromExtension($namespace, static::processValue($config));
|
Chris@14
|
58 }
|
Chris@14
|
59
|
Chris@14
|
60 final public function import($resource, $type = null, $ignoreErrors = false)
|
Chris@14
|
61 {
|
Chris@17
|
62 $this->loader->setCurrentDir(\dirname($this->path));
|
Chris@14
|
63 $this->loader->import($resource, $type, $ignoreErrors, $this->file);
|
Chris@14
|
64 }
|
Chris@14
|
65
|
Chris@14
|
66 /**
|
Chris@14
|
67 * @return ParametersConfigurator
|
Chris@14
|
68 */
|
Chris@14
|
69 final public function parameters()
|
Chris@14
|
70 {
|
Chris@14
|
71 return new ParametersConfigurator($this->container);
|
Chris@14
|
72 }
|
Chris@14
|
73
|
Chris@14
|
74 /**
|
Chris@14
|
75 * @return ServicesConfigurator
|
Chris@14
|
76 */
|
Chris@14
|
77 final public function services()
|
Chris@14
|
78 {
|
Chris@14
|
79 return new ServicesConfigurator($this->container, $this->loader, $this->instanceof);
|
Chris@14
|
80 }
|
Chris@14
|
81 }
|
Chris@14
|
82
|
Chris@14
|
83 /**
|
Chris@14
|
84 * Creates a service reference.
|
Chris@14
|
85 *
|
Chris@14
|
86 * @param string $id
|
Chris@14
|
87 *
|
Chris@14
|
88 * @return ReferenceConfigurator
|
Chris@14
|
89 */
|
Chris@14
|
90 function ref($id)
|
Chris@14
|
91 {
|
Chris@14
|
92 return new ReferenceConfigurator($id);
|
Chris@14
|
93 }
|
Chris@14
|
94
|
Chris@14
|
95 /**
|
Chris@14
|
96 * Creates an inline service.
|
Chris@14
|
97 *
|
Chris@14
|
98 * @param string|null $class
|
Chris@14
|
99 *
|
Chris@14
|
100 * @return InlineServiceConfigurator
|
Chris@14
|
101 */
|
Chris@14
|
102 function inline($class = null)
|
Chris@14
|
103 {
|
Chris@14
|
104 return new InlineServiceConfigurator(new Definition($class));
|
Chris@14
|
105 }
|
Chris@14
|
106
|
Chris@14
|
107 /**
|
Chris@14
|
108 * Creates a lazy iterator.
|
Chris@14
|
109 *
|
Chris@14
|
110 * @param ReferenceConfigurator[] $values
|
Chris@14
|
111 *
|
Chris@14
|
112 * @return IteratorArgument
|
Chris@14
|
113 */
|
Chris@14
|
114 function iterator(array $values)
|
Chris@14
|
115 {
|
Chris@14
|
116 return new IteratorArgument(AbstractConfigurator::processValue($values, true));
|
Chris@14
|
117 }
|
Chris@14
|
118
|
Chris@14
|
119 /**
|
Chris@14
|
120 * Creates a lazy iterator by tag name.
|
Chris@14
|
121 *
|
Chris@14
|
122 * @param string $tag
|
Chris@14
|
123 *
|
Chris@14
|
124 * @return TaggedIteratorArgument
|
Chris@14
|
125 */
|
Chris@14
|
126 function tagged($tag)
|
Chris@14
|
127 {
|
Chris@14
|
128 return new TaggedIteratorArgument($tag);
|
Chris@14
|
129 }
|
Chris@14
|
130
|
Chris@14
|
131 /**
|
Chris@14
|
132 * Creates an expression.
|
Chris@14
|
133 *
|
Chris@14
|
134 * @param string $expression an expression
|
Chris@14
|
135 *
|
Chris@14
|
136 * @return Expression
|
Chris@14
|
137 */
|
Chris@14
|
138 function expr($expression)
|
Chris@14
|
139 {
|
Chris@14
|
140 return new Expression($expression);
|
Chris@14
|
141 }
|