Skip to content

Commit d9ca648

Browse files
authored
Merge pull request #25 from driftphp/feature/using-custom-client-builders
Using custom client builders
2 parents feb6081 + 8b64012 commit d9ca648

File tree

7 files changed

+48
-158
lines changed

7 files changed

+48
-158
lines changed

DependencyInjection/CommandBusConfiguration.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,37 @@ protected function setupTree(ArrayNodeDefinition $rootNode)
6363
->end()
6464
->arrayNode('async_adapter')
6565
->children()
66-
->enumNode('adapter')
67-
->values(['in_memory', 'amqp', 'redis', 'postgresql'])
68-
->end()
66+
->scalarNode('adapter')->end()
6967
->arrayNode('in_memory')->end()
7068
->arrayNode('amqp')
7169
->children()
72-
->scalarNode('client')->end()
70+
->scalarNode('host')->end()
71+
->scalarNode('port')->defaultValue('5672')->end()
72+
->scalarNode('vhost')->defaultValue('/')->end()
73+
->scalarNode('user')->defaultValue('guest')->end()
74+
->scalarNode('password')->defaultValue('guest')->end()
7375
->scalarNode('queue')->end()
7476
->end()
7577
->end()
7678
->arrayNode('redis')
7779
->children()
78-
->scalarNode('client')->end()
80+
->scalarNode('host')->end()
81+
->scalarNode('port')->defaultValue('6379')->end()
82+
->scalarNode('database')->defaultValue('/')->end()
83+
->scalarNode('password')->defaultNull()->end()
84+
->scalarNode('protocol')->defaultValue('redis://')->end()
85+
->floatNode('timeout')->defaultNull()->end()
86+
->floatNode('idle')->defaultNull()->end()
7987
->scalarNode('key')->end()
8088
->end()
8189
->end()
8290
->arrayNode('postgresql')
8391
->children()
84-
->scalarNode('client')->end()
92+
->scalarNode('host')->end()
93+
->scalarNode('port')->defaultValue('5432')->end()
94+
->scalarNode('database')->isRequired()->end()
95+
->scalarNode('user')->isRequired()->end()
96+
->scalarNode('password')->isRequired()->end()
8597
->scalarNode('channel')->end()
8698
->end()
8799
->end()

DependencyInjection/CompilerPass/BusCompilerPass.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
namespace Drift\CommandBus\DependencyInjection\CompilerPass;
1717

18+
use Drift\AMQP\DependencyInjection\CompilerPass\AMQPCompilerPass;
1819
use Drift\CommandBus\Async\AMQPAdapter;
1920
use Drift\CommandBus\Async\AsyncAdapter;
2021
use Drift\CommandBus\Async\InMemoryAdapter;
@@ -32,6 +33,9 @@
3233
use Drift\CommandBus\Middleware\AsyncMiddleware;
3334
use Drift\CommandBus\Middleware\HandlerMiddleware;
3435
use Drift\CommandBus\Middleware\Middleware;
36+
use Drift\Postgresql\DependencyInjection\CompilerPass\PostgresqlCompilerPass;
37+
use Drift\Redis\DependencyInjection\CompilerPass\RedisCompilerPass;
38+
use Exception;
3539
use React\EventLoop\LoopInterface;
3640
use ReflectionClass;
3741
use ReflectionException;
@@ -105,7 +109,7 @@ public function createAsyncMiddleware(ContainerBuilder $container): bool
105109
$this->createPostgreSQLAsyncAdapter($container, $adapter);
106110
break;
107111
default:
108-
return false;
112+
throw new Exception('Wrong adapter');
109113
}
110114

