comparison vendor/symfony/dependency-injection/Compiler/CheckExceptionOnInvalidReferenceBehaviorPass.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 c2387f117808
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
9 * file that was distributed with this source code. 9 * file that was distributed with this source code.
10 */ 10 */
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\Exception\InvalidArgumentException;
15 use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; 15 use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
16 use Symfony\Component\DependencyInjection\ContainerInterface; 16 use Symfony\Component\DependencyInjection\ContainerInterface;
17 use Symfony\Component\DependencyInjection\Reference; 17 use Symfony\Component\DependencyInjection\Reference;
18 use Symfony\Component\DependencyInjection\ContainerBuilder;
19 18
20 /** 19 /**
21 * Checks that all references are pointing to a valid service. 20 * Checks that all references are pointing to a valid service.
22 * 21 *
23 * @author Johannes M. Schmitt <schmittjoh@gmail.com> 22 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
24 */ 23 */
25 class CheckExceptionOnInvalidReferenceBehaviorPass implements CompilerPassInterface 24 class CheckExceptionOnInvalidReferenceBehaviorPass extends AbstractRecursivePass
26 { 25 {
27 private $container; 26 protected function processValue($value, $isRoot = false)
28 private $sourceId; 27 {
28 if (!$value instanceof Reference) {
29 return parent::processValue($value, $isRoot);
30 }
31 if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE === $value->getInvalidBehavior() && !$this->container->has($id = (string) $value)) {
32 throw new ServiceNotFoundException($id, $this->currentId);
33 }
34 if (ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $value->getInvalidBehavior() && $this->container->has($id = (string) $value) && !$this->container->findDefinition($id)->isShared()) {
35 throw new InvalidArgumentException(sprintf('Invalid ignore-on-uninitialized reference found in service "%s": target service "%s" is not shared.', $this->currentId, $id));
36 }
29 37
30 public function process(ContainerBuilder $container) 38 return $value;
31 {
32 $this->container = $container;
33
34 foreach ($container->getDefinitions() as $id => $definition) {
35 $this->sourceId = $id;
36 $this->processDefinition($definition);
37 }
38 }
39
40 private function processDefinition(Definition $definition)
41 {
42 $this->processReferences($definition->getArguments());
43 $this->processReferences($definition->getMethodCalls());
44 $this->processReferences($definition->getProperties());
45 }
46
47 private function processReferences(array $arguments)
48 {
49 foreach ($arguments as $argument) {
50 if (is_array($argument)) {
51 $this->processReferences($argument);
52 } elseif ($argument instanceof Definition) {
53 $this->processDefinition($argument);
54 } elseif ($argument instanceof Reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE === $argument->getInvalidBehavior()) {
55 $destId = (string) $argument;
56
57 if (!$this->container->has($destId)) {
58 throw new ServiceNotFoundException($destId, $this->sourceId);
59 }
60 }
61 }
62 } 39 }
63 } 40 }