Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / vendor / symfony / dependency-injection / Loader / YamlFileLoader.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\DependencyInjection\Loader;
13
14 use Symfony\Component\DependencyInjection\Alias;
15 use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
16 use Symfony\Component\DependencyInjection\Argument\BoundArgument;
17 use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
18 use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
19 use Symfony\Component\DependencyInjection\ChildDefinition;
20 use Symfony\Component\DependencyInjection\ContainerBuilder;
21 use Symfony\Component\DependencyInjection\ContainerInterface;
22 use Symfony\Component\DependencyInjection\Definition;
23 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
24 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
25 use Symfony\Component\DependencyInjection\Reference;
26 use Symfony\Component\ExpressionLanguage\Expression;
27 use Symfony\Component\Yaml\Exception\ParseException;
28 use Symfony\Component\Yaml\Parser as YamlParser;
29 use Symfony\Component\Yaml\Tag\TaggedValue;
30 use Symfony\Component\Yaml\Yaml;
31
32 /**
33  * YamlFileLoader loads YAML files service definitions.
34  *
35  * @author Fabien Potencier <fabien@symfony.com>
36  */
37 class YamlFileLoader extends FileLoader
38 {
39     private static $serviceKeywords = array(
40         'alias' => 'alias',
41         'parent' => 'parent',
42         'class' => 'class',
43         'shared' => 'shared',
44         'synthetic' => 'synthetic',
45         'lazy' => 'lazy',
46         'public' => 'public',
47         'abstract' => 'abstract',
48         'deprecated' => 'deprecated',
49         'factory' => 'factory',
50         'file' => 'file',
51         'arguments' => 'arguments',
52         'properties' => 'properties',
53         'configurator' => 'configurator',
54         'calls' => 'calls',
55         'tags' => 'tags',
56         'decorates' => 'decorates',
57         'decoration_inner_name' => 'decoration_inner_name',
58         'decoration_priority' => 'decoration_priority',
59         'autowire' => 'autowire',
60         'autowiring_types' => 'autowiring_types',
61         'autoconfigure' => 'autoconfigure',
62         'bind' => 'bind',
63     );
64
65     private static $prototypeKeywords = array(
66         'resource' => 'resource',
67         'namespace' => 'namespace',
68         'exclude' => 'exclude',
69         'parent' => 'parent',
70         'shared' => 'shared',
71         'lazy' => 'lazy',
72         'public' => 'public',
73         'abstract' => 'abstract',
74         'deprecated' => 'deprecated',
75         'factory' => 'factory',
76         'arguments' => 'arguments',
77         'properties' => 'properties',
78         'configurator' => 'configurator',
79         'calls' => 'calls',
80         'tags' => 'tags',
81         'autowire' => 'autowire',
82         'autoconfigure' => 'autoconfigure',
83         'bind' => 'bind',
84     );
85
86     private static $instanceofKeywords = array(
87         'shared' => 'shared',
88         'lazy' => 'lazy',
89         'public' => 'public',
90         'properties' => 'properties',
91         'configurator' => 'configurator',
92         'calls' => 'calls',
93         'tags' => 'tags',
94         'autowire' => 'autowire',
95     );
96
97     private static $defaultsKeywords = array(
98         'public' => 'public',
99         'tags' => 'tags',
100         'autowire' => 'autowire',
101         'autoconfigure' => 'autoconfigure',
102         'bind' => 'bind',
103     );
104
105     private $yamlParser;
106
107     private $anonymousServicesCount;
108     private $anonymousServicesSuffix;
109
110     /**
111      * {@inheritdoc}
112      */
113     public function load($resource, $type = null)
114     {
115         $path = $this->locator->locate($resource);
116
117         $content = $this->loadFile($path);
118
119         $this->container->fileExists($path);
120
121         // empty file
122         if (null === $content) {
123             return;
124         }
125
126         // imports
127         $this->parseImports($content, $path);
128
129         // parameters
130         if (isset($content['parameters'])) {
131             if (!\is_array($content['parameters'])) {
132                 throw new InvalidArgumentException(sprintf('The "parameters" key should contain an array in %s. Check your YAML syntax.', $path));
133             }
134
135             foreach ($content['parameters'] as $key => $value) {
136                 $this->container->setParameter($key, $this->resolveServices($value, $path, true));
137             }
138         }
139
140         // extensions
141         $this->loadFromExtensions($content);
142
143         // services
144         $this->anonymousServicesCount = 0;
145         $this->anonymousServicesSuffix = '~'.ContainerBuilder::hash($path);
146         $this->setCurrentDir(\dirname($path));
147         try {
148             $this->parseDefinitions($content, $path);
149         } finally {
150             $this->instanceof = array();
151         }
152     }
153
154     /**
155      * {@inheritdoc}
156      */
157     public function supports($resource, $type = null)
158     {
159         if (!\is_string($resource)) {
160             return false;
161         }
162
163         if (null === $type && \in_array(pathinfo($resource, PATHINFO_EXTENSION), array('yaml', 'yml'), true)) {
164             return true;
165         }
166
167         return \in_array($type, array('yaml', 'yml'), true);
168     }
169
170     /**
171      * Parses all imports.
172      *
173      * @param array  $content
174      * @param string $file
175      */
176     private function parseImports(array $content, $file)
177     {
178         if (!isset($content['imports'])) {
179             return;
180         }
181
182         if (!\is_array($content['imports'])) {
183             throw new InvalidArgumentException(sprintf('The "imports" key should contain an array in %s. Check your YAML syntax.', $file));
184         }
185
186         $defaultDirectory = \dirname($file);
187         foreach ($content['imports'] as $import) {
188             if (!\is_array($import)) {
189                 $import = array('resource' => $import);
190             }
191             if (!isset($import['resource'])) {
192                 throw new InvalidArgumentException(sprintf('An import should provide a resource in %s. Check your YAML syntax.', $file));
193             }
194
195             $this->setCurrentDir($defaultDirectory);
196             $this->import($import['resource'], isset($import['type']) ? $import['type'] : null, isset($import['ignore_errors']) ? (bool) $import['ignore_errors'] : false, $file);
197         }
198     }
199
200     /**
201      * Parses definitions.
202      *
203      * @param array  $content
204      * @param string $file
205      */
206     private function parseDefinitions(array $content, $file)
207     {
208         if (!isset($content['services'])) {
209             return;
210         }
211
212         if (!\is_array($content['services'])) {
213             throw new InvalidArgumentException(sprintf('The "services" key should contain an array in %s. Check your YAML syntax.', $file));
214         }
215
216         if (array_key_exists('_instanceof', $content['services'])) {
217             $instanceof = $content['services']['_instanceof'];
218             unset($content['services']['_instanceof']);
219
220             if (!\is_array($instanceof)) {
221                 throw new InvalidArgumentException(sprintf('Service "_instanceof" key must be an array, "%s" given in "%s".', \gettype($instanceof), $file));
222             }
223             $this->instanceof = array();
224             $this->isLoadingInstanceof = true;
225             foreach ($instanceof as $id => $service) {
226                 if (!$service || !\is_array($service)) {
227                     throw new InvalidArgumentException(sprintf('Type definition "%s" must be a non-empty array within "_instanceof" in %s. Check your YAML syntax.', $id, $file));
228                 }
229                 if (\is_string($service) && 0 === strpos($service, '@')) {
230                     throw new InvalidArgumentException(sprintf('Type definition "%s" cannot be an alias within "_instanceof" in %s. Check your YAML syntax.', $id, $file));
231                 }
232                 $this->parseDefinition($id, $service, $file, array());
233             }
234         }
235
236         $this->isLoadingInstanceof = false;
237         $defaults = $this->parseDefaults($content, $file);
238         foreach ($content['services'] as $id => $service) {
239             $this->parseDefinition($id, $service, $file, $defaults);
240         }
241     }
242
243     /**
244      * @param array  $content
245      * @param string $file
246      *
247      * @return array
248      *
249      * @throws InvalidArgumentException
250      */
251     private function parseDefaults(array &$content, $file)
252     {
253         if (!array_key_exists('_defaults', $content['services'])) {
254             return array();
255         }
256         $defaults = $content['services']['_defaults'];
257         unset($content['services']['_defaults']);
258
259         if (!\is_array($defaults)) {
260             throw new InvalidArgumentException(sprintf('Service "_defaults" key must be an array, "%s" given in "%s".', \gettype($defaults), $file));
261         }
262
263         foreach ($defaults as $key => $default) {
264             if (!isset(self::$defaultsKeywords[$key])) {
265                 throw new InvalidArgumentException(sprintf('The configuration key "%s" cannot be used to define a default value in "%s". Allowed keys are "%s".', $key, $file, implode('", "', self::$defaultsKeywords)));
266             }
267         }
268
269         if (isset($defaults['tags'])) {
270             if (!\is_array($tags = $defaults['tags'])) {
271                 throw new InvalidArgumentException(sprintf('Parameter "tags" in "_defaults" must be an array in %s. Check your YAML syntax.', $file));
272             }
273
274             foreach ($tags as $tag) {
275                 if (!\is_array($tag)) {
276                     $tag = array('name' => $tag);
277                 }
278
279                 if (!isset($tag['name'])) {
280                     throw new InvalidArgumentException(sprintf('A "tags" entry in "_defaults" is missing a "name" key in %s.', $file));
281                 }
282                 $name = $tag['name'];
283                 unset($tag['name']);
284
285                 if (!\is_string($name) || '' === $name) {
286                     throw new InvalidArgumentException(sprintf('The tag name in "_defaults" must be a non-empty string in %s.', $file));
287                 }
288
289                 foreach ($tag as $attribute => $value) {
290                     if (!is_scalar($value) && null !== $value) {
291                         throw new InvalidArgumentException(sprintf('Tag "%s", attribute "%s" in "_defaults" must be of a scalar-type in %s. Check your YAML syntax.', $name, $attribute, $file));
292                     }
293                 }
294             }
295         }
296
297         if (isset($defaults['bind'])) {
298             if (!\is_array($defaults['bind'])) {
299                 throw new InvalidArgumentException(sprintf('Parameter "bind" in "_defaults" must be an array in %s. Check your YAML syntax.', $file));
300             }
301
302             $defaults['bind'] = array_map(function ($v) { return new BoundArgument($v); }, $this->resolveServices($defaults['bind'], $file));
303         }
304
305         return $defaults;
306     }
307
308     /**
309      * @param array $service
310      *
311      * @return bool
312      */
313     private function isUsingShortSyntax(array $service)
314     {
315         foreach ($service as $key => $value) {
316             if (\is_string($key) && ('' === $key || '$' !== $key[0])) {
317                 return false;
318             }
319         }
320
321         return true;
322     }
323
324     /**
325      * Parses a definition.
326      *
327      * @param string       $id
328      * @param array|string $service
329      * @param string       $file
330      * @param array        $defaults
331      *
332      * @throws InvalidArgumentException When tags are invalid
333      */
334     private function parseDefinition($id, $service, $file, array $defaults)
335     {
336         if (preg_match('/^_[a-zA-Z0-9_]*$/', $id)) {
337             @trigger_error(sprintf('Service names that start with an underscore are deprecated since Symfony 3.3 and will be reserved in 4.0. Rename the "%s" service or define it in XML instead.', $id), E_USER_DEPRECATED);
338         }
339         if (\is_string($service) && 0 === strpos($service, '@')) {
340             $this->container->setAlias($id, $alias = new Alias(substr($service, 1)));
341             if (isset($defaults['public'])) {
342                 $alias->setPublic($defaults['public']);
343             }
344
345             return;
346         }
347
348         if (\is_array($service) && $this->isUsingShortSyntax($service)) {
349             $service = array('arguments' => $service);
350         }
351
352         if (null === $service) {
353             $service = array();
354         }
355
356         if (!\is_array($service)) {
357             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));
358         }
359
360         $this->checkDefinition($id, $service, $file);
361
362         if (isset($service['alias'])) {
363             $this->container->setAlias($id, $alias = new Alias($service['alias']));
364             if (array_key_exists('public', $service)) {
365                 $alias->setPublic($service['public']);
366             } elseif (isset($defaults['public'])) {
367                 $alias->setPublic($defaults['public']);
368             }
369
370             foreach ($service as $key => $value) {
371                 if (!\in_array($key, array('alias', 'public'))) {
372                     @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);
373                 }
374             }
375
376             return;
377         }
378
379         if ($this->isLoadingInstanceof) {
380             $definition = new ChildDefinition('');
381         } elseif (isset($service['parent'])) {
382             if (!empty($this->instanceof)) {
383                 throw new InvalidArgumentException(sprintf('The service "%s" cannot use the "parent" option in the same file where "_instanceof" configuration is defined as using both is not supported. Move your child definitions to a separate file.', $id));
384             }
385
386             foreach ($defaults as $k => $v) {
387                 if ('tags' === $k) {
388                     // since tags are never inherited from parents, there is no confusion
389                     // thus we can safely add them as defaults to ChildDefinition
390                     continue;
391                 }
392                 if ('bind' === $k) {
393                     throw new InvalidArgumentException(sprintf('Attribute "bind" on service "%s" cannot be inherited from "_defaults" when a "parent" is set. Move your child definitions to a separate file.', $id));
394                 }
395                 if (!isset($service[$k])) {
396                     throw new InvalidArgumentException(sprintf('Attribute "%s" on service "%s" cannot be inherited from "_defaults" when a "parent" is set. Move your child definitions to a separate file or define this attribute explicitly.', $k, $id));
397                 }
398             }
399
400             $definition = new ChildDefinition($service['parent']);
401         } else {
402             $definition = new Definition();
403
404             if (isset($defaults['public'])) {
405                 $definition->setPublic($defaults['public']);
406             }
407             if (isset($defaults['autowire'])) {
408                 $definition->setAutowired($defaults['autowire']);
409             }
410             if (isset($defaults['autoconfigure'])) {
411                 $definition->setAutoconfigured($defaults['autoconfigure']);
412             }
413
414             $definition->setChanges(array());
415         }
416
417         if (isset($service['class'])) {
418             $definition->setClass($service['class']);
419         }
420
421         if (isset($service['shared'])) {
422             $definition->setShared($service['shared']);
423         }
424
425         if (isset($service['synthetic'])) {
426             $definition->setSynthetic($service['synthetic']);
427         }
428
429         if (isset($service['lazy'])) {
430             $definition->setLazy($service['lazy']);
431         }
432
433         if (isset($service['public'])) {
434             $definition->setPublic($service['public']);
435         }
436
437         if (isset($service['abstract'])) {
438             $definition->setAbstract($service['abstract']);
439         }
440
441         if (array_key_exists('deprecated', $service)) {
442             $definition->setDeprecated(true, $service['deprecated']);
443         }
444
445         if (isset($service['factory'])) {
446             $definition->setFactory($this->parseCallable($service['factory'], 'factory', $id, $file));
447         }
448
449         if (isset($service['file'])) {
450             $definition->setFile($service['file']);
451         }
452
453         if (isset($service['arguments'])) {
454             $definition->setArguments($this->resolveServices($service['arguments'], $file));
455         }
456
457         if (isset($service['properties'])) {
458             $definition->setProperties($this->resolveServices($service['properties'], $file));
459         }
460
461         if (isset($service['configurator'])) {
462             $definition->setConfigurator($this->parseCallable($service['configurator'], 'configurator', $id, $file));
463         }
464
465         if (isset($service['calls'])) {
466             if (!\is_array($service['calls'])) {
467                 throw new InvalidArgumentException(sprintf('Parameter "calls" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
468             }
469
470             foreach ($service['calls'] as $call) {
471                 if (isset($call['method'])) {
472                     $method = $call['method'];
473                     $args = isset($call['arguments']) ? $this->resolveServices($call['arguments'], $file) : array();
474                 } else {
475                     $method = $call[0];
476                     $args = isset($call[1]) ? $this->resolveServices($call[1], $file) : array();
477                 }
478
479                 if (!\is_array($args)) {
480                     throw new InvalidArgumentException(sprintf('The second parameter for function call "%s" must be an array of its arguments for service "%s" in %s. Check your YAML syntax.', $method, $id, $file));
481                 }
482                 $definition->addMethodCall($method, $args);
483             }
484         }
485
486         $tags = isset($service['tags']) ? $service['tags'] : array();
487         if (!\is_array($tags)) {
488             throw new InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
489         }
490
491         if (isset($defaults['tags'])) {
492             $tags = array_merge($tags, $defaults['tags']);
493         }
494
495         foreach ($tags as $tag) {
496             if (!\is_array($tag)) {
497                 $tag = array('name' => $tag);
498             }
499
500             if (!isset($tag['name'])) {
501                 throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file));
502             }
503             $name = $tag['name'];
504             unset($tag['name']);
505
506             if (!\is_string($name) || '' === $name) {
507                 throw new InvalidArgumentException(sprintf('The tag name for service "%s" in %s must be a non-empty string.', $id, $file));
508             }
509
510             foreach ($tag as $attribute => $value) {
511                 if (!is_scalar($value) && null !== $value) {
512                     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));
513                 }
514             }
515
516             $definition->addTag($name, $tag);
517         }
518
519         if (isset($service['decorates'])) {
520             if ('' !== $service['decorates'] && '@' === $service['decorates'][0]) {
521                 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)));
522             }
523
524             $renameId = isset($service['decoration_inner_name']) ? $service['decoration_inner_name'] : null;
525             $priority = isset($service['decoration_priority']) ? $service['decoration_priority'] : 0;
526             $definition->setDecoratedService($service['decorates'], $renameId, $priority);
527         }
528
529         if (isset($service['autowire'])) {
530             $definition->setAutowired($service['autowire']);
531         }
532
533         if (isset($service['autowiring_types'])) {
534             if (\is_string($service['autowiring_types'])) {
535                 $definition->addAutowiringType($service['autowiring_types']);
536             } else {
537                 if (!\is_array($service['autowiring_types'])) {
538                     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));
539                 }
540
541                 foreach ($service['autowiring_types'] as $autowiringType) {
542                     if (!\is_string($autowiringType)) {
543                         throw new InvalidArgumentException(sprintf('A "autowiring_types" attribute must be of type string for service "%s" in %s. Check your YAML syntax.', $id, $file));
544                     }
545
546                     $definition->addAutowiringType($autowiringType);
547                 }
548             }
549         }
550
551         if (isset($defaults['bind']) || isset($service['bind'])) {
552             // deep clone, to avoid multiple process of the same instance in the passes
553             $bindings = isset($defaults['bind']) ? unserialize(serialize($defaults['bind'])) : array();
554
555             if (isset($service['bind'])) {
556                 if (!\is_array($service['bind'])) {
557                     throw new InvalidArgumentException(sprintf('Parameter "bind" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
558                 }
559
560                 $bindings = array_merge($bindings, $this->resolveServices($service['bind'], $file));
561             }
562
563             $definition->setBindings($bindings);
564         }
565
566         if (isset($service['autoconfigure'])) {
567             if (!$definition instanceof ChildDefinition) {
568                 $definition->setAutoconfigured($service['autoconfigure']);
569             } elseif ($service['autoconfigure']) {
570                 throw new InvalidArgumentException(sprintf('The service "%s" cannot have a "parent" and also have "autoconfigure". Try setting "autoconfigure: false" for the service.', $id));
571             }
572         }
573
574         if (array_key_exists('namespace', $service) && !array_key_exists('resource', $service)) {
575             throw new InvalidArgumentException(sprintf('A "resource" attribute must be set when the "namespace" attribute is set for service "%s" in %s. Check your YAML syntax.', $id, $file));
576         }
577
578         if (array_key_exists('resource', $service)) {
579             if (!\is_string($service['resource'])) {
580                 throw new InvalidArgumentException(sprintf('A "resource" attribute must be of type string for service "%s" in %s. Check your YAML syntax.', $id, $file));
581             }
582             $exclude = isset($service['exclude']) ? $service['exclude'] : null;
583             $namespace = isset($service['namespace']) ? $service['namespace'] : $id;
584             $this->registerClasses($definition, $namespace, $service['resource'], $exclude);
585         } else {
586             $this->setDefinition($id, $definition);
587         }
588     }
589
590     /**
591      * Parses a callable.
592      *
593      * @param string|array $callable  A callable
594      * @param string       $parameter A parameter (e.g. 'factory' or 'configurator')
595      * @param string       $id        A service identifier
596      * @param string       $file      A parsed file
597      *
598      * @throws InvalidArgumentException When errors are occuried
599      *
600      * @return string|array A parsed callable
601      */
602     private function parseCallable($callable, $parameter, $id, $file)
603     {
604         if (\is_string($callable)) {
605             if ('' !== $callable && '@' === $callable[0]) {
606                 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)));
607             }
608
609             if (false !== strpos($callable, ':') && false === strpos($callable, '::')) {
610                 $parts = explode(':', $callable);
611
612                 return array($this->resolveServices('@'.$parts[0], $file), $parts[1]);
613             }
614
615             return $callable;
616         }
617
618         if (\is_array($callable)) {
619             if (isset($callable[0]) && isset($callable[1])) {
620                 return array($this->resolveServices($callable[0], $file), $callable[1]);
621             }
622
623             if ('factory' === $parameter && isset($callable[1]) && null === $callable[0]) {
624                 return $callable;
625             }
626
627             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));
628         }
629
630         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));
631     }
632
633     /**
634      * Loads a YAML file.
635      *
636      * @param string $file
637      *
638      * @return array The file content
639      *
640      * @throws InvalidArgumentException when the given file is not a local file or when it does not exist
641      */
642     protected function loadFile($file)
643     {
644         if (!class_exists('Symfony\Component\Yaml\Parser')) {
645             throw new RuntimeException('Unable to load YAML config files as the Symfony Yaml Component is not installed.');
646         }
647
648         if (!stream_is_local($file)) {
649             throw new InvalidArgumentException(sprintf('This is not a local file "%s".', $file));
650         }
651
652         if (!file_exists($file)) {
653             throw new InvalidArgumentException(sprintf('The file "%s" does not exist.', $file));
654         }
655
656         if (null === $this->yamlParser) {
657             $this->yamlParser = new YamlParser();
658         }
659
660         $prevErrorHandler = set_error_handler(function ($level, $message, $script, $line) use ($file, &$prevErrorHandler) {
661             $message = E_USER_DEPRECATED === $level ? preg_replace('/ on line \d+/', ' in "'.$file.'"$0', $message) : $message;
662
663             return $prevErrorHandler ? $prevErrorHandler($level, $message, $script, $line) : false;
664         });
665
666         try {
667             $configuration = $this->yamlParser->parseFile($file, Yaml::PARSE_CONSTANT | Yaml::PARSE_CUSTOM_TAGS);
668         } catch (ParseException $e) {
669             throw new InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $file), 0, $e);
670         } finally {
671             restore_error_handler();
672         }
673
674         return $this->validate($configuration, $file);
675     }
676
677     /**
678      * Validates a YAML file.
679      *
680      * @param mixed  $content
681      * @param string $file
682      *
683      * @return array
684      *
685      * @throws InvalidArgumentException When service file is not valid
686      */
687     private function validate($content, $file)
688     {
689         if (null === $content) {
690             return $content;
691         }
692
693         if (!\is_array($content)) {
694             throw new InvalidArgumentException(sprintf('The service file "%s" is not valid. It should contain an array. Check your YAML syntax.', $file));
695         }
696
697         foreach ($content as $namespace => $data) {
698             if (\in_array($namespace, array('imports', 'parameters', 'services'))) {
699                 continue;
700             }
701
702             if (!$this->container->hasExtension($namespace)) {
703                 $extensionNamespaces = array_filter(array_map(function ($ext) { return $ext->getAlias(); }, $this->container->getExtensions()));
704                 throw new InvalidArgumentException(sprintf('There is no extension able to load the configuration for "%s" (in %s). Looked for namespace "%s", found %s', $namespace, $file, $namespace, $extensionNamespaces ? sprintf('"%s"', implode('", "', $extensionNamespaces)) : 'none'));
705             }
706         }
707
708         return $content;
709     }
710
711     /**
712      * Resolves services.
713      *
714      * @param mixed  $value
715      * @param string $file
716      * @param bool   $isParameter
717      *
718      * @return array|string|Reference|ArgumentInterface
719      */
720     private function resolveServices($value, $file, $isParameter = false)
721     {
722         if ($value instanceof TaggedValue) {
723             $argument = $value->getValue();
724             if ('iterator' === $value->getTag()) {
725                 if (!\is_array($argument)) {
726                     throw new InvalidArgumentException(sprintf('"!iterator" tag only accepts sequences in "%s".', $file));
727                 }
728                 $argument = $this->resolveServices($argument, $file, $isParameter);
729                 try {
730                     return new IteratorArgument($argument);
731                 } catch (InvalidArgumentException $e) {
732                     throw new InvalidArgumentException(sprintf('"!iterator" tag only accepts arrays of "@service" references in "%s".', $file));
733                 }
734             }
735             if ('tagged' === $value->getTag()) {
736                 if (!\is_string($argument) || !$argument) {
737                     throw new InvalidArgumentException(sprintf('"!tagged" tag only accepts non empty string in "%s".', $file));
738                 }
739
740                 return new TaggedIteratorArgument($argument);
741             }
742             if ('service' === $value->getTag()) {
743                 if ($isParameter) {
744                     throw new InvalidArgumentException(sprintf('Using an anonymous service in a parameter is not allowed in "%s".', $file));
745                 }
746
747                 $isLoadingInstanceof = $this->isLoadingInstanceof;
748                 $this->isLoadingInstanceof = false;
749                 $instanceof = $this->instanceof;
750                 $this->instanceof = array();
751
752                 $id = sprintf('%d_%s', ++$this->anonymousServicesCount, preg_replace('/^.*\\\\/', '', isset($argument['class']) ? $argument['class'] : '').$this->anonymousServicesSuffix);
753                 $this->parseDefinition($id, $argument, $file, array());
754
755                 if (!$this->container->hasDefinition($id)) {
756                     throw new InvalidArgumentException(sprintf('Creating an alias using the tag "!service" is not allowed in "%s".', $file));
757                 }
758
759                 $this->container->getDefinition($id)->setPublic(false);
760
761                 $this->isLoadingInstanceof = $isLoadingInstanceof;
762                 $this->instanceof = $instanceof;
763
764                 return new Reference($id);
765             }
766
767             throw new InvalidArgumentException(sprintf('Unsupported tag "!%s".', $value->getTag()));
768         }
769
770         if (\is_array($value)) {
771             foreach ($value as $k => $v) {
772                 $value[$k] = $this->resolveServices($v, $file, $isParameter);
773             }
774         } elseif (\is_string($value) && 0 === strpos($value, '@=')) {
775             if (!class_exists(Expression::class)) {
776                 throw new \LogicException(sprintf('The "@=" expression syntax cannot be used without the ExpressionLanguage component. Try running "composer require symfony/expression-language".'));
777             }
778
779             return new Expression(substr($value, 2));
780         } elseif (\is_string($value) && 0 === strpos($value, '@')) {
781             if (0 === strpos($value, '@@')) {
782                 $value = substr($value, 1);
783                 $invalidBehavior = null;
784             } elseif (0 === strpos($value, '@!')) {
785                 $value = substr($value, 2);
786                 $invalidBehavior = ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE;
787             } elseif (0 === strpos($value, '@?')) {
788                 $value = substr($value, 2);
789                 $invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
790             } else {
791                 $value = substr($value, 1);
792                 $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
793             }
794
795             if ('=' === substr($value, -1)) {
796                 @trigger_error(sprintf('The "=" suffix that used to disable strict references in Symfony 2.x is deprecated since Symfony 3.3 and will be unsupported in 4.0. Remove it in "%s".', $value), E_USER_DEPRECATED);
797                 $value = substr($value, 0, -1);
798             }
799
800             if (null !== $invalidBehavior) {
801                 $value = new Reference($value, $invalidBehavior);
802             }
803         }
804
805         return $value;
806     }
807
808     /**
809      * Loads from Extensions.
810      */
811     private function loadFromExtensions(array $content)
812     {
813         foreach ($content as $namespace => $values) {
814             if (\in_array($namespace, array('imports', 'parameters', 'services'))) {
815                 continue;
816             }
817
818             if (!\is_array($values) && null !== $values) {
819                 $values = array();
820             }
821
822             $this->container->loadFromExtension($namespace, $values);
823         }
824     }
825
826     /**
827      * Checks the keywords used to define a service.
828      *
829      * @param string $id         The service name
830      * @param array  $definition The service definition to check
831      * @param string $file       The loaded YAML file
832      */
833     private function checkDefinition($id, array $definition, $file)
834     {
835         if ($throw = $this->isLoadingInstanceof) {
836             $keywords = self::$instanceofKeywords;
837         } elseif ($throw = (isset($definition['resource']) || isset($definition['namespace']))) {
838             $keywords = self::$prototypeKeywords;
839         } else {
840             $keywords = self::$serviceKeywords;
841         }
842
843         foreach ($definition as $key => $value) {
844             if (!isset($keywords[$key])) {
845                 if ($throw) {
846                     throw new InvalidArgumentException(sprintf('The configuration key "%s" is unsupported for definition "%s" in "%s". Allowed configuration keys are "%s".', $key, $id, $file, implode('", "', $keywords)));
847                 }
848
849                 @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('", "', $keywords)), E_USER_DEPRECATED);
850             }
851         }
852     }
853 }