Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / symfony / dependency-injection / Compiler / ResolveInvalidReferencesPass.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\Argument\ArgumentInterface;
15 use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
16 use Symfony\Component\DependencyInjection\ContainerInterface;
17 use Symfony\Component\DependencyInjection\Definition;
18 use Symfony\Component\DependencyInjection\Reference;
19 use Symfony\Component\DependencyInjection\ContainerBuilder;
20 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
21
22 /**
23  * Emulates the invalid behavior if the reference is not found within the
24  * container.
25  *
26  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
27  */
28 class ResolveInvalidReferencesPass implements CompilerPassInterface
29 {
30     private $container;
31     private $signalingException;
32
33     /**
34      * Process the ContainerBuilder to resolve invalid references.
35      */
36     public function process(ContainerBuilder $container)
37     {
38         $this->container = $container;
39         $this->signalingException = new RuntimeException('Invalid reference.');
40
41         try {
42             $this->processValue($container->getDefinitions(), 1);
43         } finally {
44             $this->container = $this->signalingException = null;
45         }
46     }
47
48     /**
49      * Processes arguments to determine invalid references.
50      *
51      * @throws RuntimeException When an invalid reference is found
52      */
53     private function processValue($value, $rootLevel = 0, $level = 0)
54     {
55         if ($value instanceof ServiceClosureArgument) {
56             $value->setValues($this->processValue($value->getValues(), 1, 1));
57         } elseif ($value instanceof ArgumentInterface) {
58             $value->setValues($this->processValue($value->getValues(), $rootLevel, 1 + $level));
59         } elseif ($value instanceof Definition) {
60             if ($value->isSynthetic() || $value->isAbstract()) {
61                 return $value;
62             }
63             $value->setArguments($this->processValue($value->getArguments(), 0));
64             $value->setProperties($this->processValue($value->getProperties(), 1));
65             $value->setMethodCalls($this->processValue($value->getMethodCalls(), 2));
66         } elseif (is_array($value)) {
67             $i = 0;
68
69             foreach ($value as $k => $v) {
70                 try {
71                     if (false !== $i && $k !== $i++) {
72                         $i = false;
73                     }
74                     if ($v !== $processedValue = $this->processValue($v, $rootLevel, 1 + $level)) {
75                         $value[$k] = $processedValue;
76                     }
77                 } catch (RuntimeException $e) {
78                     if ($rootLevel < $level || ($rootLevel && !$level)) {
79                         unset($value[$k]);
80                     } elseif ($rootLevel) {
81                         throw $e;
82                     } else {
83                         $value[$k] = null;
84                     }
85                 }
86             }
87
88             // Ensure numerically indexed arguments have sequential numeric keys.
89             if (false !== $i) {
90                 $value = array_values($value);
91             }
92         } elseif ($value instanceof Reference) {
93             if ($this->container->has($value)) {
94                 return $value;
95             }
96             $invalidBehavior = $value->getInvalidBehavior();
97
98             // resolve invalid behavior
99             if (ContainerInterface::NULL_ON_INVALID_REFERENCE === $invalidBehavior) {
100                 $value = null;
101             } elseif (ContainerInterface::IGNORE_ON_INVALID_REFERENCE === $invalidBehavior) {
102                 if (0 < $level || $rootLevel) {
103                     throw $this->signalingException;
104                 }
105                 $value = null;
106             }
107         }
108
109         return $value;
110     }
111 }