Skip to content

Commit 0842340

Browse files
committed
multi client configuration
1 parent 15e9260 commit 0842340

File tree

9 files changed

+51
-41
lines changed

9 files changed

+51
-41
lines changed

pkg/enqueue-bundle/DependencyInjection/Configuration.php

+14-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Enqueue\Bundle\DependencyInjection;
44

55
use Enqueue\AsyncCommand\RunCommandProcessor;
6+
use Enqueue\AsyncEventDispatcher\DependencyInjection\AsyncEventDispatcherExtension;
67
use Enqueue\JobQueue\Job;
78
use Enqueue\Monitoring\Symfony\DependencyInjection\MonitoringFactory;
89
use Enqueue\Symfony\Client\DependencyInjection\ClientFactory;
@@ -37,6 +38,7 @@ public function getConfigTreeBuilder(): TreeBuilder
3738
->append($this->getMonitoringConfiguration())
3839
->append($this->getAsyncCommandsConfiguration())
3940
->append($this->getJobConfiguration())
41+
->append($this->getAsyncEventsConfiguration())
4042
->arrayNode('extensions')->addDefaultsIfNotSet()->children()
4143
->booleanNode('doctrine_ping_connection_extension')->defaultFalse()->end()
4244
->booleanNode('doctrine_clear_identity_map_extension')->defaultFalse()->end()
@@ -47,13 +49,6 @@ public function getConfigTreeBuilder(): TreeBuilder
4749
->end()
4850
;
4951

50-
// $rootNode->children()
51-
// ->arrayNode('async_events')
52-
// ->addDefaultsIfNotSet()
53-
// ->canBeEnabled()
54-
// ->end()
55-
// ;
56-
5752
return $tb;
5853
}
5954

@@ -89,4 +84,16 @@ private function getJobConfiguration(): ArrayNodeDefinition
8984
->canBeEnabled()
9085
;
9186
}
87+
88+
private function getAsyncEventsConfiguration(): ArrayNodeDefinition
89+
{
90+
if (false == class_exists(AsyncEventDispatcherExtension::class)) {
91+
return MissingComponentFactory::getConfiguration('async_events', ['enqueue/async-event-dispatcher']);
92+
}
93+
94+
return (new ArrayNodeDefinition('job'))
95+
->addDefaultsIfNotSet()
96+
->canBeEnabled()
97+
;
98+
}
9299
}

pkg/enqueue-bundle/DependencyInjection/EnqueueExtension.php

+15-14
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function load(array $configs, ContainerBuilder $container): void
5353
// transport & consumption
5454
$transportNames[] = $name;
5555

56-
$transportFactory = (new TransportFactory($name));
56+
$transportFactory = (new TransportFactory($name, $defaultName === $name));
5757
$transportFactory->buildConnectionFactory($container, $configs['transport']);
5858
$transportFactory->buildContext($container, []);
5959
$transportFactory->buildQueueConsumer($container, $configs['consumption']);
@@ -68,8 +68,8 @@ public function load(array $configs, ContainerBuilder $container): void
6868
$clientConfig['transport'] = $configs['transport'];
6969
$clientConfig['consumption'] = $configs['consumption'];
7070

71-
$clientFactory = new ClientFactory($name);
72-
$clientFactory->build($container, $clientConfig, $defaultName === $name);
71+
$clientFactory = new ClientFactory($name, $defaultName === $name);
72+
$clientFactory->build($container, $clientConfig);
7373
$clientFactory->createDriver($container, $configs['transport']);
7474
$clientFactory->createFlushSpoolProducerListener($container);
7575
}
@@ -97,6 +97,18 @@ public function load(array $configs, ContainerBuilder $container): void
9797

9898
$loader->load('job.yml');
9999
}
100+
101+
// async events
102+
if (false == empty($config['async_events']['enabled'])) {
103+
if ($name !== $defaultName) {
104+
throw new \LogicException('Async events supports only default configuration.');
105+
}
106+
107+
$extension = new AsyncEventDispatcherExtension();
108+
$extension->load([[
109+
'context_service' => 'enqueue.transport.default.context',
110+
]], $container);
111+
}
100112
}
101113

102114
$defaultClient = null;
@@ -125,17 +137,6 @@ public function load(array $configs, ContainerBuilder $container): void
125137
$this->loadDoctrineClearIdentityMapExtension($config, $container);
126138
$this->loadSignalExtension($config, $container);
127139
$this->loadReplyExtension($config, $container);
128-
129-
// if ($config['async_events']['enabled']) {
130-
// if (false == class_exists(AsyncEventDispatcherExtension::class)) {
131-
// throw new \LogicException('The "enqueue/async-event-dispatcher" package has to be installed.');
132-
// }
133-
//
134-
// $extension = new AsyncEventDispatcherExtension();
135-
// $extension->load([[
136-
// 'context_service' => 'enqueue.transport.default.context',
137-
// ]], $container);
138-
// }
139140
}
140141

141142
public function getConfiguration(array $config, ContainerBuilder $container): Configuration

pkg/enqueue-bundle/Tests/Functional/App/config/config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ enqueue:
2828
traceable_producer: true
2929
async_commands: true
3030
job: true
31-
# async_commands: true
31+
async_commands: true
3232

3333
services:
3434
test_enqueue.client.default.traceable_producer:

pkg/enqueue-bundle/Tests/Functional/Events/AsyncListenerTest.php

