Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / dependency-injection / Compiler / CheckReferenceValidityPass.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\Definition;
15 use Symfony\Component\DependencyInjection\Reference;
16 use Symfony\Component\DependencyInjection\ContainerBuilder;
17 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
18
19 /**
20  * Checks the validity of references.
21  *
22  * The following checks are performed by this pass:
23  * - target definitions are not abstract
24  *
25  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
26  */
27 class CheckReferenceValidityPass implements CompilerPassInterface
28 {
29     private $container;
30     private $currentId;
31
32     /**
33      * Processes the ContainerBuilder to validate References.
34      *
35      * @param ContainerBuilder $container
36      */
37     public function process(ContainerBuilder $container)
38     {
39         $this->container = $container;
40
41         foreach ($container->getDefinitions() as $id => $definition) {
42             if ($definition->isSynthetic() || $definition->isAbstract()) {
43                 continue;
44             }
45
46             $this->currentId = $id;
47
48             $this->validateReferences($definition->getArguments());
49             $this->validateReferences($definition->getMethodCalls());
50             $this->validateReferences($definition->getProperties());
51         }
52     }
53
54     /**
55      * Validates an array of References.
56      *
57      * @param array $arguments An array of Reference objects
58      *
59      * @throws RuntimeException when there is a reference to an abstract definition.
60      */
61     private function validateReferences(array $arguments)
62     {
63         foreach ($arguments as $argument) {
64             if (is_array($argument)) {
65                 $this->validateReferences($argument);
66             } elseif ($argument instanceof Reference) {
67                 $targetDefinition = $this->getDefinition((string) $argument);
68
69                 if (null !== $targetDefinition && $targetDefinition->isAbstract()) {
70                     throw new RuntimeException(sprintf(
71                         'The definition "%s" has a reference to an abstract definition "%s". '
72                        .'Abstract definitions cannot be the target of references.',
73                        $this->currentId,
74                        $argument
75                     ));
76                 }
77             }
78         }
79     }
80
81     /**
82      * Returns the Definition given an id.
83      *
84      * @param string $id Definition identifier
85      *
86      * @return Definition
87      */
88     private function getDefinition($id)
89     {
90         if (!$this->container->hasDefinition($id)) {
91             return;
92         }
93
94         return $this->container->getDefinition($id);
95     }
96 }