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