Completed
Push — master ( 38f1ea...fd6c7c )
by Matze
04:05
created

GlobalCompilerPass   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 8
Bugs 0 Features 1
Metric Value
wmc 6
c 8
b 0
f 1
lcom 0
cbo 2
dl 0
loc 67
ccs 27
cts 27
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 20 2
A loadCompilerPasses() 0 13 2
A logResult() 0 14 2
1
<?php
2
3
namespace BrainExe\Core\DependencyInjection\CompilerPass;
4
5
use Monolog\Logger;
6
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
9
class GlobalCompilerPass implements CompilerPassInterface
10
{
11
12
    const TAG = 'compiler_pass';
13
14
    /**
15
     * {@inheritdoc}
16
     */
17 3
    public function process(ContainerBuilder $container)
18
    {
19 3
        $container->setParameter('application.root', ROOT);
20
21 3
        $servicePriorities = $this->loadCompilerPasses($container);
22
23
        /** @var Logger $logger */
24 3
        $totalTime = 0;
25 3
        $loggerStore = [];
26 3
        foreach (array_keys($servicePriorities) as $serviceId) {
27 3
            $startTime = microtime(true);
28
29 3
            $container->get($serviceId)->process($container);
30
31 3
            $totalTime += $diff = microtime(true) - $startTime;
32 3
            $loggerStore[] = sprintf('DIC: %0.2fms %s', $diff * 1000, $serviceId);
33
        }
34
35 3
        $this->logResult($container, $loggerStore, $totalTime);
36 3
    }
37
38
    /**
39
     * @param ContainerBuilder $container
40
     * @return array
41
     */
42 3
    private function loadCompilerPasses(ContainerBuilder $container)
43
    {
44 3
        $serviceIds        = $container->findTaggedServiceIds(self::TAG);
45 3
        $servicePriorities = [];
46
47 3
        foreach ($serviceIds as $serviceId => $tag) {
48 3
            $servicePriorities[$serviceId] = $tag[0]['priority'];
49
        }
50
51 3
        arsort($servicePriorities);
52
53 3
        return $servicePriorities;
54
    }
55
56
    /**
57
     * @param ContainerBuilder $container
58
     * @param array $loggerStore
59
     * @param float $totalTime
60
     */
61 3
    private function logResult(ContainerBuilder $container, array $loggerStore, float $totalTime)
62
    {
63 3
        $container->reset();
64
65
        /** @var Logger $logger */
66 3
        $logger = $container->get('logger');
67 3
        $logger->debug('DIC: start', ['channel' => 'dic']);
68
69 3
        foreach ($loggerStore as $log) {
70 3
            $logger->debug($log, ['channel' => 'dic']);
71
        }
72
73 3
        $logger->debug(sprintf('DIC: %0.2fms total time', $totalTime * 1000), ['channel' => 'dic']);
74 3
    }
75
}
76