|
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
|
|
|
|