|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Innmind\AMQPBundle\DependencyInjection; |
|
5
|
|
|
|
|
6
|
|
|
use Innmind\AMQPBundle\Producer\Producer; |
|
7
|
|
|
use Symfony\Component\{ |
|
8
|
|
|
HttpKernel\DependencyInjection\Extension, |
|
9
|
|
|
DependencyInjection\ContainerBuilder, |
|
10
|
|
|
DependencyInjection\Loader, |
|
11
|
|
|
DependencyInjection\Reference, |
|
12
|
|
|
DependencyInjection\Definition, |
|
13
|
|
|
Config\FileLocator |
|
14
|
|
|
}; |
|
15
|
|
|
|
|
16
|
|
|
final class InnmindAMQPExtension extends Extension |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* {@inheritdoc} |
|
20
|
|
|
*/ |
|
21
|
16 |
|
public function load(array $configs, ContainerBuilder $container) |
|
22
|
|
|
{ |
|
23
|
16 |
|
$loader = new Loader\YamlFileLoader( |
|
24
|
16 |
|
$container, |
|
25
|
16 |
|
new FileLocator(__DIR__.'/../Resources/config') |
|
26
|
|
|
); |
|
27
|
16 |
|
$loader->load('services.yml'); |
|
28
|
16 |
|
$config = $this->processConfiguration( |
|
29
|
16 |
|
new Configuration, |
|
30
|
16 |
|
$configs |
|
31
|
|
|
); |
|
32
|
|
|
|
|
33
|
|
|
$this |
|
34
|
16 |
|
->configureConnection($config, $container) |
|
35
|
16 |
|
->registerTranslators($config, $container) |
|
36
|
16 |
|
->registerExchanges($config, $container) |
|
37
|
16 |
|
->registerQueues($config, $container) |
|
38
|
16 |
|
->registerBindings($config, $container) |
|
39
|
16 |
|
->registerProducers($config, $container) |
|
40
|
16 |
|
->registerSignalAwareClient($config, $container); |
|
41
|
16 |
|
} |
|
42
|
|
|
|
|
43
|
16 |
|
private function configureConnection( |
|
44
|
|
|
array $config, |
|
45
|
|
|
ContainerBuilder $container |
|
46
|
|
|
): self { |
|
47
|
|
|
$container |
|
48
|
16 |
|
->getDefinition('innmind.amqp.connection.default') |
|
49
|
16 |
|
->replaceArgument(0, $config['server']['transport']['name']) |
|
50
|
16 |
|
->replaceArgument(1, $config['server']) |
|
51
|
16 |
|
->replaceArgument(3, $config['server']['timeout']) |
|
52
|
16 |
|
->replaceArgument(4, new Reference($config['clock'])); |
|
53
|
|
|
|
|
54
|
16 |
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
16 |
|
private function registerTranslators( |
|
58
|
|
|
array $config, |
|
59
|
|
|
ContainerBuilder $container |
|
60
|
|
|
): self { |
|
61
|
16 |
|
$definition = $container->getDefinition('innmind.amqp.argument_translator'); |
|
62
|
|
|
|
|
63
|
16 |
|
foreach ($config['argument_translators'] as $translator) { |
|
64
|
|
|
$definition->addArgument(new Reference($translator)); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
16 |
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
16 |
View Code Duplication |
private function registerExchanges( |
|
71
|
|
|
array $config, |
|
72
|
|
|
ContainerBuilder $container |
|
73
|
|
|
): self { |
|
74
|
16 |
|
$autoDeclare = $container->getDefinition('innmind.amqp.client.auto_declare'); |
|
75
|
|
|
|
|
76
|
16 |
|
foreach ($config['exchanges'] as $name => $exchange) { |
|
77
|
10 |
|
$autoDeclare->addMethodCall( |
|
78
|
10 |
|
'declareExchange', |
|
79
|
10 |
|
[$name, $exchange['type'], $exchange['durable'], $exchange['arguments']] |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
16 |
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
16 |
|
private function registerQueues( |
|
87
|
|
|
array $config, |
|
88
|
|
|
ContainerBuilder $container |
|
89
|
|
|
): self { |
|
90
|
16 |
|
$autoDeclare = $container->getDefinition('innmind.amqp.client.auto_declare'); |
|
91
|
16 |
|
$consumers = $container->getDefinition('innmind.amqp.consumers'); |
|
92
|
|
|
|
|
93
|
16 |
|
foreach ($config['queues'] as $name => $queue) { |
|
94
|
12 |
|
$autoDeclare->addMethodCall( |
|
95
|
12 |
|
'declareQueue', |
|
96
|
12 |
|
[$name, $queue['durable'], $queue['exclusive'], $queue['arguments']] |
|
97
|
|
|
); |
|
98
|
12 |
|
$consumers->addMethodCall( |
|
99
|
12 |
|
'add', |
|
100
|
12 |
|
[$name, new Reference($queue['consumer'])] |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
16 |
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
16 |
View Code Duplication |
private function registerBindings( |
|
108
|
|
|
array $config, |
|
109
|
|
|
ContainerBuilder $container |
|
110
|
|
|
): self { |
|
111
|
16 |
|
$autoDeclare = $container->getDefinition('innmind.amqp.client.auto_declare'); |
|
112
|
|
|
|
|
113
|
16 |
|
foreach ($config['bindings'] as $binding) { |
|
114
|
10 |
|
$autoDeclare->addMethodCall( |
|
115
|
10 |
|
'declareBinding', |
|
116
|
10 |
|
[$binding['exchange'], $binding['queue'], $binding['routingKey'], $binding['arguments']] |
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
16 |
|
return $this; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
16 |
|
private function registerProducers( |
|
124
|
|
|
array $config, |
|
125
|
|
|
ContainerBuilder $container |
|
126
|
|
|
): self { |
|
127
|
16 |
|
foreach ($config['exchanges'] as $name => $_) { |
|
128
|
10 |
|
$container->setDefinition( |
|
129
|
10 |
|
'innmind.amqp.producer.'.$name, |
|
130
|
10 |
|
new Definition( |
|
131
|
10 |
|
Producer::class, |
|
132
|
|
|
[ |
|
133
|
10 |
|
new Reference('innmind.amqp.client'), |
|
134
|
10 |
|
$name |
|
135
|
|
|
] |
|
136
|
|
|
) |
|
137
|
|
|
); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
16 |
|
return $this; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
16 |
|
private function registerSignalAwareClient( |
|
144
|
|
|
array $config, |
|
145
|
|
|
ContainerBuilder $container |
|
146
|
|
|
): self { |
|
147
|
16 |
|
if ($config['handle_posix_signals'] === true) { |
|
148
|
14 |
|
$container->setAlias( |
|
149
|
14 |
|
'innmind.amqp.client', |
|
150
|
14 |
|
'innmind.amqp.client.signal_aware' |
|
151
|
|
|
); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
16 |
|
return $this; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|