Skip to content

Commit

Permalink
Merge pull request #25 from driftphp/feature/using-custom-client-buil…
Browse files Browse the repository at this point in the history
…ders

Using custom client builders
  • Loading branch information
mmoreram authored Mar 10, 2020
2 parents feb6081 + 8b64012 commit d9ca648
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 158 deletions.
24 changes: 18 additions & 6 deletions DependencyInjection/CommandBusConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
25 changes: 20 additions & 5 deletions DependencyInjection/CompilerPass/BusCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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',
])
Expand All @@ -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)
);
Expand All @@ -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',
])
Expand Down
40 changes: 1 addition & 39 deletions Tests/Async/AMQPAsyncTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand All @@ -67,7 +29,7 @@ protected static function getAsyncConfiguration(): array
'adapter' => 'amqp',
'in_memory' => [],
'amqp' => [
'client' => 'amqp_1',
'host' => '127.0.0.1',
'queue' => 'commands',
],
];
Expand Down
46 changes: 4 additions & 42 deletions Tests/Async/PostgreSQLAsyncTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,60 +15,22 @@

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}
*/
protected static function getAsyncConfiguration(): array
{
return [
'postgresql' => [
'client' => 'postgresql_1',
'host' => '127.0.0.1',
'database' => 'commands',
'user' => 'root',
'password' => 'root',
'channel' => 'commands',
],
];
Expand Down
40 changes: 1 addition & 39 deletions Tests/Async/RedisAsyncTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,57 +15,19 @@

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}
*/
protected static function getAsyncConfiguration(): array
{
return [
'redis' => [
'client' => 'redis_1',
'host' => '127.0.0.1',
'key' => 'commands',
],
];
Expand Down
25 changes: 1 addition & 24 deletions Tests/Console/CommandsListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,13 @@

namespace Drift\CommandBus\Tests\Console;

use Drift\AMQP\AMQPBundle;
use Drift\CommandBus\Tests\BusFunctionalTest;

/**
* Class CommandsListTest.
*/
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.
*
Expand All @@ -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',
],
],
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit d9ca648

Please sign in to comment.