SvgTraceableLoaderPass::process()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 11
ccs 0
cts 6
cp 0
crap 12
rs 10
1
<?php
2
3
/*
4
 * This file is part of ocubom/twig-extra-bundle
5
 *
6
 * © Oscar Cubo Medina <https://ocubom.github.io>
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 Ocubom\TwigExtraBundle\DependencyInjection\Compiler;
13
14
use Ocubom\Twig\Extension\Svg\Loader\LoaderInterface;
15
use Ocubom\TwigExtraBundle\DataCollector\SvgTraceableLoader;
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
class SvgTraceableLoaderPass implements CompilerPassInterface
22
{
23
    public function process(ContainerBuilder $container): void
24
    {
25
        if (!$container->hasDefinition('data_collector.svg')) {
26
            return;
27
        }
28
29
        $this->addLoaderToCollector('ocubom_twig_extra.svg_loader', $container);
30
31
        /* @var LoaderInterface $loader */
32
        foreach ($container->findTaggedServiceIds('ocubom_twig_extra.svg_loader') as $ident => $tags) {
33
            $this->addLoaderToCollector($ident, $container);
34
        }
35
    }
36
37
    private function addLoaderToCollector(string $ident, ContainerBuilder $container): void
38
    {
39
        /** @var class-string $class */
40
        $class = $container->getDefinition($ident)->getClass();
41
        $class = new \ReflectionClass($class);
42
43
        // Convert to traceable loaders
44
        if (!$class->isSubclassOf(SvgTraceableLoader::class)) {
45
            $old = $container->getDefinition($ident);
46
            if ($old->isAbstract()) {
47
                return;
48
            }
49
50
            $new = new Definition(SvgTraceableLoader::class);
51
            $new->setTags($old->getTags());
52
            if (!$old->isPublic() || !$old->isPrivate()) {
53
                $new->setPublic($old->isPublic());
54
            }
55
            $new->setArguments([new Reference($newId = '.'.$ident.'.inner')]);
56
57
            foreach ($old->getMethodCalls() as [$call, $args]) {
58
                if (
59
                    'setCallbackWrapper' !== $call
60
                    || !$args[0] instanceof Definition
61
                    || !($args[0]->getArguments()[2] ?? null) instanceof Definition
62
                ) {
63
                    continue;
64
                }
65
                if ([new Reference($ident), 'setCallbackWrapper'] == $args[0]->getArguments()[2]->getFactory()) {
66
                    $args[0]->getArguments()[2]->setFactory([new Reference($newId), 'setCallbackWrapper']);
67
                }
68
            }
69
70
            $old->setTags([]);
71
            $old->setPublic(false);
72
73
            $new->setAutowired(true);
74
            $new->setAutoconfigured(true);
75
            $new->addTag('monolog.logger', ['channel' => 'twig_svg']);
76
77
            $container->setDefinition($newId, $old);
78
            $container->setDefinition($ident, $new);
79
        }
80
81
        // Tell the collector to add the new instance
82
        $collector = $container->getDefinition('data_collector.svg');
83
        $collector->addMethodCall('addLoader', [$ident, new Reference($ident)]);
84
        $collector->setPublic(false);
85
    }
86
}
87