|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MyOnlineStore\Bundle\RabbitMqManagerBundle\DependencyInjection; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
|
6
|
|
|
use Symfony\Component\Config\FileLocator; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
9
|
|
|
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; |
|
10
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
|
11
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
12
|
|
|
|
|
13
|
|
|
class RabbitMqManagerExtension extends Extension implements PrependExtensionInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @inheritdoc |
|
17
|
|
|
*/ |
|
18
|
4 |
|
public function load(array $configs, ContainerBuilder $container) |
|
19
|
|
|
{ |
|
20
|
4 |
|
$configuration = new Configuration(); |
|
21
|
4 |
|
$config = $this->processConfiguration($configuration, $configs); |
|
22
|
|
|
|
|
23
|
4 |
|
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
24
|
4 |
|
$loader->load('services.yml'); |
|
25
|
|
|
|
|
26
|
|
|
$configuration = [ |
|
27
|
4 |
|
'path' => $config['path'], |
|
28
|
|
|
'consumers' => [], |
|
29
|
|
|
'rpc_servers' => [], |
|
30
|
|
|
]; |
|
31
|
|
|
|
|
32
|
4 |
|
$rabbitMqBundleConfiguration = $container->getParameter('mos_old_sound_rabbit_mq.config'); |
|
33
|
|
|
|
|
34
|
4 |
|
foreach (['consumers', 'rpc_servers'] as $type) { |
|
35
|
4 |
|
if (isset($rabbitMqBundleConfiguration[$type])) { |
|
36
|
3 |
|
foreach ($rabbitMqBundleConfiguration[$type] as $name => $consumer) { |
|
37
|
3 |
|
$consumerConfig = isset($config[$type]['individual'][$name]) ? |
|
38
|
|
|
$config[$type]['individual'][$name] : |
|
39
|
3 |
|
$config[$type]['general']; |
|
40
|
|
|
|
|
41
|
3 |
|
$command = $config['commands'][$type]; |
|
42
|
|
|
|
|
43
|
3 |
|
if ('consumers' === $type && 'cli-consumer' === $consumerConfig['processor']) { |
|
44
|
|
|
$command = $config['commands']['cli_consumer_invoker']; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
4 |
|
$configuration[$type][$name] = $this->generateConfiguration( |
|
48
|
|
|
$container, |
|
49
|
|
|
$name, |
|
50
|
|
|
$command, |
|
51
|
|
|
$consumerConfig, |
|
52
|
|
|
$consumer |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
3 |
|
$container->setParameter('mos_rabbitmq_cli_consumer.config', $configuration); |
|
59
|
3 |
|
$container->setParameter('mos_rabbitmq_cli_consumer.path', $configuration['path']); |
|
60
|
3 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @inheritdoc |
|
64
|
|
|
*/ |
|
65
|
2 |
|
public function prepend(ContainerBuilder $container) |
|
66
|
|
|
{ |
|
67
|
2 |
|
foreach ($container->getExtensions() as $name => $extension) { |
|
68
|
|
|
switch ($name) { |
|
69
|
2 |
|
case 'old_sound_rabbit_mq': |
|
70
|
|
|
// take over this bundle's configuration |
|
71
|
1 |
|
$extensionConfig = $container->getExtensionConfig($name); |
|
72
|
|
|
|
|
73
|
1 |
|
$container->setParameter('mos_old_sound_rabbit_mq.config', $extensionConfig[0]); |
|
74
|
2 |
|
break; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
2 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param ContainerInterface $container |
|
81
|
|
|
* @param string $name |
|
82
|
|
|
* @param string $command |
|
83
|
|
|
* @param array $config |
|
84
|
|
|
* @param array $consumer |
|
85
|
|
|
* |
|
86
|
|
|
* @return array |
|
87
|
|
|
*/ |
|
88
|
3 |
|
private function generateConfiguration(ContainerInterface $container, $name, $command, array $config, array $consumer) |
|
89
|
|
|
{ |
|
90
|
3 |
|
$connection = $this->getConnection($container, $consumer); |
|
91
|
|
|
|
|
92
|
|
|
return [ |
|
93
|
2 |
|
'name' => $name, |
|
94
|
2 |
|
'processor' => $config['processor'], |
|
95
|
2 |
|
'messages' => $config['messages'], |
|
96
|
2 |
|
'callback' => $consumer['callback'], |
|
97
|
|
|
'connection' => [ |
|
98
|
2 |
|
'host' => $connection['host'], |
|
99
|
2 |
|
'port' => $connection['port'], |
|
100
|
2 |
|
'user' => $connection['user'], |
|
101
|
2 |
|
'password' => $connection['password'], |
|
102
|
2 |
|
'vhost' => $connection['vhost'], |
|
103
|
|
|
], |
|
104
|
|
|
'supervisor' => [ |
|
105
|
2 |
|
'count' => $config['worker']['count'], |
|
106
|
2 |
|
'startsecs' => $config['worker']['startsecs'], |
|
107
|
2 |
|
'autorestart' => $config['worker']['autorestart'], |
|
108
|
2 |
|
'stopsignal' => $config['worker']['stopsignal'], |
|
109
|
2 |
|
'stopwaitsecs' => $config['worker']['stopwaitsecs'], |
|
110
|
|
|
], |
|
111
|
|
|
'command' => [ // todo make this configurable at some point. as for now it's not important... |
|
112
|
2 |
|
'console' => '%kernel.root_dir%/../bin/console', |
|
113
|
2 |
|
'command' => isset($config['command']['command']) ? $config['command']['command'] : $command, |
|
114
|
|
|
'arguments' => [ |
|
115
|
|
|
'--env=%kernel.environment%', |
|
116
|
|
|
'--app=%kernel.name%', |
|
117
|
|
|
], |
|
118
|
|
|
], |
|
119
|
|
|
'worker' => [ |
|
120
|
2 |
|
'compression' => $config['compression'], |
|
121
|
|
|
'prefetch' => [ |
|
122
|
2 |
|
'count' => isset($consumer['qos_options']['prefetch_count']) ? $consumer['qos_options']['prefetch_count'] : 0, |
|
123
|
2 |
|
'global' => isset($consumer['qos_options']['global']) ? $consumer['qos_options']['global'] : false, |
|
124
|
|
|
], |
|
125
|
|
|
'exchange' => [ |
|
126
|
2 |
|
'name' => $consumer['exchange_options']['name'], |
|
127
|
2 |
|
'type' => $consumer['exchange_options']['type'], |
|
128
|
2 |
|
'autodelete' => isset($consumer['exchange_options']['auto_delete']) ? $consumer['exchange_options']['auto_delete'] : false, |
|
129
|
2 |
|
'durable' => isset($consumer['exchange_options']['durable']) ? $consumer['exchange_options']['durable'] : true, |
|
130
|
|
|
], |
|
131
|
|
|
'queue' => [ |
|
132
|
2 |
|
'name' => $consumer['queue_options']['name'], |
|
133
|
2 |
|
'routing' => isset($consumer['queue_options']['routing_keys']) ? $consumer['queue_options']['routing_keys'] : null, |
|
134
|
|
|
], |
|
135
|
|
|
], |
|
136
|
|
|
]; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param ContainerInterface $container |
|
141
|
|
|
* @param array $consumer |
|
142
|
|
|
* |
|
143
|
|
|
* @return array |
|
144
|
|
|
* |
|
145
|
|
|
* @throws InvalidConfigurationException |
|
146
|
|
|
*/ |
|
147
|
3 |
|
private function getConnection(ContainerInterface $container, array $consumer) |
|
148
|
|
|
{ |
|
149
|
3 |
|
$connections = $container->getParameter('mos_old_sound_rabbit_mq.config')['connections']; |
|
150
|
3 |
|
$name = isset($consumer['connection']) ? $consumer['connection'] : 'default'; |
|
151
|
|
|
|
|
152
|
3 |
|
if (isset($connections[$name])) { |
|
153
|
2 |
|
return $connections[$name]; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
1 |
|
throw new InvalidConfigurationException(sprintf( |
|
157
|
1 |
|
'Connection "%s" does not exist. Available connections: %s', |
|
158
|
|
|
$name, |
|
159
|
1 |
|
implode(', ', array_keys($connections)) |
|
160
|
|
|
)); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|