111115
$container->setDefinition(AsyncMiddleware::class,
@@ -474,11 +478,14 @@ private function createRedisAsyncAdapter(
474478
ContainerBuilder $container,
475479
array $adapter
476480
) {
481+
$adapter['preload'] = true;
482+
RedisCompilerPass::createClient($container, 'command_bus', $adapter);
483+
477484
$container->setDefinition(
478485
AsyncAdapter::class,
479486
(
480487
new Definition(RedisAdapter::class, [
481-
new Reference('redis.'.$adapter['client'].'_client'),
488+
new Reference('redis.command_bus_client'),
482489
new Reference('reactphp.event_loop'),
483490
$adapter['key'] ?? 'commands',
484491
])
@@ -496,13 +503,18 @@ private function createPostgreSQLAsyncAdapter(
496503
ContainerBuilder $container,
497504
array $adapter
498505
) {
506+
$channel = $adapter['channel'] ?? 'commands';
507+
unset($adapter['channel']);
508+
509+
PostgresqlCompilerPass::createclient($container, 'command_bus', $adapter);
510+
499511
$container->setDefinition(
500512
AsyncAdapter::class,
501513
(
502514
new Definition(PostgreSQLAdapter::class, [
503-
new Reference('postgresql.'.$adapter['client'].'_client'),
515+
new Reference('postgresql.command_bus_client'),
504516
new Reference('reactphp.event_loop'),
505-
$adapter['channel'] ?? 'commands',
517+
$channel,
506518
])
507519
)->setLazy(true)
508520
);
@@ -518,11 +530,14 @@ private function createAMQPAsyncAdapter(
518530
ContainerBuilder $container,
519531
array $adapter
520532
) {
533+
$adapter['preload'] = true;
534+
AMQPCompilerPass::registerClient($container, 'command_bus', $adapter);
535+
521536
$container->setDefinition(
522537
AsyncAdapter::class,
523538
(
524539
new Definition(AMQPAdapter::class, [
525-
new Reference('amqp.'.$adapter['client'].'_channel'),
540+
new Reference('amqp.command_bus_channel'),
526541
new Reference('reactphp.event_loop'),
527542
$adapter['queue'] ?? 'commands',
528543
])

Tests/Async/AMQPAsyncTest.php

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,49 +15,11 @@
1515

1616
namespace Drift\CommandBus\Tests\Async;
1717

18-
use Drift\AMQP\AMQPBundle;
19-
2018
/**
2119
* Class AMQPAsyncTest.
2220
*/
2321
class AMQPAsyncTest extends AsyncAdapterTest
2422
{
25-
/**
26-
* Decorate bundles.
27-
*
28-
* @param array $bundles
29-
*
30-
* @return array
31-
*/
32-
protected static function decorateBundles(array $bundles): array
33-
{
34-
$bundles[] = AMQPBundle::class;
35-
36-
return $bundles;
37-
}
38-
39-
/**
40-
* Decorate configuration.
41-
*
42-
* @param array $configuration
43-
*
44-
* @return array
45-
*/
46-
protected static function decorateConfiguration(array $configuration): array
47-
{
48-
$configuration = parent::decorateConfiguration($configuration);
49-
50-
$configuration['amqp'] = [
51-
'clients' => [
52-
'amqp_1' => [
53-
'host' => '127.0.0.1',
54-
],
55-
],
56-
];
57-
58-
return $configuration;
59-
}
60-
6123
/**
6224
* {@inheritdoc}
6325
*/
@@ -67,7 +29,7 @@ protected static function getAsyncConfiguration(): array
6729
'adapter' => 'amqp',
6830
'in_memory' => [],
6931
'amqp' => [
70-
'client' => 'amqp_1',
32+
'host' => '127.0.0.1',
7133
'queue' => 'commands',
7234
],
7335
];

Tests/Async/PostgreSQLAsyncTest.php

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,60 +15,22 @@
1515

1616
namespace Drift\CommandBus\Tests\Async;
1717

18-
use Drift\Postgresql\PostgresqlBundle;
19-
2018
/**
2119
* Class PostgreSQLAsyncTest.
2220
*/
2321
class PostgreSQLAsyncTest extends AsyncAdapterTest
2422
{
25-
/**
26-
* Decorate bundles.
27-
*
28-
* @param array $bundles
29-
*
30-
* @return array
31-
*/
32-
protected static function decorateBundles(array $bundles): array
33-
{
34-
$bundles[] = PostgresqlBundle::class;
35-
36-
return $bundles;
37-
}
38-
39-
/**
40-
* Decorate configuration.
41-
*
42-
* @param array $configuration
43-
*
44-
* @return array
45-
*/
46-
protected static function decorateConfiguration(array $configuration): array
47-
{
48-
$configuration = parent::decorateConfiguration($configuration);
49-
50-
$configuration['postgresql'] = [
51-
'clients' => [
52-
'postgresql_1' => [
53-
'host' => '127.0.0.1',
54-
'database' => 'commands',
55-
'user' => 'root',
56-
'password' => 'root',
57-
],
58-
],
59-
];
60-
61-
return $configuration;
62-
}
63-
6423
/**
6524
* {@inheritdoc}
6625
*/
6726
protected static function getAsyncConfiguration(): array
6827
{
6928
return [
7029
'postgresql' => [
71-
'client' => 'postgresql_1',
30+
'host' => '127.0.0.1',
31+
'database' => 'commands',
32+
'user' => 'root',
33+
'password' => 'root',
7234
'channel' => 'commands',
7335
],
7436
];

Tests/Async/RedisAsyncTest.php

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,57 +15,19 @@
1515

1616
namespace Drift\CommandBus\Tests\Async;
1717

18-
use Drift\Redis\RedisBundle;
19-
2018
/**
2119
* Class RedisAsyncAdapterTest.
2220
*/
2321
class RedisAsyncAdapterTest extends AsyncAdapterTest
2422
{
25-
/**
26-
* Decorate bundles.
27-
*
28-
* @param array $bundles
29-
*
30-
* @return array
31-
*/
32-
protected static function decorateBundles(array $bundles): array
33-
{
34-
$bundles[] = RedisBundle::class;
35-
36-
return $bundles;
37-
}
38-
39-
/**
40-
* Decorate configuration.
41-
*
42-
* @param array $configuration
43-
*
44-
* @return array
45-
*/
46-
protected static function decorateConfiguration(array $configuration): array
47-
{
48-
$configuration = parent::decorateConfiguration($configuration);
49-
50-
$configuration['redis'] = [
51-
'clients' => [
52-
'redis_1' => [
53-
'host' => '127.0.0.1',
54-
],
55-
],
56-
];
57-
58-
return $configuration;
59-
}
60-
6123
/**
6224
* {@inheritdoc}
6325
*/
6426
protected static function getAsyncConfiguration(): array
6527
{
6628
return [
6729
'redis' => [
68-
'client' => 'redis_1',
30+
'host' => '127.0.0.1',
6931
'key' => 'commands',
7032
],
7133
];

Tests/Console/CommandsListTest.php

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,13 @@
1515

1616
namespace Drift\CommandBus\Tests\Console;
1717

18-
use Drift\AMQP\AMQPBundle;
1918
use Drift\CommandBus\Tests\BusFunctionalTest;
2019

2120
/**
2221
* Class CommandsListTest.
2322
*/
2423
class CommandsListTest extends BusFunctionalTest
2524
{
26-
/**
27-
* Decorate bundles.
28-
*
29-
* @param array $bundles
30-
*
31-
* @return array
32-
*/
33-
protected static function decorateBundles(array $bundles): array
34-
{
35-
$bundles[] = AMQPBundle::class;
36-
37-
return $bundles;
38-
}
39-
4025
/**
4126
* Decorate configuration.
4227
*
@@ -48,19 +33,11 @@ protected static function decorateConfiguration(array $configuration): array
4833
{
4934
$configuration = parent::decorateConfiguration($configuration);
5035

51-
$configuration['amqp'] = [
52-
'clients' => [
53-
'amqp_1' => [
54-
'host' => '127.0.0.99',
55-
],
56-
],
57-
];
58-
5936
$configuration['command_bus'] = [
6037
'command_bus' => [
6138
'async_adapter' => [
6239
'amqp' => [
63-
'client' => 'amqp_1',
40+
'host' => '127.0.0.99',
6441
'queue' => 'commands',
6542
],
6643
],

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
"clue/block-react": "^1.3"
2525
},
2626
"require-dev": {
27-
"drift/redis-bundle": "0.1.*, >=0.1.2",
28-
"drift/amqp-bundle": "0.1.*, >=0.1.1",
29-
"drift/postgresql-bundle": "0.1.*, >=0.1.1",
27+
"drift/redis-bundle": "0.1.*, >=0.1.4",
28+
"drift/amqp-bundle": "0.1.*, >=0.1.2",
29+
"drift/postgresql-bundle": "0.1.*, >=0.1.2",
3030
"symfony/process": "^5.0.0"
3131
},
3232
"suggest": {

0 commit comments

Comments
 (0)