Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/dependency-injection/Compiler/InlineServiceDefinitionsPass.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 7a779792577d |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
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\Definition; | |
15 use Symfony\Component\DependencyInjection\Reference; | |
16 use Symfony\Component\DependencyInjection\ContainerBuilder; | |
17 | |
18 /** | |
19 * Inline service definitions where this is possible. | |
20 * | |
21 * @author Johannes M. Schmitt <schmittjoh@gmail.com> | |
22 */ | |
23 class InlineServiceDefinitionsPass implements RepeatablePassInterface | |
24 { | |
25 private $repeatedPass; | |
26 private $graph; | |
27 private $compiler; | |
28 private $formatter; | |
29 private $currentId; | |
30 | |
31 /** | |
32 * {@inheritdoc} | |
33 */ | |
34 public function setRepeatedPass(RepeatedPass $repeatedPass) | |
35 { | |
36 $this->repeatedPass = $repeatedPass; | |
37 } | |
38 | |
39 /** | |
40 * Processes the ContainerBuilder for inline service definitions. | |
41 * | |
42 * @param ContainerBuilder $container | |
43 */ | |
44 public function process(ContainerBuilder $container) | |
45 { | |
46 $this->compiler = $container->getCompiler(); | |
47 $this->formatter = $this->compiler->getLoggingFormatter(); | |
48 $this->graph = $this->compiler->getServiceReferenceGraph(); | |
49 | |
50 $container->setDefinitions($this->inlineArguments($container, $container->getDefinitions(), true)); | |
51 } | |
52 | |
53 /** | |
54 * Processes inline arguments. | |
55 * | |
56 * @param ContainerBuilder $container The ContainerBuilder | |
57 * @param array $arguments An array of arguments | |
58 * @param bool $isRoot If we are processing the root definitions or not | |
59 * | |
60 * @return array | |
61 */ | |
62 private function inlineArguments(ContainerBuilder $container, array $arguments, $isRoot = false) | |
63 { | |
64 foreach ($arguments as $k => $argument) { | |
65 if ($isRoot) { | |
66 $this->currentId = $k; | |
67 } | |
68 if (is_array($argument)) { | |
69 $arguments[$k] = $this->inlineArguments($container, $argument); | |
70 } elseif ($argument instanceof Reference) { | |
71 if (!$container->hasDefinition($id = (string) $argument)) { | |
72 continue; | |
73 } | |
74 | |
75 if ($this->isInlineableDefinition($id, $definition = $container->getDefinition($id))) { | |
76 $this->compiler->addLogMessage($this->formatter->formatInlineService($this, $id, $this->currentId)); | |
77 | |
78 if ($definition->isShared()) { | |
79 $arguments[$k] = $definition; | |
80 } else { | |
81 $arguments[$k] = clone $definition; | |
82 } | |
83 } | |
84 } elseif ($argument instanceof Definition) { | |
85 $argument->setArguments($this->inlineArguments($container, $argument->getArguments())); | |
86 $argument->setMethodCalls($this->inlineArguments($container, $argument->getMethodCalls())); | |
87 $argument->setProperties($this->inlineArguments($container, $argument->getProperties())); | |
88 | |
89 $configurator = $this->inlineArguments($container, array($argument->getConfigurator())); | |
90 $argument->setConfigurator($configurator[0]); | |
91 | |
92 $factory = $this->inlineArguments($container, array($argument->getFactory())); | |
93 $argument->setFactory($factory[0]); | |
94 } | |
95 } | |
96 | |
97 return $arguments; | |
98 } | |
99 | |
100 /** | |
101 * Checks if the definition is inlineable. | |
102 * | |
103 * @param string $id | |
104 * @param Definition $definition | |
105 * | |
106 * @return bool If the definition is inlineable | |
107 */ | |
108 private function isInlineableDefinition($id, Definition $definition) | |
109 { | |
110 if (!$definition->isShared()) { | |
111 return true; | |
112 } | |
113 | |
114 if ($definition->isPublic() || $definition->isLazy()) { | |
115 return false; | |
116 } | |
117 | |
118 if (!$this->graph->hasNode($id)) { | |
119 return true; | |
120 } | |
121 | |
122 if ($this->currentId == $id) { | |
123 return false; | |
124 } | |
125 | |
126 $ids = array(); | |
127 foreach ($this->graph->getNode($id)->getInEdges() as $edge) { | |
128 $ids[] = $edge->getSourceNode()->getId(); | |
129 } | |
130 | |
131 if (count(array_unique($ids)) > 1) { | |
132 return false; | |
133 } | |
134 | |
135 if (count($ids) > 1 && is_array($factory = $definition->getFactory()) && ($factory[0] instanceof Reference || $factory[0] instanceof Definition)) { | |
136 return false; | |
137 } | |
138 | |
139 return true; | |
140 } | |
141 } |