Completed
Pull Request — master (#18)
by Eric
33:48
created

ConvertRegistryPass::process()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
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)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Lug\Bundle\RegistryBund...egistryInterface::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
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