c64348ed1de3bbb62cd509aa9a590c5f5b9219ed
[yaffs-website] / vendor / symfony / dependency-injection / Compiler / InlineServiceDefinitionsPass.php
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\Argument\ArgumentInterface;
15 use Symfony\Component\DependencyInjection\Definition;
16 use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
17 use Symfony\Component\DependencyInjection\Reference;
18
19 /**
20  * Inline service definitions where this is possible.
21  *
22  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
23  */
24 class InlineServiceDefinitionsPass extends AbstractRecursivePass implements RepeatablePassInterface
25 {
26     private $cloningIds = array();
27     private $inlinedServiceIds = array();
28
29     /**
30      * {@inheritdoc}
31      */
32     public function setRepeatedPass(RepeatedPass $repeatedPass)
33     {
34         // no-op for BC
35     }
36
37     /**
38      * Returns an array of all services inlined by this pass.
39      *
40      * The key is the inlined service id and its value is the list of services it was inlined into.
41      *
42      * @deprecated since version 3.4, to be removed in 4.0.
43      *
44      * @return array
45      */
46     public function getInlinedServiceIds()
47     {
48         @trigger_error('Calling InlineServiceDefinitionsPass::getInlinedServiceIds() is deprecated since Symfony 3.4 and will be removed in 4.0.', E_USER_DEPRECATED);
49
50         return $this->inlinedServiceIds;
51     }
52
53     /**
54      * {@inheritdoc}
55      */
56     protected function processValue($value, $isRoot = false)
57     {
58         if ($value instanceof ArgumentInterface) {
59             // Reference found in ArgumentInterface::getValues() are not inlineable
60             return $value;
61         }
62
63         if ($value instanceof Definition && $this->cloningIds) {
64             if ($value->isShared()) {
65                 return $value;
66             }
67             $value = clone $value;
68         }
69
70         if (!$value instanceof Reference || !$this->container->hasDefinition($id = $this->container->normalizeId($value))) {
71             return parent::processValue($value, $isRoot);
72         }
73
74         $definition = $this->container->getDefinition($id);
75
76         if (!$this->isInlineableDefinition($id, $definition, $this->container->getCompiler()->getServiceReferenceGraph())) {
77             return $value;
78         }
79
80         $this->container->log($this, sprintf('Inlined service "%s" to "%s".', $id, $this->currentId));
81         $this->inlinedServiceIds[$id][] = $this->currentId;
82
83         if ($definition->isShared()) {
84             return $definition;
85         }
86
87         if (isset($this->cloningIds[$id])) {
88             $ids = array_keys($this->cloningIds);
89             $ids[] = $id;
90
91             throw new ServiceCircularReferenceException($id, array_slice($ids, array_search($id, $ids)));
92         }
93
94         $this->cloningIds[$id] = true;
95         try {
96             return $this->processValue($definition);
97         } finally {
98             unset($this->cloningIds[$id]);
99         }
100     }
101
102     /**
103      * Checks if the definition is inlineable.
104      *
105      * @return bool If the definition is inlineable
106      */
107     private function isInlineableDefinition($id, Definition $definition, ServiceReferenceGraph $graph)
108     {
109         if (!$definition->isShared()) {
110             return true;
111         }
112
113         if ($definition->isDeprecated() || $definition->isPublic() || $definition->isPrivate() || $definition->isLazy()) {
114             return false;
115         }
116
117         if (!$graph->hasNode($id)) {
118             return true;
119         }
120
121         if ($this->currentId == $id) {
122             return false;
123         }
124
125         $ids = array();
126         foreach ($graph->getNode($id)->getInEdges() as $edge) {
127             if ($edge->isWeak()) {
128                 return false;
129             }
130             $ids[] = $edge->getSourceNode()->getId();
131         }
132
133         if (count(array_unique($ids)) > 1) {
134             return false;
135         }
136
137         if (count($ids) > 1 && is_array($factory = $definition->getFactory()) && ($factory[0] instanceof Reference || $factory[0] instanceof Definition)) {
138             return false;
139         }
140
141         return true;
142     }
143 }