|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bdf\QueueBundle\DependencyInjection; |
|
4
|
|
|
|
|
5
|
|
|
use Bdf\Queue\Connection\ConnectionDriverInterface; |
|
6
|
|
|
use Bdf\QueueBundle\ConnectionFactory\Configuration as DriverConfiguration; |
|
7
|
|
|
use Bdf\QueueBundle\ConnectionFactory\ConnectionDriverConfiguratorInterface; |
|
8
|
|
|
use Bdf\QueueBundle\ConnectionFactory\ConnectionDriverFactory; |
|
9
|
|
|
use Bdf\QueueBundle\Consumption\ReceiverFactoryProviderInterface; |
|
10
|
|
|
use Bdf\QueueBundle\Consumption\ReceiverLoader; |
|
11
|
|
|
use Bdf\QueueBundle\DependencyInjection\Compiler\DriverFactoryPass; |
|
12
|
|
|
use Bdf\QueueBundle\DependencyInjection\Compiler\RegisterFailerDriverPass; |
|
13
|
|
|
use Bdf\QueueBundle\DependencyInjection\Compiler\RegisterReceiverFactoryPass; |
|
14
|
|
|
use Bdf\QueueBundle\DependencyInjection\Failer\FailerDriverConfiguratorInterface; |
|
15
|
|
|
use Symfony\Component\Config\FileLocator; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
17
|
|
|
use Symfony\Component\DependencyInjection\Extension\Extension; |
|
18
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
|
19
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* QueueExtension. |
|
23
|
|
|
*/ |
|
24
|
|
|
class BdfQueueExtension extends Extension |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritDoc} |
|
28
|
|
|
*/ |
|
29
|
9 |
|
public function load(array $configs, ContainerBuilder $container) |
|
30
|
|
|
{ |
|
31
|
9 |
|
$configuration = $this->getConfiguration($configs, $container); |
|
32
|
9 |
|
$config = $this->processConfiguration($configuration, $configs); |
|
33
|
|
|
|
|
34
|
9 |
|
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
35
|
9 |
|
$loader->load('queue.yaml'); |
|
36
|
|
|
|
|
37
|
9 |
|
$this->configureConnections($config, $container); |
|
38
|
9 |
|
$this->configureDestinations($config, $container); |
|
39
|
9 |
|
$this->configureFailer($config, $container); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
9 |
|
public function configureConnections(array $config, ContainerBuilder $container): void |
|
43
|
|
|
{ |
|
44
|
9 |
|
$connectionConfigs = []; |
|
45
|
|
|
|
|
46
|
9 |
|
foreach ($config['connections'] as $name => $options) { |
|
47
|
9 |
|
$connectionConfigs[$name] = $options; |
|
48
|
|
|
|
|
49
|
9 |
|
$container->register('bdf_queue.config_definition.'.$name, DriverConfiguration::class) |
|
50
|
9 |
|
->setFactory([DriverConfiguration::class, 'createConfiguration']) |
|
51
|
9 |
|
->setArguments([$name, $options]); |
|
52
|
|
|
|
|
53
|
9 |
|
$container->register('bdf_queue.connection_definition.'.$name, ConnectionDriverInterface::class) |
|
54
|
9 |
|
->setPublic(true) |
|
55
|
9 |
|
->setFactory($options['connection_factory'] ?? [new Reference(ConnectionDriverFactory::class), 'createDriver']) |
|
56
|
9 |
|
->setArguments([ |
|
57
|
9 |
|
new Reference('bdf_queue.config_definition.'.$name), |
|
58
|
9 |
|
new Reference($this->configureSerializer($options['serializer'], $config['default_serializer'])), |
|
59
|
9 |
|
]); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
9 |
|
$container->registerForAutoconfiguration(ConnectionDriverConfiguratorInterface::class) |
|
63
|
9 |
|
->setShared(false) |
|
64
|
9 |
|
->setPublic(false) |
|
65
|
9 |
|
->addTag(DriverFactoryPass::CONFIGURATOR_TAG_NAME); |
|
66
|
|
|
|
|
67
|
9 |
|
$container->setParameter('bdf_queue.default_connection', $config['default_connection'] ?? key($connectionConfigs)); |
|
68
|
9 |
|
$container->setParameter('bdf_queue.connections', $connectionConfigs); |
|
69
|
9 |
|
$container->setParameter('bdf_queue.connection_names', array_keys($connectionConfigs)); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
9 |
|
private function configureSerializer(array $config, string $default): string |
|
73
|
|
|
{ |
|
74
|
9 |
|
if (isset($config['service'])) { |
|
75
|
|
|
return $config['service']; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
9 |
|
return 'bdf_queue.serializer.'.($config['id'] ?? $default); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
9 |
|
public function configureDestinations(array $config, ContainerBuilder $container): void |
|
82
|
|
|
{ |
|
83
|
9 |
|
$destinations = []; |
|
84
|
9 |
|
$consumptionConfig = []; |
|
85
|
|
|
|
|
86
|
9 |
|
foreach ($config['destinations'] as $name => $options) { |
|
87
|
8 |
|
$destinations[$name] = $options['url']; |
|
88
|
|
|
|
|
89
|
|
|
// TODO build a builder rule in the container for this destination |
|
90
|
8 |
|
foreach ($options['consumer'] ?? [] as $option => $value) { |
|
91
|
8 |
|
if (null !== $value) { |
|
92
|
8 |
|
if (($value[0] ?? '') === '@') { |
|
93
|
2 |
|
$value = new Reference(ltrim($value, '@')); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
8 |
|
$consumptionConfig[$name][$option] = $value; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
9 |
|
$container->registerForAutoconfiguration(ReceiverFactoryProviderInterface::class) |
|
102
|
9 |
|
->setShared(false) |
|
103
|
9 |
|
->setPublic(false) |
|
104
|
9 |
|
->addTag(RegisterReceiverFactoryPass::CONFIGURATOR_TAG_NAME); |
|
105
|
|
|
|
|
106
|
9 |
|
$container->getDefinition(ReceiverLoader::class) |
|
107
|
9 |
|
->replaceArgument(1, $consumptionConfig); |
|
108
|
|
|
|
|
109
|
9 |
|
$container->setParameter('bdf_queue.destinations', $destinations); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
9 |
|
public function configureFailer(array $config, ContainerBuilder $container): void |
|
113
|
|
|
{ |
|
114
|
9 |
|
$container->setParameter('bdf_queue.failer_dsn', $config['failer']); |
|
115
|
|
|
|
|
116
|
9 |
|
$container->registerForAutoconfiguration(FailerDriverConfiguratorInterface::class) |
|
117
|
9 |
|
->setShared(false) |
|
118
|
9 |
|
->setPublic(false) |
|
119
|
9 |
|
->addTag(RegisterFailerDriverPass::CONFIGURATOR_TAG_NAME) |
|
120
|
9 |
|
; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* {@inheritDoc} |
|
125
|
|
|
*/ |
|
126
|
9 |
|
public function getConfiguration(array $config, ContainerBuilder $container): Configuration |
|
127
|
|
|
{ |
|
128
|
9 |
|
return new Configuration($container->getParameter('kernel.debug')); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|