comparison vendor/symfony/dependency-injection/Compiler/CheckReferenceValidityPass.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
11 11
12 namespace Symfony\Component\DependencyInjection\Compiler; 12 namespace Symfony\Component\DependencyInjection\Compiler;
13 13
14 use Symfony\Component\DependencyInjection\Definition; 14 use Symfony\Component\DependencyInjection\Definition;
15 use Symfony\Component\DependencyInjection\Reference; 15 use Symfony\Component\DependencyInjection\Reference;
16 use Symfony\Component\DependencyInjection\ContainerBuilder;
17 use Symfony\Component\DependencyInjection\Exception\RuntimeException; 16 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
18 17
19 /** 18 /**
20 * Checks the validity of references. 19 * Checks the validity of references.
21 * 20 *
22 * The following checks are performed by this pass: 21 * The following checks are performed by this pass:
23 * - target definitions are not abstract 22 * - target definitions are not abstract
24 * 23 *
25 * @author Johannes M. Schmitt <schmittjoh@gmail.com> 24 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
26 */ 25 */
27 class CheckReferenceValidityPass implements CompilerPassInterface 26 class CheckReferenceValidityPass extends AbstractRecursivePass
28 { 27 {
29 private $container; 28 protected function processValue($value, $isRoot = false)
30 private $currentId; 29 {
30 if ($isRoot && $value instanceof Definition && ($value->isSynthetic() || $value->isAbstract())) {
31 return $value;
32 }
33 if ($value instanceof Reference && $this->container->hasDefinition((string) $value)) {
34 $targetDefinition = $this->container->getDefinition((string) $value);
31 35
32 /** 36 if ($targetDefinition->isAbstract()) {
33 * Processes the ContainerBuilder to validate References. 37 throw new RuntimeException(sprintf(
34 * 38 'The definition "%s" has a reference to an abstract definition "%s". '
35 * @param ContainerBuilder $container 39 .'Abstract definitions cannot be the target of references.',
36 */ 40 $this->currentId,
37 public function process(ContainerBuilder $container) 41 $value
38 { 42 ));
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 } 43 }
78 } 44 }
79 }
80 45
81 /** 46 return parent::processValue($value, $isRoot);
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 } 47 }
96 } 48 }