|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace TomCizek\AsynchronousRouter\DependencyInjection; |
|
6
|
|
|
|
|
7
|
|
|
use Prooph\Common\Messaging\MessageConverter; |
|
8
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
|
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
10
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
11
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
12
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
13
|
|
|
use TomCizek\AsynchronousRouter\AsynchronousMessageProducer; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Defines and load message bus instances. |
|
17
|
|
|
*/ |
|
18
|
|
|
final class ProophAsynchronousRouterExtension extends Extension |
|
19
|
|
|
{ |
|
20
|
|
|
const KEY_BRIDGE = 'bridge'; |
|
21
|
|
|
const KEY_ROUTES = 'routes'; |
|
22
|
|
|
|
|
23
|
|
|
public function getConfiguration(array $config, ContainerBuilder $container): ConfigurationInterface |
|
24
|
|
|
{ |
|
25
|
|
|
return new Configuration(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function load(array $configs, ContainerBuilder $container) |
|
29
|
|
|
{ |
|
30
|
|
|
$configuration = $this->getConfiguration($configs, $container); |
|
31
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
|
32
|
|
|
|
|
33
|
|
|
if (! empty($config['producers'])) { |
|
34
|
|
|
$this->loadProducers($config['producers'], $container); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
private function loadProducers( |
|
39
|
|
|
array $config, |
|
40
|
|
|
ContainerBuilder $container |
|
41
|
|
|
): void { |
|
42
|
|
|
$producers = []; |
|
43
|
|
|
foreach (array_keys($config) as $producerName) { |
|
44
|
|
|
$producers[$producerName] = 'prooph_asynchronous_router.' . $producerName; |
|
45
|
|
|
} |
|
46
|
|
|
$container->setParameter('prooph_asynchronous_router.producers', $producers); |
|
47
|
|
|
|
|
48
|
|
|
foreach ($config as $producerName => $producerConfig) { |
|
49
|
|
|
$this->loadProducer($producerName, $config[$producerName], $container); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
private function loadProducer(string $name, array $config, ContainerBuilder $container) |
|
54
|
|
|
{ |
|
55
|
|
|
$routes = is_array($config[self::KEY_ROUTES]) ? $config[self::KEY_ROUTES] : []; |
|
56
|
|
|
|
|
57
|
|
|
$producerBridgeKey = $config[self::KEY_BRIDGE]; |
|
58
|
|
|
|
|
59
|
|
|
$asynchronousProducerDefinition = new Definition(AsynchronousMessageProducer::class); |
|
60
|
|
|
$asynchronousProducerDefinition |
|
61
|
|
|
->setArguments( |
|
62
|
|
|
[ |
|
63
|
|
|
new Reference($producerBridgeKey), |
|
64
|
|
|
new Reference(MessageConverter::class), |
|
65
|
|
|
] |
|
66
|
|
|
) |
|
67
|
|
|
->addMethodCall('injectRoutes', [$routes]) |
|
68
|
|
|
->setPublic(true); |
|
69
|
|
|
$producerServiceId = 'prooph_asynchronous_router.' . $name; |
|
70
|
|
|
|
|
71
|
|
|
$container->setDefinition($producerServiceId, $asynchronousProducerDefinition); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|