1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AndrewCarterUK\NoMoreLeaksBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
6
|
|
|
use Symfony\Component\Config\FileLocator; |
7
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
8
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
9
|
|
|
use Symfony\Component\DependencyInjection\DefinitionDecorator; |
10
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
11
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
12
|
|
|
|
13
|
|
|
class NoMoreLeaksExtension extends Extension |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* {@inheritdoc} |
17
|
|
|
*/ |
18
|
1 |
|
public function load(array $configs, ContainerBuilder $container) |
19
|
|
|
{ |
20
|
1 |
|
$configuration = new Configuration(); |
21
|
1 |
|
$config = $this->processConfiguration($configuration, $configs); |
22
|
|
|
|
23
|
1 |
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
24
|
1 |
|
$loader->load('services.yml'); |
25
|
|
|
|
26
|
1 |
|
if (!empty($config)) { |
27
|
1 |
|
$this->process($container, $config); |
28
|
1 |
|
} |
29
|
1 |
|
} |
30
|
|
|
|
31
|
1 |
|
private function process(ContainerBuilder $container, array $config) |
32
|
|
|
{ |
33
|
1 |
|
$listenerDefinition = $container->getDefinition('no_more_leaks.listener.terminate'); |
34
|
|
|
|
35
|
1 |
|
if (!empty($config['doctrine'])) { |
36
|
1 |
|
$this->processDoctrine($container, $listenerDefinition, $config['doctrine']); |
37
|
1 |
|
} |
38
|
|
|
|
39
|
1 |
|
if (!empty($config['monolog'])) { |
40
|
1 |
|
$this->processMonolog($container, $listenerDefinition, $config['monolog']); |
41
|
1 |
|
} |
42
|
1 |
|
} |
43
|
|
|
|
44
|
1 |
|
private function processDoctrine(ContainerBuilder $container, Definition $listenerDefinition, array $config) |
45
|
|
|
{ |
46
|
1 |
|
if (!$config['enabled']) { |
47
|
|
|
return; |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
if (empty($config['managers'])) { |
51
|
1 |
|
$config['managers'] = array('default'); |
52
|
1 |
|
} |
53
|
|
|
|
54
|
1 |
|
foreach ($config['managers'] as $manager) { |
55
|
1 |
|
$managerPrototype = new DefinitionDecorator('no_more_leaks.resetter.doctrine_entity_manager_resetter_prototype'); |
56
|
1 |
|
$managerPrototype->replaceArgument(0, new Reference('doctrine.orm.' . $manager . '_entity_manager')); |
57
|
1 |
|
$serviceId = 'no_more_leaks.resetter.doctrine.entity_manager.' . $manager; |
58
|
1 |
|
$container->setDefinition($serviceId, $managerPrototype); |
59
|
1 |
|
$this->addResetter($listenerDefinition, $serviceId); |
60
|
1 |
|
} |
61
|
1 |
|
} |
62
|
|
|
|
63
|
1 |
|
private function processMonolog(ContainerBuilder $container, Definition $listenerDefinition, array $config) |
64
|
|
|
{ |
65
|
1 |
|
if (!$config['enabled']) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
if (empty($config['channels'])) { |
70
|
1 |
|
$config['channels'] = array('app'); |
71
|
1 |
|
} |
72
|
|
|
|
73
|
1 |
|
foreach ($config['channels'] as $channel) { |
74
|
1 |
|
$monologPrototype = new DefinitionDecorator('no_more_leaks.resetter.monolog_prototype'); |
75
|
|
|
|
76
|
1 |
|
if ('app' === $channel) { |
77
|
1 |
|
$serviceId = 'no_more_leaks.resetter.monolog'; |
78
|
1 |
|
} else { |
79
|
|
|
$monologPrototype->replaceArgument(0, new Reference('monolog.logger.' . $channel)); |
80
|
|
|
$serviceId = 'no_more_leaks.resetter.monolog.' . $channel; |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
$container->setDefinition($serviceId, $monologPrototype); |
84
|
1 |
|
$this->addResetter($listenerDefinition, $serviceId); |
85
|
1 |
|
} |
86
|
1 |
|
} |
87
|
|
|
|
88
|
1 |
|
private function addResetter(Definition $listenerDefinition, $resetterId) |
89
|
|
|
{ |
90
|
1 |
|
$listenerDefinition->addMethodCall('addResetter', array(new Reference($resetterId))); |
91
|
1 |
|
} |
92
|
|
|
} |
93
|
|
|
|