From 8b64012632f8a0788d2e1dbce12c320e2fe23fc5 Mon Sep 17 00:00:00 2001 From: Marc Morera Date: Tue, 10 Mar 2020 10:23:21 +0100 Subject: [PATCH] Using custom client builders --- .../CommandBusConfiguration.php | 24 +++++++--- .../CompilerPass/BusCompilerPass.php | 25 ++++++++-- Tests/Async/AMQPAsyncTest.php | 40 +--------------- Tests/Async/PostgreSQLAsyncTest.php | 46 ++----------------- Tests/Async/RedisAsyncTest.php | 40 +--------------- Tests/Console/CommandsListTest.php | 25 +--------- composer.json | 6 +-- 7 files changed, 48 insertions(+), 158 deletions(-) diff --git a/DependencyInjection/CommandBusConfiguration.php b/DependencyInjection/CommandBusConfiguration.php index 59f6653..871dc14 100644 --- a/DependencyInjection/CommandBusConfiguration.php +++ b/DependencyInjection/CommandBusConfiguration.php @@ -63,25 +63,37 @@ protected function setupTree(ArrayNodeDefinition $rootNode) ->end() ->arrayNode('async_adapter') ->children() - ->enumNode('adapter') - ->values(['in_memory', 'amqp', 'redis', 'postgresql']) - ->end() + ->scalarNode('adapter')->end() ->arrayNode('in_memory')->end() ->arrayNode('amqp') ->children() - ->scalarNode('client')->end() + ->scalarNode('host')->end() + ->scalarNode('port')->defaultValue('5672')->end() + ->scalarNode('vhost')->defaultValue('/')->end() + ->scalarNode('user')->defaultValue('guest')->end() + ->scalarNode('password')->defaultValue('guest')->end() ->scalarNode('queue')->end() ->end() ->end() ->arrayNode('redis') ->children() - ->scalarNode('client')->end() + ->scalarNode('host')->end() + ->scalarNode('port')->defaultValue('6379')->end() + ->scalarNode('database')->defaultValue('/')->end() + ->scalarNode('password')->defaultNull()->end() + ->scalarNode('protocol')->defaultValue('redis://')->end() + ->floatNode('timeout')->defaultNull()->end() + ->floatNode('idle')->defaultNull()->end() ->scalarNode('key')->end() ->end() ->end() ->arrayNode('postgresql') ->children() - ->scalarNode('client')->end() + ->scalarNode('host')->end() + ->scalarNode('port')->defaultValue('5432')->end() + ->scalarNode('database')->isRequired()->end() + ->scalarNode('user')->isRequired()->end() + ->scalarNode('password')->isRequired()->end() ->scalarNode('channel')->end() ->end() ->end() diff --git a/DependencyInjection/CompilerPass/BusCompilerPass.php b/DependencyInjection/CompilerPass/BusCompilerPass.php index 34473b2..fc0fef3 100644 --- a/DependencyInjection/CompilerPass/BusCompilerPass.php +++ b/DependencyInjection/CompilerPass/BusCompilerPass.php @@ -15,6 +15,7 @@ namespace Drift\CommandBus\DependencyInjection\CompilerPass; +use Drift\AMQP\DependencyInjection\CompilerPass\AMQPCompilerPass; use Drift\CommandBus\Async\AMQPAdapter; use Drift\CommandBus\Async\AsyncAdapter; use Drift\CommandBus\Async\InMemoryAdapter; @@ -32,6 +33,9 @@ use Drift\CommandBus\Middleware\AsyncMiddleware; use Drift\CommandBus\Middleware\HandlerMiddleware; use Drift\CommandBus\Middleware\Middleware; +use Drift\Postgresql\DependencyInjection\CompilerPass\PostgresqlCompilerPass; +use Drift\Redis\DependencyInjection\CompilerPass\RedisCompilerPass; +use Exception; use React\EventLoop\LoopInterface; use ReflectionClass; use ReflectionException; @@ -105,7 +109,7 @@ public function createAsyncMiddleware(ContainerBuilder $container): bool $this->createPostgreSQLAsyncAdapter($container, $adapter); break; default: - return false; + throw new Exception('Wrong adapter'); } $container->setDefinition(AsyncMiddleware::class, @@ -474,11 +478,14 @@ private function createRedisAsyncAdapter( ContainerBuilder $container, array $adapter ) { + $adapter['preload'] = true; + RedisCompilerPass::createClient($container, 'command_bus', $adapter); + $container->setDefinition( AsyncAdapter::class, ( new Definition(RedisAdapter::class, [ - new Reference('redis.'.$adapter['client'].'_client'), + new Reference('redis.command_bus_client'), new Reference('reactphp.event_loop'), $adapter['key'] ?? 'commands', ]) @@ -496,13 +503,18 @@ private function createPostgreSQLAsyncAdapter( ContainerBuilder $container, array $adapter ) { + $channel = $adapter['channel'] ?? 'commands'; + unset($adapter['channel']); + + PostgresqlCompilerPass::createclient($container, 'command_bus', $adapter); + $container->setDefinition( AsyncAdapter::class, ( new Definition(PostgreSQLAdapter::class, [ - new Reference('postgresql.'.$adapter['client'].'_client'), + new Reference('postgresql.command_bus_client'), new Reference('reactphp.event_loop'), - $adapter['channel'] ?? 'commands', + $channel, ]) )->setLazy(true) ); @@ -518,11 +530,14 @@ private function createAMQPAsyncAdapter( ContainerBuilder $container, array $adapter ) { + $adapter['preload'] = true; + AMQPCompilerPass::registerClient($container, 'command_bus', $adapter); + $container->setDefinition( AsyncAdapter::class, ( new Definition(AMQPAdapter::class, [ - new Reference('amqp.'.$adapter['client'].'_channel'), + new Reference('amqp.command_bus_channel'), new Reference('reactphp.event_loop'), $adapter['queue'] ?? 'commands', ]) diff --git a/Tests/Async/AMQPAsyncTest.php b/Tests/Async/AMQPAsyncTest.php index 3cd47e1..3bf4726 100644 --- a/Tests/Async/AMQPAsyncTest.php +++ b/Tests/Async/AMQPAsyncTest.php @@ -15,49 +15,11 @@ namespace Drift\CommandBus\Tests\Async; -use Drift\AMQP\AMQPBundle; - /** * Class AMQPAsyncTest. */ class AMQPAsyncTest extends AsyncAdapterTest { - /** - * Decorate bundles. - * - * @param array $bundles - * - * @return array - */ - protected static function decorateBundles(array $bundles): array - { - $bundles[] = AMQPBundle::class; - - return $bundles; - } - - /** - * Decorate configuration. - * - * @param array $configuration - * - * @return array - */ - protected static function decorateConfiguration(array $configuration): array - { - $configuration = parent::decorateConfiguration($configuration); - - $configuration['amqp'] = [ - 'clients' => [ - 'amqp_1' => [ - 'host' => '127.0.0.1', - ], - ], - ]; - - return $configuration; - } - /** * {@inheritdoc} */ @@ -67,7 +29,7 @@ protected static function getAsyncConfiguration(): array 'adapter' => 'amqp', 'in_memory' => [], 'amqp' => [ - 'client' => 'amqp_1', + 'host' => '127.0.0.1', 'queue' => 'commands', ], ]; diff --git a/Tests/Async/PostgreSQLAsyncTest.php b/Tests/Async/PostgreSQLAsyncTest.php index 04dddbc..2dfe40e 100644 --- a/Tests/Async/PostgreSQLAsyncTest.php +++ b/Tests/Async/PostgreSQLAsyncTest.php @@ -15,52 +15,11 @@ namespace Drift\CommandBus\Tests\Async; -use Drift\Postgresql\PostgresqlBundle; - /** * Class PostgreSQLAsyncTest. */ class PostgreSQLAsyncTest extends AsyncAdapterTest { - /** - * Decorate bundles. - * - * @param array $bundles - * - * @return array - */ - protected static function decorateBundles(array $bundles): array - { - $bundles[] = PostgresqlBundle::class; - - return $bundles; - } - - /** - * Decorate configuration. - * - * @param array $configuration - * - * @return array - */ - protected static function decorateConfiguration(array $configuration): array - { - $configuration = parent::decorateConfiguration($configuration); - - $configuration['postgresql'] = [ - 'clients' => [ - 'postgresql_1' => [ - 'host' => '127.0.0.1', - 'database' => 'commands', - 'user' => 'root', - 'password' => 'root', - ], - ], - ]; - - return $configuration; - } - /** * {@inheritdoc} */ @@ -68,7 +27,10 @@ protected static function getAsyncConfiguration(): array { return [ 'postgresql' => [ - 'client' => 'postgresql_1', + 'host' => '127.0.0.1', + 'database' => 'commands', + 'user' => 'root', + 'password' => 'root', 'channel' => 'commands', ], ]; diff --git a/Tests/Async/RedisAsyncTest.php b/Tests/Async/RedisAsyncTest.php index ed7d338..4d17f0c 100644 --- a/Tests/Async/RedisAsyncTest.php +++ b/Tests/Async/RedisAsyncTest.php @@ -15,49 +15,11 @@ namespace Drift\CommandBus\Tests\Async; -use Drift\Redis\RedisBundle; - /** * Class RedisAsyncAdapterTest. */ class RedisAsyncAdapterTest extends AsyncAdapterTest { - /** - * Decorate bundles. - * - * @param array $bundles - * - * @return array - */ - protected static function decorateBundles(array $bundles): array - { - $bundles[] = RedisBundle::class; - - return $bundles; - } - - /** - * Decorate configuration. - * - * @param array $configuration - * - * @return array - */ - protected static function decorateConfiguration(array $configuration): array - { - $configuration = parent::decorateConfiguration($configuration); - - $configuration['redis'] = [ - 'clients' => [ - 'redis_1' => [ - 'host' => '127.0.0.1', - ], - ], - ]; - - return $configuration; - } - /** * {@inheritdoc} */ @@ -65,7 +27,7 @@ protected static function getAsyncConfiguration(): array { return [ 'redis' => [ - 'client' => 'redis_1', + 'host' => '127.0.0.1', 'key' => 'commands', ], ]; diff --git a/Tests/Console/CommandsListTest.php b/Tests/Console/CommandsListTest.php index 3f7863a..97ad7ed 100644 --- a/Tests/Console/CommandsListTest.php +++ b/Tests/Console/CommandsListTest.php @@ -15,7 +15,6 @@ namespace Drift\CommandBus\Tests\Console; -use Drift\AMQP\AMQPBundle; use Drift\CommandBus\Tests\BusFunctionalTest; /** @@ -23,20 +22,6 @@ */ class CommandsListTest extends BusFunctionalTest { - /** - * Decorate bundles. - * - * @param array $bundles - * - * @return array - */ - protected static function decorateBundles(array $bundles): array - { - $bundles[] = AMQPBundle::class; - - return $bundles; - } - /** * Decorate configuration. * @@ -48,19 +33,11 @@ protected static function decorateConfiguration(array $configuration): array { $configuration = parent::decorateConfiguration($configuration); - $configuration['amqp'] = [ - 'clients' => [ - 'amqp_1' => [ - 'host' => '127.0.0.99', - ], - ], - ]; - $configuration['command_bus'] = [ 'command_bus' => [ 'async_adapter' => [ 'amqp' => [ - 'client' => 'amqp_1', + 'host' => '127.0.0.99', 'queue' => 'commands', ], ], diff --git a/composer.json b/composer.json index 764ed99..2ff9adf 100644 --- a/composer.json +++ b/composer.json @@ -24,9 +24,9 @@ "clue/block-react": "^1.3" }, "require-dev": { - "drift/redis-bundle": "0.1.*, >=0.1.2", - "drift/amqp-bundle": "0.1.*, >=0.1.1", - "drift/postgresql-bundle": "0.1.*, >=0.1.1", + "drift/redis-bundle": "0.1.*, >=0.1.4", + "drift/amqp-bundle": "0.1.*, >=0.1.2", + "drift/postgresql-bundle": "0.1.*, >=0.1.2", "symfony/process": "^5.0.0" }, "suggest": {