|
25 | 25 | *
|
26 | 26 | * @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
27 | 27 | */
|
28 |
| -class AnalyzeServiceReferencesPass implements RepeatablePassInterface |
| 28 | +class AnalyzeServiceReferencesPass extends AbstractRecursivePass implements RepeatablePassInterface |
29 | 29 | {
|
30 | 30 | private $graph;
|
31 |
| - private $container; |
32 |
| - private $currentId; |
33 | 31 | private $currentDefinition;
|
34 | 32 | private $repeatedPass;
|
35 | 33 | private $onlyConstructorArguments;
|
| 34 | + private $lazy; |
36 | 35 |
|
37 | 36 | /**
|
38 | 37 | * @param bool $onlyConstructorArguments Sets this Service Reference pass to ignore method calls
|
@@ -60,68 +59,60 @@ public function process(ContainerBuilder $container)
|
60 | 59 | $this->container = $container;
|
61 | 60 | $this->graph = $container->getCompiler()->getServiceReferenceGraph();
|
62 | 61 | $this->graph->clear();
|
63 |
| - |
64 |
| - foreach ($container->getDefinitions() as $id => $definition) { |
65 |
| - if ($definition->isSynthetic() || $definition->isAbstract()) { |
66 |
| - continue; |
67 |
| - } |
68 |
| - |
69 |
| - $this->currentId = $id; |
70 |
| - $this->currentDefinition = $definition; |
71 |
| - |
72 |
| - $this->processArguments($definition->getArguments()); |
73 |
| - if (is_array($definition->getFactory())) { |
74 |
| - $this->processArguments($definition->getFactory()); |
75 |
| - } |
76 |
| - |
77 |
| - if (!$this->onlyConstructorArguments) { |
78 |
| - $this->processArguments($definition->getMethodCalls()); |
79 |
| - $this->processArguments($definition->getProperties()); |
80 |
| - if ($definition->getConfigurator()) { |
81 |
| - $this->processArguments(array($definition->getConfigurator())); |
82 |
| - } |
83 |
| - } |
84 |
| - } |
| 62 | + $this->lazy = false; |
85 | 63 |
|
86 | 64 | foreach ($container->getAliases() as $id => $alias) {
|
87 | 65 | $this->graph->connect($id, $alias, (string) $alias, $this->getDefinition((string) $alias), null);
|
88 | 66 | }
|
| 67 | + |
| 68 | + parent::process($container); |
89 | 69 | }
|
90 | 70 |
|
91 |
| - /** |
92 |
| - * Processes service definitions for arguments to find relationships for the service graph. |
93 |
| - * |
94 |
| - * @param array $arguments An array of Reference or Definition objects relating to service definitions |
95 |
| - * @param bool $lazy Whether the references nested in the arguments should be considered lazy or not |
96 |
| - */ |
97 |
| - private function processArguments(array $arguments, $lazy = false) |
| 71 | + protected function processValue($value, $isRoot = false) |
98 | 72 | {
|
99 |
| - foreach ($arguments as $argument) { |
100 |
| - if (is_array($argument)) { |
101 |
| - $this->processArguments($argument, $lazy); |
102 |
| - } elseif ($argument instanceof ArgumentInterface) { |
103 |
| - $this->processArguments($argument->getValues(), true); |
104 |
| - } elseif ($argument instanceof Reference) { |
105 |
| - $targetDefinition = $this->getDefinition((string) $argument); |
106 |
| - |
107 |
| - $this->graph->connect( |
108 |
| - $this->currentId, |
109 |
| - $this->currentDefinition, |
110 |
| - $this->getDefinitionId((string) $argument), |
111 |
| - $targetDefinition, |
112 |
| - $argument, |
113 |
| - $lazy || ($targetDefinition && $targetDefinition->isLazy()) |
114 |
| - ); |
115 |
| - } elseif ($argument instanceof Definition) { |
116 |
| - $this->processArguments($argument->getArguments()); |
117 |
| - $this->processArguments($argument->getMethodCalls()); |
118 |
| - $this->processArguments($argument->getProperties()); |
119 |
| - |
120 |
| - if (is_array($argument->getFactory())) { |
121 |
| - $this->processArguments($argument->getFactory()); |
122 |
| - } |
| 73 | + $lazy = $this->lazy; |
| 74 | + |
| 75 | + if ($value instanceof ArgumentInterface) { |
| 76 | + $this->lazy = true; |
| 77 | + parent::processValue($value); |
| 78 | + $this->lazy = $lazy; |
| 79 | + |
| 80 | + return $value; |
| 81 | + } |
| 82 | + if ($value instanceof Reference) { |
| 83 | + $targetDefinition = $this->getDefinition((string) $value); |
| 84 | + |
| 85 | + $this->graph->connect( |
| 86 | + $this->currentId, |
| 87 | + $this->currentDefinition, |
| 88 | + $this->getDefinitionId((string) $value), |
| 89 | + $targetDefinition, |
| 90 | + $value, |
| 91 | + $this->lazy || ($targetDefinition && $targetDefinition->isLazy()) |
| 92 | + ); |
| 93 | + |
| 94 | + return $value; |
| 95 | + } |
| 96 | + if (!$value instanceof Definition) { |
| 97 | + return parent::processValue($value, $isRoot); |
| 98 | + } |
| 99 | + if ($isRoot) { |
| 100 | + if ($value->isSynthetic() || $value->isAbstract()) { |
| 101 | + return $value; |
123 | 102 | }
|
| 103 | + $this->currentDefinition = $value; |
124 | 104 | }
|
| 105 | + $this->lazy = false; |
| 106 | + |
| 107 | + if ($this->onlyConstructorArguments) { |
| 108 | + $this->processValue($value->getFactory()); |
| 109 | + $this->processValue($value->getArguments()); |
| 110 | + } else { |
| 111 | + parent::processValue($value, $isRoot); |
| 112 | + } |
| 113 | + $this->lazy = $lazy; |
| 114 | + |
| 115 | + return $value; |
125 | 116 | }
|
126 | 117 |
|
127 | 118 | /**
|
|
0 commit comments