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
|
6 |
|
public function load(array $configs, ContainerBuilder $container) |
22
|
|
|
{ |
23
|
6 |
|
$loader = new Loader\YamlFileLoader( |
24
|
6 |
|
$container, |
25
|
6 |
|
new FileLocator(__DIR__.'/../Resources/config') |
26
|
|
|
); |
27
|
6 |
|
$loader->load('services.yml'); |
28
|
6 |
|
$config = $this->processConfiguration( |
29
|
6 |
|
new Configuration, |
30
|
6 |
|
$configs |
31
|
|
|
); |
32
|
|
|
|
33
|
|
|
$this |
34
|
6 |
|
->configureConnection($config, $container) |
35
|
6 |
|
->registerTranslators($config, $container) |
36
|
6 |
|
->registerExchanges($config, $container) |
37
|
6 |
|
->registerQueues($config, $container) |
38
|
6 |
|
->registerBindings($config, $container) |
39
|
6 |
|
->registerProducers($config, $container); |
40
|
6 |
|
} |
41
|
|
|
|
42
|
6 |
|
private function configureConnection( |
43
|
|
|
array $config, |
44
|
|
|
ContainerBuilder $container |
45
|
|
|
): self { |
46
|
|
|
$container |
47
|
6 |
|
->getDefinition('innmind.amqp.connection.default') |
48
|
6 |
|
->replaceArgument(0, $config['server']['transport']['name']) |
49
|
6 |
|
->replaceArgument(1, $config['server']) |
50
|
6 |
|
->replaceArgument(3, $config['server']['timeout']) |
51
|
6 |
|
->replaceArgument(4, new Reference($config['clock'])); |
52
|
|
|
|
53
|
6 |
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
6 |
|
private function registerTranslators( |
57
|
|
|
array $config, |
58
|
|
|
ContainerBuilder $container |
59
|
|
|
): self { |
60
|
6 |
|
$definition = $container->getDefinition('innmind.amqp.argument_translator'); |
61
|
|
|
|
62
|
6 |
|
foreach ($config['argument_translators'] as $translator) { |
63
|
|
|
$definition->addArgument(new Reference($translator)); |
64
|
|
|
} |
65
|
|
|
|
66
|
6 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
6 |
View Code Duplication |
private function registerExchanges( |
70
|
|
|
array $config, |
71
|
|
|
ContainerBuilder $container |
72
|
|
|
): self { |
73
|
6 |
|
$autoDeclare = $container->getDefinition('innmind.amqp.client.auto_declare'); |
74
|
|
|
|
75
|
6 |
|
foreach ($config['exchanges'] as $name => $exchange) { |
76
|
4 |
|
$autoDeclare->addMethodCall( |
77
|
4 |
|
'declareExchange', |
78
|
4 |
|
[$name, $exchange['type'], $exchange['durable'], $exchange['arguments']] |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
6 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
6 |
|
private function registerQueues( |
86
|
|
|
array $config, |
87
|
|
|
ContainerBuilder $container |
88
|
|
|
): self { |
89
|
6 |
|
$autoDeclare = $container->getDefinition('innmind.amqp.client.auto_declare'); |
90
|
6 |
|
$consumers = $container->getDefinition('innmind.amqp.consumers'); |
91
|
|
|
|
92
|
6 |
|
foreach ($config['queues'] as $name => $queue) { |
93
|
5 |
|
$autoDeclare->addMethodCall( |
94
|
5 |
|
'declareQueue', |
95
|
5 |
|
[$name, $queue['durable'], $queue['exclusive'], $queue['arguments']] |
96
|
|
|
); |
97
|
5 |
|
$consumers->addMethodCall( |
98
|
5 |
|
'add', |
99
|
5 |
|
[$name, new Reference($queue['consumer'])] |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
6 |
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
6 |
View Code Duplication |
private function registerBindings( |
107
|
|
|
array $config, |
108
|
|
|
ContainerBuilder $container |
109
|
|
|
): self { |
110
|
6 |
|
$autoDeclare = $container->getDefinition('innmind.amqp.client.auto_declare'); |
111
|
|
|
|
112
|
6 |
|
foreach ($config['bindings'] as $binding) { |
113
|
4 |
|
$autoDeclare->addMethodCall( |
114
|
4 |
|
'declareBinding', |
115
|
4 |
|
[$binding['exchange'], $binding['queue'], $binding['routingKey'], $binding['arguments']] |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
6 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
6 |
|
private function registerProducers( |
123
|
|
|
array $config, |
124
|
|
|
ContainerBuilder $container |
125
|
|
|
): self { |
126
|
6 |
|
foreach ($config['exchanges'] as $name => $_) { |
127
|
4 |
|
$container->setDefinition( |
128
|
4 |
|
'innmind.amqp.producer.'.$name, |
129
|
4 |
|
new Definition( |
130
|
4 |
|
Producer::class, |
131
|
|
|
[ |
132
|
4 |
|
new Reference('innmind.amqp.client'), |
133
|
4 |
|
$name |
134
|
|
|
] |
135
|
|
|
) |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
|
139
|
6 |
|
return $this; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|