Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\DependencyInjection\Loader;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\DependencyInjection\DefinitionDecorator;
|
Chris@0
|
15 use Symfony\Component\DependencyInjection\Alias;
|
Chris@0
|
16 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
17 use Symfony\Component\DependencyInjection\Definition;
|
Chris@0
|
18 use Symfony\Component\DependencyInjection\Reference;
|
Chris@0
|
19 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
Chris@0
|
20 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
|
Chris@0
|
21 use Symfony\Component\Config\Resource\FileResource;
|
Chris@0
|
22 use Symfony\Component\Yaml\Exception\ParseException;
|
Chris@0
|
23 use Symfony\Component\Yaml\Parser as YamlParser;
|
Chris@0
|
24 use Symfony\Component\Yaml\Yaml;
|
Chris@0
|
25 use Symfony\Component\ExpressionLanguage\Expression;
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * YamlFileLoader loads YAML files service definitions.
|
Chris@0
|
29 *
|
Chris@0
|
30 * The YAML format does not support anonymous services (cf. the XML loader).
|
Chris@0
|
31 *
|
Chris@0
|
32 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
33 */
|
Chris@0
|
34 class YamlFileLoader extends FileLoader
|
Chris@0
|
35 {
|
Chris@0
|
36 private static $keywords = array(
|
Chris@0
|
37 'alias' => 'alias',
|
Chris@0
|
38 'parent' => 'parent',
|
Chris@0
|
39 'class' => 'class',
|
Chris@0
|
40 'shared' => 'shared',
|
Chris@0
|
41 'synthetic' => 'synthetic',
|
Chris@0
|
42 'lazy' => 'lazy',
|
Chris@0
|
43 'public' => 'public',
|
Chris@0
|
44 'abstract' => 'abstract',
|
Chris@0
|
45 'deprecated' => 'deprecated',
|
Chris@0
|
46 'factory' => 'factory',
|
Chris@0
|
47 'file' => 'file',
|
Chris@0
|
48 'arguments' => 'arguments',
|
Chris@0
|
49 'properties' => 'properties',
|
Chris@0
|
50 'configurator' => 'configurator',
|
Chris@0
|
51 'calls' => 'calls',
|
Chris@0
|
52 'tags' => 'tags',
|
Chris@0
|
53 'decorates' => 'decorates',
|
Chris@0
|
54 'decoration_inner_name' => 'decoration_inner_name',
|
Chris@0
|
55 'decoration_priority' => 'decoration_priority',
|
Chris@0
|
56 'autowire' => 'autowire',
|
Chris@0
|
57 'autowiring_types' => 'autowiring_types',
|
Chris@0
|
58 );
|
Chris@0
|
59
|
Chris@0
|
60 private $yamlParser;
|
Chris@0
|
61
|
Chris@0
|
62 /**
|
Chris@0
|
63 * {@inheritdoc}
|
Chris@0
|
64 */
|
Chris@0
|
65 public function load($resource, $type = null)
|
Chris@0
|
66 {
|
Chris@0
|
67 $path = $this->locator->locate($resource);
|
Chris@0
|
68
|
Chris@0
|
69 $content = $this->loadFile($path);
|
Chris@0
|
70
|
Chris@0
|
71 $this->container->addResource(new FileResource($path));
|
Chris@0
|
72
|
Chris@0
|
73 // empty file
|
Chris@0
|
74 if (null === $content) {
|
Chris@0
|
75 return;
|
Chris@0
|
76 }
|
Chris@0
|
77
|
Chris@0
|
78 // imports
|
Chris@0
|
79 $this->parseImports($content, $path);
|
Chris@0
|
80
|
Chris@0
|
81 // parameters
|
Chris@0
|
82 if (isset($content['parameters'])) {
|
Chris@0
|
83 if (!is_array($content['parameters'])) {
|
Chris@0
|
84 throw new InvalidArgumentException(sprintf('The "parameters" key should contain an array in %s. Check your YAML syntax.', $resource));
|
Chris@0
|
85 }
|
Chris@0
|
86
|
Chris@0
|
87 foreach ($content['parameters'] as $key => $value) {
|
Chris@0
|
88 $this->container->setParameter($key, $this->resolveServices($value));
|
Chris@0
|
89 }
|
Chris@0
|
90 }
|
Chris@0
|
91
|
Chris@0
|
92 // extensions
|
Chris@0
|
93 $this->loadFromExtensions($content);
|
Chris@0
|
94
|
Chris@0
|
95 // services
|
Chris@0
|
96 $this->parseDefinitions($content, $resource);
|
Chris@0
|
97 }
|
Chris@0
|
98
|
Chris@0
|
99 /**
|
Chris@0
|
100 * {@inheritdoc}
|
Chris@0
|
101 */
|
Chris@0
|
102 public function supports($resource, $type = null)
|
Chris@0
|
103 {
|
Chris@0
|
104 return is_string($resource) && in_array(pathinfo($resource, PATHINFO_EXTENSION), array('yml', 'yaml'), true);
|
Chris@0
|
105 }
|
Chris@0
|
106
|
Chris@0
|
107 /**
|
Chris@0
|
108 * Parses all imports.
|
Chris@0
|
109 *
|
Chris@0
|
110 * @param array $content
|
Chris@0
|
111 * @param string $file
|
Chris@0
|
112 */
|
Chris@0
|
113 private function parseImports(array $content, $file)
|
Chris@0
|
114 {
|
Chris@0
|
115 if (!isset($content['imports'])) {
|
Chris@0
|
116 return;
|
Chris@0
|
117 }
|
Chris@0
|
118
|
Chris@0
|
119 if (!is_array($content['imports'])) {
|
Chris@0
|
120 throw new InvalidArgumentException(sprintf('The "imports" key should contain an array in %s. Check your YAML syntax.', $file));
|
Chris@0
|
121 }
|
Chris@0
|
122
|
Chris@0
|
123 $defaultDirectory = dirname($file);
|
Chris@0
|
124 foreach ($content['imports'] as $import) {
|
Chris@0
|
125 if (!is_array($import)) {
|
Chris@0
|
126 throw new InvalidArgumentException(sprintf('The values in the "imports" key should be arrays in %s. Check your YAML syntax.', $file));
|
Chris@0
|
127 }
|
Chris@0
|
128
|
Chris@0
|
129 $this->setCurrentDir($defaultDirectory);
|
Chris@0
|
130 $this->import($import['resource'], null, isset($import['ignore_errors']) ? (bool) $import['ignore_errors'] : false, $file);
|
Chris@0
|
131 }
|
Chris@0
|
132 }
|
Chris@0
|
133
|
Chris@0
|
134 /**
|
Chris@0
|
135 * Parses definitions.
|
Chris@0
|
136 *
|
Chris@0
|
137 * @param array $content
|
Chris@0
|
138 * @param string $file
|
Chris@0
|
139 */
|
Chris@0
|
140 private function parseDefinitions(array $content, $file)
|
Chris@0
|
141 {
|
Chris@0
|
142 if (!isset($content['services'])) {
|
Chris@0
|
143 return;
|
Chris@0
|
144 }
|
Chris@0
|
145
|
Chris@0
|
146 if (!is_array($content['services'])) {
|
Chris@0
|
147 throw new InvalidArgumentException(sprintf('The "services" key should contain an array in %s. Check your YAML syntax.', $file));
|
Chris@0
|
148 }
|
Chris@0
|
149
|
Chris@0
|
150 foreach ($content['services'] as $id => $service) {
|
Chris@0
|
151 $this->parseDefinition($id, $service, $file);
|
Chris@0
|
152 }
|
Chris@0
|
153 }
|
Chris@0
|
154
|
Chris@0
|
155 /**
|
Chris@0
|
156 * Parses a definition.
|
Chris@0
|
157 *
|
Chris@0
|
158 * @param string $id
|
Chris@0
|
159 * @param array|string $service
|
Chris@0
|
160 * @param string $file
|
Chris@0
|
161 *
|
Chris@0
|
162 * @throws InvalidArgumentException When tags are invalid
|
Chris@0
|
163 */
|
Chris@0
|
164 private function parseDefinition($id, $service, $file)
|
Chris@0
|
165 {
|
Chris@0
|
166 if (is_string($service) && 0 === strpos($service, '@')) {
|
Chris@0
|
167 $this->container->setAlias($id, substr($service, 1));
|
Chris@0
|
168
|
Chris@0
|
169 return;
|
Chris@0
|
170 }
|
Chris@0
|
171
|
Chris@0
|
172 if (!is_array($service)) {
|
Chris@0
|
173 throw new InvalidArgumentException(sprintf('A service definition must be an array or a string starting with "@" but %s found for service "%s" in %s. Check your YAML syntax.', gettype($service), $id, $file));
|
Chris@0
|
174 }
|
Chris@0
|
175
|
Chris@0
|
176 static::checkDefinition($id, $service, $file);
|
Chris@0
|
177
|
Chris@0
|
178 if (isset($service['alias'])) {
|
Chris@0
|
179 $public = !array_key_exists('public', $service) || (bool) $service['public'];
|
Chris@0
|
180 $this->container->setAlias($id, new Alias($service['alias'], $public));
|
Chris@0
|
181
|
Chris@0
|
182 foreach ($service as $key => $value) {
|
Chris@0
|
183 if (!in_array($key, array('alias', 'public'))) {
|
Chris@0
|
184 @trigger_error(sprintf('The configuration key "%s" is unsupported for the service "%s" which is defined as an alias in "%s". Allowed configuration keys for service aliases are "alias" and "public". The YamlFileLoader will raise an exception in Symfony 4.0, instead of silently ignoring unsupported attributes.', $key, $id, $file), E_USER_DEPRECATED);
|
Chris@0
|
185 }
|
Chris@0
|
186 }
|
Chris@0
|
187
|
Chris@0
|
188 return;
|
Chris@0
|
189 }
|
Chris@0
|
190
|
Chris@0
|
191 if (isset($service['parent'])) {
|
Chris@0
|
192 $definition = new DefinitionDecorator($service['parent']);
|
Chris@0
|
193 } else {
|
Chris@0
|
194 $definition = new Definition();
|
Chris@0
|
195 }
|
Chris@0
|
196
|
Chris@0
|
197 if (isset($service['class'])) {
|
Chris@0
|
198 $definition->setClass($service['class']);
|
Chris@0
|
199 }
|
Chris@0
|
200
|
Chris@0
|
201 if (isset($service['shared'])) {
|
Chris@0
|
202 $definition->setShared($service['shared']);
|
Chris@0
|
203 }
|
Chris@0
|
204
|
Chris@0
|
205 if (isset($service['synthetic'])) {
|
Chris@0
|
206 $definition->setSynthetic($service['synthetic']);
|
Chris@0
|
207 }
|
Chris@0
|
208
|
Chris@0
|
209 if (isset($service['lazy'])) {
|
Chris@0
|
210 $definition->setLazy($service['lazy']);
|
Chris@0
|
211 }
|
Chris@0
|
212
|
Chris@0
|
213 if (isset($service['public'])) {
|
Chris@0
|
214 $definition->setPublic($service['public']);
|
Chris@0
|
215 }
|
Chris@0
|
216
|
Chris@0
|
217 if (isset($service['abstract'])) {
|
Chris@0
|
218 $definition->setAbstract($service['abstract']);
|
Chris@0
|
219 }
|
Chris@0
|
220
|
Chris@0
|
221 if (array_key_exists('deprecated', $service)) {
|
Chris@0
|
222 $definition->setDeprecated(true, $service['deprecated']);
|
Chris@0
|
223 }
|
Chris@0
|
224
|
Chris@0
|
225 if (isset($service['factory'])) {
|
Chris@0
|
226 $definition->setFactory($this->parseCallable($service['factory'], 'factory', $id, $file));
|
Chris@0
|
227 }
|
Chris@0
|
228
|
Chris@0
|
229 if (isset($service['file'])) {
|
Chris@0
|
230 $definition->setFile($service['file']);
|
Chris@0
|
231 }
|
Chris@0
|
232
|
Chris@0
|
233 if (isset($service['arguments'])) {
|
Chris@0
|
234 $definition->setArguments($this->resolveServices($service['arguments']));
|
Chris@0
|
235 }
|
Chris@0
|
236
|
Chris@0
|
237 if (isset($service['properties'])) {
|
Chris@0
|
238 $definition->setProperties($this->resolveServices($service['properties']));
|
Chris@0
|
239 }
|
Chris@0
|
240
|
Chris@0
|
241 if (isset($service['configurator'])) {
|
Chris@0
|
242 $definition->setConfigurator($this->parseCallable($service['configurator'], 'configurator', $id, $file));
|
Chris@0
|
243 }
|
Chris@0
|
244
|
Chris@0
|
245 if (isset($service['calls'])) {
|
Chris@0
|
246 if (!is_array($service['calls'])) {
|
Chris@0
|
247 throw new InvalidArgumentException(sprintf('Parameter "calls" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
|
Chris@0
|
248 }
|
Chris@0
|
249
|
Chris@0
|
250 foreach ($service['calls'] as $call) {
|
Chris@0
|
251 if (isset($call['method'])) {
|
Chris@0
|
252 $method = $call['method'];
|
Chris@0
|
253 $args = isset($call['arguments']) ? $this->resolveServices($call['arguments']) : array();
|
Chris@0
|
254 } else {
|
Chris@0
|
255 $method = $call[0];
|
Chris@0
|
256 $args = isset($call[1]) ? $this->resolveServices($call[1]) : array();
|
Chris@0
|
257 }
|
Chris@0
|
258
|
Chris@0
|
259 $definition->addMethodCall($method, $args);
|
Chris@0
|
260 }
|
Chris@0
|
261 }
|
Chris@0
|
262
|
Chris@0
|
263 if (isset($service['tags'])) {
|
Chris@0
|
264 if (!is_array($service['tags'])) {
|
Chris@0
|
265 throw new InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
|
Chris@0
|
266 }
|
Chris@0
|
267
|
Chris@0
|
268 foreach ($service['tags'] as $tag) {
|
Chris@0
|
269 if (!is_array($tag)) {
|
Chris@0
|
270 throw new InvalidArgumentException(sprintf('A "tags" entry must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
|
Chris@0
|
271 }
|
Chris@0
|
272
|
Chris@0
|
273 if (!isset($tag['name'])) {
|
Chris@0
|
274 throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file));
|
Chris@0
|
275 }
|
Chris@0
|
276
|
Chris@0
|
277 if (!is_string($tag['name']) || '' === $tag['name']) {
|
Chris@0
|
278 throw new InvalidArgumentException(sprintf('The tag name for service "%s" in %s must be a non-empty string.', $id, $file));
|
Chris@0
|
279 }
|
Chris@0
|
280
|
Chris@0
|
281 $name = $tag['name'];
|
Chris@0
|
282 unset($tag['name']);
|
Chris@0
|
283
|
Chris@0
|
284 foreach ($tag as $attribute => $value) {
|
Chris@0
|
285 if (!is_scalar($value) && null !== $value) {
|
Chris@0
|
286 throw new InvalidArgumentException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s" in %s. Check your YAML syntax.', $id, $name, $attribute, $file));
|
Chris@0
|
287 }
|
Chris@0
|
288 }
|
Chris@0
|
289
|
Chris@0
|
290 $definition->addTag($name, $tag);
|
Chris@0
|
291 }
|
Chris@0
|
292 }
|
Chris@0
|
293
|
Chris@0
|
294 if (isset($service['decorates'])) {
|
Chris@0
|
295 if ('' !== $service['decorates'] && '@' === $service['decorates'][0]) {
|
Chris@0
|
296 throw new InvalidArgumentException(sprintf('The value of the "decorates" option for the "%s" service must be the id of the service without the "@" prefix (replace "%s" with "%s").', $id, $service['decorates'], substr($service['decorates'], 1)));
|
Chris@0
|
297 }
|
Chris@0
|
298
|
Chris@0
|
299 $renameId = isset($service['decoration_inner_name']) ? $service['decoration_inner_name'] : null;
|
Chris@0
|
300 $priority = isset($service['decoration_priority']) ? $service['decoration_priority'] : 0;
|
Chris@0
|
301 $definition->setDecoratedService($service['decorates'], $renameId, $priority);
|
Chris@0
|
302 }
|
Chris@0
|
303
|
Chris@0
|
304 if (isset($service['autowire'])) {
|
Chris@0
|
305 $definition->setAutowired($service['autowire']);
|
Chris@0
|
306 }
|
Chris@0
|
307
|
Chris@0
|
308 if (isset($service['autowiring_types'])) {
|
Chris@0
|
309 if (is_string($service['autowiring_types'])) {
|
Chris@0
|
310 $definition->addAutowiringType($service['autowiring_types']);
|
Chris@0
|
311 } else {
|
Chris@0
|
312 if (!is_array($service['autowiring_types'])) {
|
Chris@0
|
313 throw new InvalidArgumentException(sprintf('Parameter "autowiring_types" must be a string or an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
|
Chris@0
|
314 }
|
Chris@0
|
315
|
Chris@0
|
316 foreach ($service['autowiring_types'] as $autowiringType) {
|
Chris@0
|
317 if (!is_string($autowiringType)) {
|
Chris@0
|
318 throw new InvalidArgumentException(sprintf('A "autowiring_types" attribute must be of type string for service "%s" in %s. Check your YAML syntax.', $id, $file));
|
Chris@0
|
319 }
|
Chris@0
|
320
|
Chris@0
|
321 $definition->addAutowiringType($autowiringType);
|
Chris@0
|
322 }
|
Chris@0
|
323 }
|
Chris@0
|
324 }
|
Chris@0
|
325
|
Chris@0
|
326 $this->container->setDefinition($id, $definition);
|
Chris@0
|
327 }
|
Chris@0
|
328
|
Chris@0
|
329 /**
|
Chris@0
|
330 * Parses a callable.
|
Chris@0
|
331 *
|
Chris@0
|
332 * @param string|array $callable A callable
|
Chris@0
|
333 * @param string $parameter A parameter (e.g. 'factory' or 'configurator')
|
Chris@0
|
334 * @param string $id A service identifier
|
Chris@0
|
335 * @param string $file A parsed file
|
Chris@0
|
336 *
|
Chris@0
|
337 * @throws InvalidArgumentException When errors are occuried
|
Chris@0
|
338 *
|
Chris@0
|
339 * @return string|array A parsed callable
|
Chris@0
|
340 */
|
Chris@0
|
341 private function parseCallable($callable, $parameter, $id, $file)
|
Chris@0
|
342 {
|
Chris@0
|
343 if (is_string($callable)) {
|
Chris@0
|
344 if ('' !== $callable && '@' === $callable[0]) {
|
Chris@0
|
345 throw new InvalidArgumentException(sprintf('The value of the "%s" option for the "%s" service must be the id of the service without the "@" prefix (replace "%s" with "%s").', $parameter, $id, $callable, substr($callable, 1)));
|
Chris@0
|
346 }
|
Chris@0
|
347
|
Chris@0
|
348 if (false !== strpos($callable, ':') && false === strpos($callable, '::')) {
|
Chris@0
|
349 $parts = explode(':', $callable);
|
Chris@0
|
350
|
Chris@0
|
351 return array($this->resolveServices('@'.$parts[0]), $parts[1]);
|
Chris@0
|
352 }
|
Chris@0
|
353
|
Chris@0
|
354 return $callable;
|
Chris@0
|
355 }
|
Chris@0
|
356
|
Chris@0
|
357 if (is_array($callable)) {
|
Chris@0
|
358 if (isset($callable[0]) && isset($callable[1])) {
|
Chris@0
|
359 return array($this->resolveServices($callable[0]), $callable[1]);
|
Chris@0
|
360 }
|
Chris@0
|
361
|
Chris@0
|
362 throw new InvalidArgumentException(sprintf('Parameter "%s" must contain an array with two elements for service "%s" in %s. Check your YAML syntax.', $parameter, $id, $file));
|
Chris@0
|
363 }
|
Chris@0
|
364
|
Chris@0
|
365 throw new InvalidArgumentException(sprintf('Parameter "%s" must be a string or an array for service "%s" in %s. Check your YAML syntax.', $parameter, $id, $file));
|
Chris@0
|
366 }
|
Chris@0
|
367
|
Chris@0
|
368 /**
|
Chris@0
|
369 * Loads a YAML file.
|
Chris@0
|
370 *
|
Chris@0
|
371 * @param string $file
|
Chris@0
|
372 *
|
Chris@0
|
373 * @return array The file content
|
Chris@0
|
374 *
|
Chris@0
|
375 * @throws InvalidArgumentException when the given file is not a local file or when it does not exist
|
Chris@0
|
376 */
|
Chris@0
|
377 protected function loadFile($file)
|
Chris@0
|
378 {
|
Chris@0
|
379 if (!class_exists('Symfony\Component\Yaml\Parser')) {
|
Chris@0
|
380 throw new RuntimeException('Unable to load YAML config files as the Symfony Yaml Component is not installed.');
|
Chris@0
|
381 }
|
Chris@0
|
382
|
Chris@0
|
383 if (!stream_is_local($file)) {
|
Chris@0
|
384 throw new InvalidArgumentException(sprintf('This is not a local file "%s".', $file));
|
Chris@0
|
385 }
|
Chris@0
|
386
|
Chris@0
|
387 if (!file_exists($file)) {
|
Chris@0
|
388 throw new InvalidArgumentException(sprintf('The file "%s" does not exist.', $file));
|
Chris@0
|
389 }
|
Chris@0
|
390
|
Chris@0
|
391 if (null === $this->yamlParser) {
|
Chris@0
|
392 $this->yamlParser = new YamlParser();
|
Chris@0
|
393 }
|
Chris@0
|
394
|
Chris@0
|
395 try {
|
Chris@0
|
396 $configuration = $this->yamlParser->parse(file_get_contents($file), Yaml::PARSE_CONSTANT);
|
Chris@0
|
397 } catch (ParseException $e) {
|
Chris@0
|
398 throw new InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $file), 0, $e);
|
Chris@0
|
399 }
|
Chris@0
|
400
|
Chris@0
|
401 return $this->validate($configuration, $file);
|
Chris@0
|
402 }
|
Chris@0
|
403
|
Chris@0
|
404 /**
|
Chris@0
|
405 * Validates a YAML file.
|
Chris@0
|
406 *
|
Chris@0
|
407 * @param mixed $content
|
Chris@0
|
408 * @param string $file
|
Chris@0
|
409 *
|
Chris@0
|
410 * @return array
|
Chris@0
|
411 *
|
Chris@0
|
412 * @throws InvalidArgumentException When service file is not valid
|
Chris@0
|
413 */
|
Chris@0
|
414 private function validate($content, $file)
|
Chris@0
|
415 {
|
Chris@0
|
416 if (null === $content) {
|
Chris@0
|
417 return $content;
|
Chris@0
|
418 }
|
Chris@0
|
419
|
Chris@0
|
420 if (!is_array($content)) {
|
Chris@0
|
421 throw new InvalidArgumentException(sprintf('The service file "%s" is not valid. It should contain an array. Check your YAML syntax.', $file));
|
Chris@0
|
422 }
|
Chris@0
|
423
|
Chris@0
|
424 foreach ($content as $namespace => $data) {
|
Chris@0
|
425 if (in_array($namespace, array('imports', 'parameters', 'services'))) {
|
Chris@0
|
426 continue;
|
Chris@0
|
427 }
|
Chris@0
|
428
|
Chris@0
|
429 if (!$this->container->hasExtension($namespace)) {
|
Chris@0
|
430 $extensionNamespaces = array_filter(array_map(function ($ext) { return $ext->getAlias(); }, $this->container->getExtensions()));
|
Chris@0
|
431 throw new InvalidArgumentException(sprintf(
|
Chris@0
|
432 'There is no extension able to load the configuration for "%s" (in %s). Looked for namespace "%s", found %s',
|
Chris@0
|
433 $namespace,
|
Chris@0
|
434 $file,
|
Chris@0
|
435 $namespace,
|
Chris@0
|
436 $extensionNamespaces ? sprintf('"%s"', implode('", "', $extensionNamespaces)) : 'none'
|
Chris@0
|
437 ));
|
Chris@0
|
438 }
|
Chris@0
|
439 }
|
Chris@0
|
440
|
Chris@0
|
441 return $content;
|
Chris@0
|
442 }
|
Chris@0
|
443
|
Chris@0
|
444 /**
|
Chris@0
|
445 * Resolves services.
|
Chris@0
|
446 *
|
Chris@0
|
447 * @param string|array $value
|
Chris@0
|
448 *
|
Chris@0
|
449 * @return array|string|Reference
|
Chris@0
|
450 */
|
Chris@0
|
451 private function resolveServices($value)
|
Chris@0
|
452 {
|
Chris@0
|
453 if (is_array($value)) {
|
Chris@0
|
454 $value = array_map(array($this, 'resolveServices'), $value);
|
Chris@0
|
455 } elseif (is_string($value) && 0 === strpos($value, '@=')) {
|
Chris@0
|
456 return new Expression(substr($value, 2));
|
Chris@0
|
457 } elseif (is_string($value) && 0 === strpos($value, '@')) {
|
Chris@0
|
458 if (0 === strpos($value, '@@')) {
|
Chris@0
|
459 $value = substr($value, 1);
|
Chris@0
|
460 $invalidBehavior = null;
|
Chris@0
|
461 } elseif (0 === strpos($value, '@?')) {
|
Chris@0
|
462 $value = substr($value, 2);
|
Chris@0
|
463 $invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
|
Chris@0
|
464 } else {
|
Chris@0
|
465 $value = substr($value, 1);
|
Chris@0
|
466 $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
|
Chris@0
|
467 }
|
Chris@0
|
468
|
Chris@0
|
469 if ('=' === substr($value, -1)) {
|
Chris@0
|
470 $value = substr($value, 0, -1);
|
Chris@0
|
471 }
|
Chris@0
|
472
|
Chris@0
|
473 if (null !== $invalidBehavior) {
|
Chris@0
|
474 $value = new Reference($value, $invalidBehavior);
|
Chris@0
|
475 }
|
Chris@0
|
476 }
|
Chris@0
|
477
|
Chris@0
|
478 return $value;
|
Chris@0
|
479 }
|
Chris@0
|
480
|
Chris@0
|
481 /**
|
Chris@0
|
482 * Loads from Extensions.
|
Chris@0
|
483 *
|
Chris@0
|
484 * @param array $content
|
Chris@0
|
485 */
|
Chris@0
|
486 private function loadFromExtensions(array $content)
|
Chris@0
|
487 {
|
Chris@0
|
488 foreach ($content as $namespace => $values) {
|
Chris@0
|
489 if (in_array($namespace, array('imports', 'parameters', 'services'))) {
|
Chris@0
|
490 continue;
|
Chris@0
|
491 }
|
Chris@0
|
492
|
Chris@0
|
493 if (!is_array($values)) {
|
Chris@0
|
494 $values = array();
|
Chris@0
|
495 }
|
Chris@0
|
496
|
Chris@0
|
497 $this->container->loadFromExtension($namespace, $values);
|
Chris@0
|
498 }
|
Chris@0
|
499 }
|
Chris@0
|
500
|
Chris@0
|
501 /**
|
Chris@0
|
502 * Checks the keywords used to define a service.
|
Chris@0
|
503 *
|
Chris@0
|
504 * @param string $id The service name
|
Chris@0
|
505 * @param array $definition The service definition to check
|
Chris@0
|
506 * @param string $file The loaded YAML file
|
Chris@0
|
507 */
|
Chris@0
|
508 private static function checkDefinition($id, array $definition, $file)
|
Chris@0
|
509 {
|
Chris@0
|
510 foreach ($definition as $key => $value) {
|
Chris@0
|
511 if (!isset(static::$keywords[$key])) {
|
Chris@0
|
512 @trigger_error(sprintf('The configuration key "%s" is unsupported for service definition "%s" in "%s". Allowed configuration keys are "%s". The YamlFileLoader object will raise an exception instead in Symfony 4.0 when detecting an unsupported service configuration key.', $key, $id, $file, implode('", "', static::$keywords)), E_USER_DEPRECATED);
|
Chris@0
|
513 // @deprecated Uncomment the following statement in Symfony 4.0
|
Chris@0
|
514 // and also update the corresponding unit test to make it expect
|
Chris@0
|
515 // an InvalidArgumentException exception.
|
Chris@0
|
516 //throw new InvalidArgumentException(sprintf('The configuration key "%s" is unsupported for service definition "%s" in "%s". Allowed configuration keys are "%s".', $key, $id, $file, implode('", "', static::$keywords)));
|
Chris@0
|
517 }
|
Chris@0
|
518 }
|
Chris@0
|
519 }
|
Chris@0
|
520 }
|