a1967802f6a30d37a8eebd2d935f8b0704822331
[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                     if (0 === strpos($id, '\\') && 1 < substr_count($id, '\\')) {
52                         throw new RuntimeException(sprintf(
53                             'The definition for "%s" has no class attribute, and appears to reference a class or interface. '
54                             .'Please specify the class attribute explicitly or remove the leading backslash by renaming '
55                             .'the service to "%s" to get rid of this error.',
56                             $id, substr($id, 1)
57                         ));
58                     }
59
60                     throw new RuntimeException(sprintf(
61                          'The definition for "%s" has no class attribute, and appears to reference a '
62                         .'class or interface in the global namespace. Leaving out the "class" attribute '
63                         .'is only allowed for namespaced classes. Please specify the class attribute '
64                         .'explicitly to get rid of this error.',
65                         $id
66                     ));
67                 }
68
69                 throw new RuntimeException(sprintf('The definition for "%s" has no class. If you intend to inject this service dynamically at runtime, please mark it as synthetic=true. If this is an abstract definition solely used by child definitions, please add abstract=true, otherwise specify a class to get rid of this error.', $id));
70             }
71
72             // tag attribute values must be scalars
73             foreach ($definition->getTags() as $name => $tags) {
74                 foreach ($tags as $attributes) {
75                     foreach ($attributes as $attribute => $value) {
76                         if (!is_scalar($value) && null !== $value) {
77                             throw new RuntimeException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s".', $id, $name, $attribute));
78                         }
79                     }
80                 }
81             }
82
83             if ($definition->isPublic() && !$definition->isPrivate()) {
84                 $resolvedId = $container->resolveEnvPlaceholders($id, null, $usedEnvs);
85                 if (null !== $usedEnvs) {
86                     throw new EnvParameterException(array($resolvedId), null, 'A service name ("%s") cannot contain dynamic values.');
87                 }
88             }
89         }
90
91         foreach ($container->getAliases() as $id => $alias) {
92             if ($alias->isPublic() && !$alias->isPrivate()) {
93                 $resolvedId = $container->resolveEnvPlaceholders($id, null, $usedEnvs);
94                 if (null !== $usedEnvs) {
95                     throw new EnvParameterException(array($resolvedId), null, 'An alias name ("%s") cannot contain dynamic values.');
96                 }
97             }
98         }
99     }
100 }