comparison vendor/symfony/dependency-injection/Compiler/ResolveChildDefinitionsPass.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents c2387f117808
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
13 13
14 use Symfony\Component\DependencyInjection\ChildDefinition; 14 use Symfony\Component\DependencyInjection\ChildDefinition;
15 use Symfony\Component\DependencyInjection\Definition; 15 use Symfony\Component\DependencyInjection\Definition;
16 use Symfony\Component\DependencyInjection\Exception\ExceptionInterface; 16 use Symfony\Component\DependencyInjection\Exception\ExceptionInterface;
17 use Symfony\Component\DependencyInjection\Exception\RuntimeException; 17 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
18 use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
18 19
19 /** 20 /**
20 * This replaces all ChildDefinition instances with their equivalent fully 21 * This replaces all ChildDefinition instances with their equivalent fully
21 * merged Definition instance. 22 * merged Definition instance.
22 * 23 *
23 * @author Johannes M. Schmitt <schmittjoh@gmail.com> 24 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
24 * @author Nicolas Grekas <p@tchwork.com> 25 * @author Nicolas Grekas <p@tchwork.com>
25 */ 26 */
26 class ResolveChildDefinitionsPass extends AbstractRecursivePass 27 class ResolveChildDefinitionsPass extends AbstractRecursivePass
27 { 28 {
29 private $currentPath;
30
28 protected function processValue($value, $isRoot = false) 31 protected function processValue($value, $isRoot = false)
29 { 32 {
30 if (!$value instanceof Definition) { 33 if (!$value instanceof Definition) {
31 return parent::processValue($value, $isRoot); 34 return parent::processValue($value, $isRoot);
32 } 35 }
34 // yes, we are specifically fetching the definition from the 37 // yes, we are specifically fetching the definition from the
35 // container to ensure we are not operating on stale data 38 // container to ensure we are not operating on stale data
36 $value = $this->container->getDefinition($this->currentId); 39 $value = $this->container->getDefinition($this->currentId);
37 } 40 }
38 if ($value instanceof ChildDefinition) { 41 if ($value instanceof ChildDefinition) {
42 $this->currentPath = [];
39 $value = $this->resolveDefinition($value); 43 $value = $this->resolveDefinition($value);
40 if ($isRoot) { 44 if ($isRoot) {
41 $this->container->setDefinition($this->currentId, $value); 45 $this->container->setDefinition($this->currentId, $value);
42 } 46 }
43 } 47 }
54 */ 58 */
55 private function resolveDefinition(ChildDefinition $definition) 59 private function resolveDefinition(ChildDefinition $definition)
56 { 60 {
57 try { 61 try {
58 return $this->doResolveDefinition($definition); 62 return $this->doResolveDefinition($definition);
63 } catch (ServiceCircularReferenceException $e) {
64 throw $e;
59 } catch (ExceptionInterface $e) { 65 } catch (ExceptionInterface $e) {
60 $r = new \ReflectionProperty($e, 'message'); 66 $r = new \ReflectionProperty($e, 'message');
61 $r->setAccessible(true); 67 $r->setAccessible(true);
62 $r->setValue($e, sprintf('Service "%s": %s', $this->currentId, $e->getMessage())); 68 $r->setValue($e, sprintf('Service "%s": %s', $this->currentId, $e->getMessage()));
63 69
67 73
68 private function doResolveDefinition(ChildDefinition $definition) 74 private function doResolveDefinition(ChildDefinition $definition)
69 { 75 {
70 if (!$this->container->has($parent = $definition->getParent())) { 76 if (!$this->container->has($parent = $definition->getParent())) {
71 throw new RuntimeException(sprintf('Parent definition "%s" does not exist.', $parent)); 77 throw new RuntimeException(sprintf('Parent definition "%s" does not exist.', $parent));
78 }
79
80 $searchKey = array_search($parent, $this->currentPath);
81 $this->currentPath[] = $parent;
82
83 if (false !== $searchKey) {
84 throw new ServiceCircularReferenceException($parent, \array_slice($this->currentPath, $searchKey));
72 } 85 }
73 86
74 $parentDef = $this->container->findDefinition($parent); 87 $parentDef = $this->container->findDefinition($parent);
75 if ($parentDef instanceof ChildDefinition) { 88 if ($parentDef instanceof ChildDefinition) {
76 $id = $this->currentId; 89 $id = $this->currentId;
148 // merge arguments 161 // merge arguments
149 foreach ($definition->getArguments() as $k => $v) { 162 foreach ($definition->getArguments() as $k => $v) {
150 if (is_numeric($k)) { 163 if (is_numeric($k)) {
151 $def->addArgument($v); 164 $def->addArgument($v);
152 } elseif (0 === strpos($k, 'index_')) { 165 } elseif (0 === strpos($k, 'index_')) {
153 $def->replaceArgument((int) substr($k, strlen('index_')), $v); 166 $def->replaceArgument((int) substr($k, \strlen('index_')), $v);
154 } else { 167 } else {
155 $def->setArgument($k, $v); 168 $def->setArgument($k, $v);
156 } 169 }
157 } 170 }
158 171