|
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\ContainerBuilder; |
|
|
15 |
use Symfony\Component\DependencyInjection\Reference; |
|
|
16 |
|
|
|
17 |
/** |
|
|
18 |
* Replaces aliases with actual service definitions, effectively removing these |
|
|
19 |
* aliases. |
|
|
20 |
* |
|
|
21 |
* @author Johannes M. Schmitt <schmittjoh@gmail.com> |
|
|
22 |
*/ |
|
|
23 |
class ReplaceAliasByActualDefinitionPass implements CompilerPassInterface |
|
|
24 |
{ |
|
|
25 |
private $compiler; |
|
|
26 |
private $formatter; |
|
|
27 |
private $sourceId; |
|
|
28 |
|
|
|
29 |
/** |
|
|
30 |
* Process the Container to replace aliases with service definitions. |
|
|
31 |
* |
|
|
32 |
* @param ContainerBuilder $container |
|
|
33 |
*/ |
|
|
34 |
public function process(ContainerBuilder $container) |
|
|
35 |
{ |
|
|
36 |
$this->compiler = $container->getCompiler(); |
|
|
37 |
$this->formatter = $this->compiler->getLoggingFormatter(); |
|
|
38 |
|
|
|
39 |
foreach ($container->getAliases() as $id => $alias) { |
|
|
40 |
$aliasId = (string) $alias; |
|
|
41 |
|
|
|
42 |
$definition = $container->getDefinition($aliasId); |
|
|
43 |
|
|
|
44 |
if ($definition->isPublic()) { |
|
|
45 |
continue; |
|
|
46 |
} |
|
|
47 |
|
|
|
48 |
$definition->setPublic(true); |
|
|
49 |
$container->setDefinition($id, $definition); |
|
|
50 |
$container->removeDefinition($aliasId); |
|
|
51 |
|
|
|
52 |
$this->updateReferences($container, $aliasId, $id); |
|
|
53 |
|
|
|
54 |
// we have to restart the process due to concurrent modification of |
|
|
55 |
// the container |
|
|
56 |
$this->process($container); |
|
|
57 |
|
|
|
58 |
break; |
|
|
59 |
} |
|
|
60 |
} |
|
|
61 |
|
|
|
62 |
/** |
|
|
63 |
* Updates references to remove aliases. |
|
|
64 |
* |
|
|
65 |
* @param ContainerBuilder $container The container |
|
|
66 |
* @param string $currentId The alias identifier being replaced |
|
|
67 |
* @param string $newId The id of the service the alias points to |
|
|
68 |
*/ |
|
|
69 |
private function updateReferences($container, $currentId, $newId) |
|
|
70 |
{ |
|
|
71 |
foreach ($container->getAliases() as $id => $alias) { |
|
|
72 |
if ($currentId === (string) $alias) { |
|
|
73 |
$container->setAlias($id, $newId); |
|
|
74 |
} |
|
|
75 |
} |
|
|
76 |
|
|
|
77 |
foreach ($container->getDefinitions() as $id => $definition) { |
|
|
78 |
$this->sourceId = $id; |
|
|
79 |
|
|
|
80 |
$definition->setArguments( |
|
|
81 |
$this->updateArgumentReferences($definition->getArguments(), $currentId, $newId) |
|
|
82 |
); |
|
|
83 |
|
|
|
84 |
$definition->setMethodCalls( |
|
|
85 |
$this->updateArgumentReferences($definition->getMethodCalls(), $currentId, $newId) |
|
|
86 |
); |
|
|
87 |
|
|
|
88 |
$definition->setProperties( |
|
|
89 |
$this->updateArgumentReferences($definition->getProperties(), $currentId, $newId) |
|
|
90 |
); |
|
|
91 |
} |
|
|
92 |
} |
|
|
93 |
|
|
|
94 |
/** |
|
|
95 |
* Updates argument references. |
|
|
96 |
* |
|
|
97 |
* @param array $arguments An array of Arguments |
|
|
98 |
* @param string $currentId The alias identifier |
|
|
99 |
* @param string $newId The identifier the alias points to |
|
|
100 |
*/ |
|
|
101 |
private function updateArgumentReferences(array $arguments, $currentId, $newId) |
|
|
102 |
{ |
|
|
103 |
foreach ($arguments as $k => $argument) { |
|
|
104 |
if (is_array($argument)) { |
|
|
105 |
$arguments[$k] = $this->updateArgumentReferences($argument, $currentId, $newId); |
|
|
106 |
} else if ($argument instanceof Reference) { |
|
|
107 |
if ($currentId === (string) $argument) { |
|
|
108 |
$arguments[$k] = new Reference($newId, $argument->getInvalidBehavior()); |
|
|
109 |
$this->compiler->addLogMessage($this->formatter->formatUpdateReference($this, $this->sourceId, $currentId, $newId)); |
|
|
110 |
} |
|
|
111 |
} |
|
|
112 |
} |
|
|
113 |
|
|
|
114 |
return $arguments; |
|
|
115 |
} |
|
|
116 |
} |