-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ class AsyncListenerTest extends WebTestCase
1818
{
1919
public function setUp()
2020
{
21-
$this->markTestSkipped('Configuration for async_events is not yet ready');
22-
2321
parent::setUp();
2422

2523
/** @var AsyncListener $asyncListener */

pkg/enqueue-bundle/Tests/Functional/Events/AsyncProcessorTest.php

-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ class AsyncProcessorTest extends WebTestCase
2020
{
2121
public function setUp()
2222
{
23-
$this->markTestSkipped('Configuration for async_events is not yet ready');
24-
2523
parent::setUp();
2624

2725
/** @var AsyncListener $asyncListener */

pkg/enqueue-bundle/Tests/Functional/Events/AsyncSubscriberTest.php

-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ class AsyncSubscriberTest extends WebTestCase
1818
{
1919
public function setUp()
2020
{
21-
$this->markTestSkipped('Configuration for async_events is not yet ready');
22-
2321
parent::setUp();
2422

2523
/** @var AsyncListener $asyncListener */

pkg/enqueue-bundle/Tests/Unit/DependencyInjection/ConfigurationTest.php

-4
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,6 @@ public function testReplyExtensionCouldBeDisabled()
373373

374374
public function testShouldDisableAsyncEventsByDefault()
375375
{
376-
$this->markTestSkipped('Configuration for async_events is not yet ready');
377-
378376
$configuration = new Configuration(true);
379377

380378
$processor = new Processor();
@@ -391,8 +389,6 @@ public function testShouldDisableAsyncEventsByDefault()
391389

392390
public function testShouldAllowEnableAsyncEvents()
393391
{
394-
$this->markTestSkipped('Configuration for async_events is not yet ready');
395-
396392
$configuration = new Configuration(true);
397393

398394
$processor = new Processor();

pkg/enqueue/Symfony/Client/DependencyInjection/ClientFactory.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,19 @@ final class ClientFactory
4141
*/
4242
private $name;
4343

44-
public function __construct(string $name)
44+
/**
45+
* @var bool
46+
*/
47+
private $default;
48+
49+
public function __construct(string $name, bool $default = false)
4550
{
4651
if (empty($name)) {
4752
throw new \InvalidArgumentException('The name could not be empty.');
4853
}
4954

5055
$this->name = $name;
56+
$this->default = $default;
5157
}
5258

5359
public static function getConfiguration(bool $debug, string $name = 'client'): NodeDefinition
@@ -69,7 +75,7 @@ public static function getConfiguration(bool $debug, string $name = 'client'): N
6975
return $builder;
7076
}
7177

72-
public function build(ContainerBuilder $container, array $config, bool $default = false): void
78+
public function build(ContainerBuilder $container, array $config): void
7379
{
7480
$container->register($this->format('context'), Context::class)
7581
->setFactory([$this->reference('driver'), 'getContext'])
@@ -176,7 +182,7 @@ public function build(ContainerBuilder $container, array $config, bool $default
176182
->addTag('enqueue.consumption_extension', ['priority' => 10, 'client' => $this->name])
177183
;
178184

179-
$container->getDefinition('enqueue.client.default.delay_redelivered_message_extension')
185+
$container->getDefinition($this->format('delay_redelivered_message_extension'))
180186
->replaceArgument(1, $config['redelivered_delay_time'])
181187
;
182188
}
@@ -192,7 +198,7 @@ public function build(ContainerBuilder $container, array $config, bool $default
192198
]));
193199
}
194200

195-
if ($default) {
201+
if ($this->default) {
196202
$container->setAlias(ProducerInterface::class, $this->format('producer'));
197203
$container->setAlias(SpoolProducer::class, $this->format('spool_producer'));
198204
}

pkg/enqueue/Symfony/DependencyInjection/TransportFactory.php

+11-5
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,19 @@ final class TransportFactory
3232
*/
3333
private $name;
3434

35-
public function __construct(string $name)
35+
/**
36+
* @var bool
37+
*/
38+
private $default;
39+
40+
public function __construct(string $name, bool $default = false)
3641
{
3742
if (empty($name)) {
3843
throw new \InvalidArgumentException('The name could not be empty.');
3944
}
4045

4146
$this->name = $name;
47+
$this->default = $default;
4248
}
4349

4450
public static function getConfiguration(string $name = 'transport'): NodeDefinition
@@ -149,7 +155,7 @@ public function buildConnectionFactory(ContainerBuilder $container, array $confi
149155
;
150156
}
151157

152-
if ('default' === $this->name) {
158+
if ($this->default) {
153159
$container->setAlias(ConnectionFactory::class, $this->format('connection_factory'));
154160
}
155161
}
@@ -167,7 +173,7 @@ public function buildContext(ContainerBuilder $container, array $config): void
167173

168174
$this->addServiceToLocator($container, 'context');
169175

170-
if ('default' === $this->name) {
176+
if ($this->default) {
171177
$container->setAlias(Context::class, $this->format('context'));
172178
}
173179
}
@@ -201,7 +207,7 @@ public function buildQueueConsumer(ContainerBuilder $container, array $config):
201207
$this->addServiceToLocator($container, 'queue_consumer');
202208
$this->addServiceToLocator($container, 'processor_registry');
203209

204-
if ('default' === $this->name) {
210+
if ($this->default) {
205211
$container->setAlias(QueueConsumerInterface::class, $this->format('queue_consumer'));
206212
}
207213
}
@@ -220,7 +226,7 @@ public function buildRpcClient(ContainerBuilder $container, array $config): void
220226
->addArgument(new Reference($this->format('rpc_factory')))
221227
;
222228

223-
if ('default' === $this->name) {
229+
if ($this->default) {
224230
$container->setAlias(RpcClient::class, $this->format('rpc_client'));
225231
}
226232
}

0 commit comments

Comments
 (0)