Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / symfony / dependency-injection / Compiler / CheckDefinitionValidityPass.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\Compiler;
13
14 use Symfony\Component\DependencyInjection\ContainerBuilder;
15 use Symfony\Component\DependencyInjection\Exception\EnvParameterException;
16 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
17
18 /**
19  * This pass validates each definition individually only taking the information
20  * into account which is contained in the definition itself.
21  *
22  * Later passes can rely on the following, and specifically do not need to
23  * perform these checks themselves:
24  *
25  * - non synthetic, non abstract services always have a class set
26  * - synthetic services are always public
27  *
28  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
29  */
30 class CheckDefinitionValidityPass implements CompilerPassInterface
31 {
32     /**
33      * Processes the ContainerBuilder to validate the Definition.
34      *
35      * @throws RuntimeException When the Definition is invalid
36      */
37     public function process(ContainerBuilder $container)
38     {
39         foreach ($container->getDefinitions() as $id => $definition) {
40             // synthetic service is public
41             if ($definition->isSynthetic() && !($definition->isPublic() || $definition->isPrivate())) {
42                 throw new RuntimeException(sprintf('A synthetic service ("%s") must be public.', $id));
43             }
44
45             // non-synthetic, non-abstract service has class
46             if (!$definition->isAbstract() && !$definition->isSynthetic() && !$definition->getClass()) {
47                 if ($definition->getFactory()) {
48                     throw new RuntimeException(sprintf('Please add the class to service "%s" even if it is constructed by a factory since we might need to add method calls based on compile-time checks.', $id));
49                 }
50                 if (class_exists($id) || interface_exists($id, false)) {
51                     throw new RuntimeException(sprintf(
52                          'The definition for "%s" has no class attribute, and appears to reference a '
53                         .'class or interface in the global namespace. Leaving out the "class" attribute '
54                         .'is only allowed for namespaced classes. Please specify the class attribute '
55                         .'explicitly to get rid of this error.',
56                         $id
57                     ));
58                 }
59
60                 throw new RuntimeException(sprintf(
61                     'The definition for "%s" has no class. If you intend to inject '
62                    .'this service dynamically at runtime, please mark it as synthetic=true. '
63                    .'If this is an abstract definition solely used by child definitions, '
64                    .'please add abstract=true, otherwise specify a class to get rid of this error.',
65                    $id
66                 ));
67             }
68
69             // tag attribute values must be scalars
70             foreach ($definition->getTags() as $name => $tags) {
71                 foreach ($tags as $attributes) {
72                     foreach ($attributes as $attribute => $value) {
73                         if (!is_scalar($value) && null !== $value) {
74                             throw new RuntimeException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s".', $id, $name, $attribute));
75                         }
76                     }
77                 }
78             }
79
80             if ($definition->isPublic() && !$definition->isPrivate()) {
81                 $resolvedId = $container->resolveEnvPlaceholders($id, null, $usedEnvs);
82                 if (null !== $usedEnvs) {
83                     throw new EnvParameterException(array($resolvedId), null, 'A service name ("%s") cannot contain dynamic values.');
84                 }
85             }
86         }
87
88         foreach ($container->getAliases() as $id => $alias) {
89             if ($alias->isPublic() && !$alias->isPrivate()) {
90                 $resolvedId = $container->resolveEnvPlaceholders($id, null, $usedEnvs);
91                 if (null !== $usedEnvs) {
92                     throw new EnvParameterException(array($resolvedId), null, 'An alias name ("%s") cannot contain dynamic values.');
93                 }
94             }
95         }
96     }
97 }