|
0
|
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\Alias; |
|
|
15 |
use Symfony\Component\DependencyInjection\Reference; |
|
|
16 |
use Symfony\Component\DependencyInjection\Definition; |
|
|
17 |
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
|
18 |
|
|
|
19 |
/** |
|
|
20 |
* Removes unused service definitions from the container. |
|
|
21 |
* |
|
|
22 |
* @author Johannes M. Schmitt <schmittjoh@gmail.com> |
|
|
23 |
*/ |
|
|
24 |
class RemoveUnusedDefinitionsPass implements RepeatablePassInterface |
|
|
25 |
{ |
|
|
26 |
private $repeatedPass; |
|
|
27 |
|
|
|
28 |
/** |
|
|
29 |
* {@inheritDoc} |
|
|
30 |
*/ |
|
|
31 |
public function setRepeatedPass(RepeatedPass $repeatedPass) |
|
|
32 |
{ |
|
|
33 |
$this->repeatedPass = $repeatedPass; |
|
|
34 |
} |
|
|
35 |
|
|
|
36 |
/** |
|
|
37 |
* Processes the ContainerBuilder to remove unused definitions. |
|
|
38 |
* |
|
|
39 |
* @param ContainerBuilder $container |
|
|
40 |
* @return void |
|
|
41 |
*/ |
|
|
42 |
public function process(ContainerBuilder $container) |
|
|
43 |
{ |
|
|
44 |
$compiler = $container->getCompiler(); |
|
|
45 |
$formatter = $compiler->getLoggingFormatter(); |
|
|
46 |
$graph = $compiler->getServiceReferenceGraph(); |
|
|
47 |
|
|
|
48 |
$hasChanged = false; |
|
|
49 |
foreach ($container->getDefinitions() as $id => $definition) { |
|
|
50 |
if ($definition->isPublic()) { |
|
|
51 |
continue; |
|
|
52 |
} |
|
|
53 |
|
|
|
54 |
if ($graph->hasNode($id)) { |
|
|
55 |
$edges = $graph->getNode($id)->getInEdges(); |
|
|
56 |
$referencingAliases = array(); |
|
|
57 |
$sourceIds = array(); |
|
|
58 |
foreach ($edges as $edge) { |
|
|
59 |
$node = $edge->getSourceNode(); |
|
|
60 |
$sourceIds[] = $node->getId(); |
|
|
61 |
|
|
|
62 |
if ($node->isAlias()) { |
|
|
63 |
$referencingAlias[] = $node->getValue(); |
|
|
64 |
} |
|
|
65 |
} |
|
|
66 |
$isReferenced = (count(array_unique($sourceIds)) - count($referencingAliases)) > 0; |
|
|
67 |
} else { |
|
|
68 |
$referencingAliases = array(); |
|
|
69 |
$isReferenced = false; |
|
|
70 |
} |
|
|
71 |
|
|
|
72 |
if (1 === count($referencingAliases) && false === $isReferenced) { |
|
|
73 |
$container->setDefinition((string) reset($referencingAliases), $definition); |
|
|
74 |
$definition->setPublic(true); |
|
|
75 |
$container->removeDefinition($id); |
|
|
76 |
$compiler->addLogMessage($formatter->formatRemoveService($this, $id, 'replaces alias '.reset($referencingAliases))); |
|
|
77 |
} else if (0 === count($referencingAliases) && false === $isReferenced) { |
|
|
78 |
$container->removeDefinition($id); |
|
|
79 |
$compiler->addLogMessage($formatter->formatRemoveService($this, $id, 'unused')); |
|
|
80 |
$hasChanged = true; |
|
|
81 |
} |
|
|
82 |
} |
|
|
83 |
|
|
|
84 |
if ($hasChanged) { |
|
|
85 |
$this->repeatedPass->setRepeat(); |
|
|
86 |
} |
|
|
87 |
} |
|
|
88 |
} |