1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Lug package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Lug\Bundle\RegistryBundle\DependencyInjection\Compiler; |
13
|
|
|
|
14
|
|
|
use Lug\Bundle\RegistryBundle\Model\LazyRegistry; |
15
|
|
|
use Lug\Bundle\RegistryBundle\Model\LazyRegistryInterface; |
16
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
18
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
19
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author GeLo <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class ConvertRegistryPass implements CompilerPassInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
|
|
public function process(ContainerBuilder $container) |
30
|
|
|
{ |
31
|
|
|
foreach (array_keys($container->findTaggedServiceIds($tag = 'lug.registry')) as $registry) { |
32
|
|
|
$definition = $container->getDefinition($registry); |
33
|
|
|
|
34
|
|
|
if (is_subclass_of($definition->getClass(), LazyRegistryInterface::class)) { |
|
|
|
|
35
|
|
|
continue; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$container->setDefinition($internal = $registry.'.internal', $definition->clearTag($tag)); |
39
|
|
|
$container->setDefinition($registry, $this->createLazyDefinition($internal, $definition)); |
40
|
|
|
|
41
|
|
|
$this->clearMethodCalls($definition); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $registry |
47
|
|
|
* @param Definition $definition |
48
|
|
|
* |
49
|
|
|
* @return Definition |
50
|
|
|
*/ |
51
|
|
|
private function createLazyDefinition($registry, Definition $definition) |
52
|
|
|
{ |
53
|
|
|
$lazy = new Definition(LazyRegistry::class, [ |
54
|
|
|
new Reference('service_container'), |
55
|
|
|
new Reference($registry), |
56
|
|
|
]); |
57
|
|
|
|
58
|
|
|
foreach ($definition->getMethodCalls() as $methodCall) { |
59
|
|
|
if ($methodCall[0] === 'offsetSet') { |
60
|
|
|
$methodCall[1][1] = (string) $methodCall[1][1]; |
61
|
|
|
$lazy->addMethodCall('setLazy', $methodCall[1]); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $lazy; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param Definition $definition |
70
|
|
|
*/ |
71
|
|
|
private function clearMethodCalls(Definition $definition) |
72
|
|
|
{ |
73
|
|
|
$methodCall = 'offsetSet'; |
74
|
|
|
|
75
|
|
|
while ($definition->hasMethodCall($methodCall)) { |
76
|
|
|
$definition->removeMethodCall($methodCall); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|