1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gendoria\CommandQueueBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Gendoria\CommandQueue\QueueManager\NullQueueManager; |
6
|
|
|
use Gendoria\CommandQueue\QueueManager\SingleQueueManager; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Symfony\Component\Config\FileLocator; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
10
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
11
|
|
|
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; |
12
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
13
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* This is the class that loads and manages your bundle configuration. |
17
|
|
|
* |
18
|
|
|
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} |
19
|
|
|
*/ |
20
|
|
|
class GendoriaCommandQueueExtension extends Extension implements PrependExtensionInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Get extension alias. |
24
|
|
|
* |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
11 |
|
public function getAlias() |
28
|
|
|
{ |
29
|
11 |
|
return 'gendoria_command_queue'; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Load extension. |
34
|
|
|
* |
35
|
|
|
* @param array $configs |
36
|
|
|
* @param ContainerBuilder $container |
37
|
|
|
* @throws InvalidArgumentException Thrown, when pool required in configuration is not present. |
38
|
|
|
*/ |
39
|
11 |
|
public function load(array $configs, ContainerBuilder $container) |
40
|
|
|
{ |
41
|
11 |
|
$configuration = new Configuration($this->getAlias()); |
42
|
11 |
|
$config = $this->processConfiguration($configuration, $configs); |
43
|
|
|
|
44
|
10 |
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
45
|
10 |
|
$loader->load('services.yml'); |
46
|
|
|
|
47
|
10 |
|
$container->setParameter('gendoria_command_queue.pools', $config['pools']); |
48
|
10 |
|
if (!$config['enabled']) { |
49
|
1 |
|
$managerDefinition = $container->getDefinition('gendoria_command_queue.manager'); |
50
|
1 |
|
$managerDefinition->setClass(NullQueueManager::class); |
51
|
1 |
|
} |
52
|
10 |
|
$this->setupManagers($config, $container); |
53
|
9 |
|
if (!$config['listeners']['clear_entity_managers']) { |
54
|
2 |
|
$container->removeDefinition('gendoria_command_queue.listener.clear_entity_managers'); |
55
|
2 |
|
} |
56
|
9 |
|
if (!$config['listeners']['clear_logs']) { |
57
|
1 |
|
$container->removeDefinition('gendoria_command_queue.listener.clear_logs'); |
58
|
1 |
|
} |
59
|
9 |
|
} |
60
|
|
|
|
61
|
10 |
|
private function setupManagers($config, ContainerBuilder $container) |
62
|
|
|
{ |
63
|
|
|
//Create simple managers for each pool and tag multiple manager to receive all pools |
64
|
10 |
|
$managerDefinition = $container->getDefinition('gendoria_command_queue.manager'); |
65
|
10 |
|
$poolNames = array_keys($config['pools']); |
66
|
10 |
|
foreach ($config['pools'] as $poolName => $poolConfig) { |
67
|
9 |
|
$definition = new Definition(SingleQueueManager::class); |
68
|
9 |
|
$definition->addTag('gendoria_command_queue.send_manager', array('pool' => $poolName)); |
69
|
9 |
|
$container->setDefinition('gendoria_command_queue.manager.'.$poolName, $definition); |
70
|
10 |
|
$managerDefinition->addTag('gendoria_command_queue.send_manager', array('pool' => $poolName, 'default' => $poolName == 'default')); |
71
|
10 |
|
} |
72
|
|
|
//Inject command routing to default manager |
73
|
10 |
|
foreach ($config['routes'] as $commandExpression => $poolName) { |
74
|
2 |
|
if (!in_array($poolName, $poolNames)) { |
75
|
1 |
|
throw new InvalidArgumentException(sprintf("Pool \"%s\" required in command routing is not present.", $poolName)); |
76
|
|
|
} |
77
|
1 |
|
$managerDefinition->addMethodCall('addCommandRoute', array($commandExpression, $poolName)); |
78
|
9 |
|
} |
79
|
9 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Prepend configuration. |
83
|
|
|
* |
84
|
|
|
* @param ContainerBuilder $container |
85
|
|
|
*/ |
86
|
2 |
|
public function prepend(ContainerBuilder $container) |
87
|
|
|
{ |
88
|
2 |
|
$this->disableDoctrineListener($container); |
89
|
2 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Disable clear entity managers listener, if no doctrine bundle is installed. |
93
|
|
|
* |
94
|
|
|
* @param ContainerBuilder $container |
95
|
|
|
* @return void |
96
|
|
|
*/ |
97
|
2 |
|
private function disableDoctrineListener(ContainerBuilder $container) |
98
|
|
|
{ |
99
|
2 |
|
if (!$container->hasParameter('kernel.bundles')) { |
100
|
1 |
|
return; |
101
|
|
|
} |
102
|
1 |
|
$bundles = $container->getParameter('kernel.bundles'); |
103
|
1 |
|
if (!isset($bundles['DoctrineBundle'])) { |
104
|
1 |
|
$container->prependExtensionConfig($this->getAlias(), array( |
105
|
|
|
'listeners' => array( |
106
|
|
|
'clear_entity_managers' => false |
107
|
1 |
|
) |
108
|
1 |
|
)); |
109
|
1 |
|
} |
110
|
1 |
|
} |
111
|
|
|
} |
112
|
|
|
|