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