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\Loader; |
12
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* This is the class that loads and manages your bundle configuration. |
16
|
|
|
* |
17
|
|
|
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} |
18
|
|
|
*/ |
19
|
|
|
class GendoriaCommandQueueExtension extends Extension |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Get extension alias. |
23
|
|
|
* |
24
|
|
|
* @return string |
25
|
|
|
*/ |
26
|
7 |
|
public function getAlias() |
27
|
|
|
{ |
28
|
7 |
|
return 'gendoria_command_queue'; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Load extension. |
33
|
|
|
* |
34
|
|
|
* @param array $configs |
35
|
|
|
* @param ContainerBuilder $container |
36
|
|
|
* @throws InvalidArgumentException Thrown, when pool required in configuration is not present. |
37
|
|
|
*/ |
38
|
7 |
|
public function load(array $configs, ContainerBuilder $container) |
39
|
|
|
{ |
40
|
7 |
|
$configuration = new Configuration($this->getAlias()); |
41
|
7 |
|
$config = $this->processConfiguration($configuration, $configs); |
42
|
|
|
|
43
|
6 |
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
44
|
6 |
|
$loader->load('services.yml'); |
45
|
|
|
|
46
|
6 |
|
$container->setParameter('gendoria_command_queue.pools', $config['pools']); |
47
|
6 |
|
if (!$config['enabled']) { |
48
|
1 |
|
$managerDefinition = $container->getDefinition('gendoria_command_queue.manager'); |
49
|
1 |
|
$managerDefinition->setClass(NullQueueManager::class); |
50
|
1 |
|
} |
51
|
6 |
|
$this->setupManagers($config, $container); |
52
|
5 |
|
} |
53
|
|
|
|
54
|
6 |
|
private function setupManagers($config, ContainerBuilder $container) |
55
|
|
|
{ |
56
|
|
|
//Create simple managers for each pool and tag multiple manager to receive all pools |
57
|
6 |
|
$managerDefinition = $container->getDefinition('gendoria_command_queue.manager'); |
58
|
6 |
|
$poolNames = array_keys($config['pools']); |
59
|
6 |
|
foreach ($config['pools'] as $poolName => $poolConfig) { |
60
|
5 |
|
$definition = new Definition(SingleQueueManager::class); |
61
|
5 |
|
$definition->addTag('gendoria_command_queue.send_manager', array('pool' => $poolName)); |
62
|
5 |
|
$container->setDefinition('gendoria_command_queue.manager.'.$poolName, $definition); |
63
|
5 |
|
$managerDefinition->addTag('gendoria_command_queue.send_manager', array('pool' => $poolName, 'default' => $poolName == 'default')); |
64
|
6 |
|
} |
65
|
|
|
//Inject command routing to default manager |
66
|
6 |
|
foreach ($config['routes'] as $commandExpression => $poolName) { |
67
|
2 |
|
if (!in_array($poolName, $poolNames)) { |
68
|
1 |
|
throw new InvalidArgumentException(sprintf("Pool \"%s\" required in command routing is not present.", $poolName)); |
69
|
|
|
} |
70
|
2 |
|
$managerDefinition->addMethodCall('addCommandRoute', array($commandExpression, $poolName)); |
71
|
5 |
|
} |
72
|
5 |
|
} |
73
|
|
|
} |
74
|
|
|
|