Version 1
[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\ContainerInterface;
15 use Symfony\Component\DependencyInjection\ContainerBuilder;
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  * - synthetic services are always of non-prototype scope
28  * - shared services are always of non-prototype scope
29  *
30  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
31  */
32 class CheckDefinitionValidityPass implements CompilerPassInterface
33 {
34     /**
35      * Processes the ContainerBuilder to validate the Definition.
36      *
37      * @param ContainerBuilder $container
38      *
39      * @throws RuntimeException When the Definition is invalid
40      */
41     public function process(ContainerBuilder $container)
42     {
43         foreach ($container->getDefinitions() as $id => $definition) {
44             // synthetic service is public
45             if ($definition->isSynthetic() && !$definition->isPublic()) {
46                 throw new RuntimeException(sprintf('A synthetic service ("%s") must be public.', $id));
47             }
48
49             // synthetic service has non-prototype scope
50             if ($definition->isSynthetic() && ContainerInterface::SCOPE_PROTOTYPE === $definition->getScope(false)) {
51                 throw new RuntimeException(sprintf('A synthetic service ("%s") cannot be of scope "prototype".', $id));
52             }
53
54             // shared service has non-prototype scope
55             if ($definition->isShared() && ContainerInterface::SCOPE_PROTOTYPE === $definition->getScope(false)) {
56                 throw new RuntimeException(sprintf('A shared service ("%s") cannot be of scope "prototype".', $id));
57             }
58
59             if ($definition->getFactory() && ($definition->getFactoryClass(false) || $definition->getFactoryService(false) || $definition->getFactoryMethod(false))) {
60                 throw new RuntimeException(sprintf('A service ("%s") can use either the old or the new factory syntax, not both.', $id));
61             }
62
63             // non-synthetic, non-abstract service has class
64             if (!$definition->isAbstract() && !$definition->isSynthetic() && !$definition->getClass()) {
65                 if ($definition->getFactory() || $definition->getFactoryClass(false) || $definition->getFactoryService(false)) {
66                     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));
67                 }
68
69                 throw new RuntimeException(sprintf(
70                     'The definition for "%s" has no class. If you intend to inject '
71                    .'this service dynamically at runtime, please mark it as synthetic=true. '
72                    .'If this is an abstract definition solely used by child definitions, '
73                    .'please add abstract=true, otherwise specify a class to get rid of this error.',
74                    $id
75                 ));
76             }
77
78             // tag attribute values must be scalars
79             foreach ($definition->getTags() as $name => $tags) {
80                 foreach ($tags as $attributes) {
81                     foreach ($attributes as $attribute => $value) {
82                         if (!is_scalar($value) && null !== $value) {
83                             throw new RuntimeException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s".', $id, $name, $attribute));
84                         }
85                     }
86                 }
87             }
88         }
89     }
90 }