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