Skip to content

Commit

Permalink
Merge pull request #6 from prooph/feature/tagging_support
Browse files Browse the repository at this point in the history
Add tagging support for plugins
  • Loading branch information
codeliner authored Aug 8, 2016
2 parents ca552b6 + a4bda31 commit 558cc1e
Show file tree
Hide file tree
Showing 9 changed files with 301 additions and 2 deletions.
44 changes: 44 additions & 0 deletions src/DependencyInjection/Compiler/PluginsPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* prooph (http://getprooph.org/)
*
* @see https://github.com/prooph/service-bus-symfony-bundle for the canonical source repository
* @copyright Copyright (c) 2016 prooph software GmbH (http://prooph-software.com/)
* @license https://github.com/prooph/service-bus-symfony-bundle/blob/master/LICENSE.md New BSD License
*/

declare (strict_types = 1);

namespace Prooph\Bundle\ServiceBus\DependencyInjection\Compiler;

use Prooph\Bundle\ServiceBus\DependencyInjection\ProophServiceBusExtension;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;

class PluginsPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
foreach (ProophServiceBusExtension::AVAILABLE_BUSES as $type => $busClass) {
if (!$container->hasParameter('prooph_service_bus.' . $type . '_buses')) {
continue;
}

$buses = $container->getParameter('prooph_service_bus.' . $type . '_buses');

foreach ($buses as $name => $bus) {
$globalPlugins = $container->findTaggedServiceIds('prooph_service_bus.plugin');
$typePlugins = $container->findTaggedServiceIds(sprintf('prooph_service_bus.%s.plugin', $type . '_bus'));
$plugins = $container->findTaggedServiceIds(sprintf('prooph_service_bus.%s.plugin', $name));

$plugins = array_merge($globalPlugins, $typePlugins, $plugins);

foreach ($plugins as $id => $args) {
$definition = $container->findDefinition('prooph_service_bus.' . $name);
$definition->addMethodCall('utilize', [new Reference($id)]);
}
}
}
}
}
4 changes: 2 additions & 2 deletions src/ProophServiceBusBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Prooph\Bundle\ServiceBus;

use Prooph\Bundle\ServiceBus\DependencyInjection\Compiler\PluginsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

Expand All @@ -19,7 +20,6 @@ final class ProophServiceBusBundle extends Bundle
public function build(ContainerBuilder $container)
{
parent::build($container);

// TODO RegisterEventListenersAndSubscribersPass like doctrine ?
$container->addCompilerPass(new PluginsPass());
}
}
70 changes: 70 additions & 0 deletions test/DependencyInjection/AbtractServiceBusExtensionTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@
namespace ProophTest\Bundle\ServiceBus\DependencyInjection;

use PHPUnit_Framework_TestCase as TestCase;
use Prooph\Bundle\ServiceBus\DependencyInjection\Compiler\PluginsPass;
use Prooph\Bundle\ServiceBus\DependencyInjection\ProophServiceBusExtension;
use Prooph\ServiceBus\CommandBus;
use Prooph\ServiceBus\EventBus;
use Prooph\ServiceBus\Exception\CommandDispatchException;
use Prooph\ServiceBus\Exception\MessageDispatchException;
use Prooph\ServiceBus\Plugin\Router\CommandRouter;
use Prooph\ServiceBus\Plugin\Router\EventRouter;
use Prooph\ServiceBus\Plugin\Router\QueryRouter;
use Prooph\ServiceBus\QueryBus;
use ProophTest\Bundle\ServiceBus\DependencyInjection\Fixture\Model\AcmeRegisterUserCommand;
use ProophTest\Bundle\ServiceBus\DependencyInjection\Fixture\Model\AcmeRegisterUserHandler;
use ProophTest\Bundle\ServiceBus\DependencyInjection\Fixture\Model\MockPlugin;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\ResolveDefinitionTemplatesPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -105,6 +109,72 @@ public function it_adds_default_container_plugin()
self::assertSame($command, $mockHandler->lastCommand());
}

/**
* @test
*/
public function it_adds_plugins_based_on_tags()
{
$container = $this->loadContainer('plugins', new PluginsPass());

/** @var MockPlugin $globalPlugin */
$globalPlugin = $container->get('global_plugin');

/** @var MockPlugin $commandTypePlugin */
$commandTypePlugin = $container->get('command_type_plugin');

/** @var MockPlugin $mainCommandBusPlugin */
$mainCommandBusPlugin = $container->get('main_command_bus_plugin');

$reset = function () use ($globalPlugin, $commandTypePlugin, $mainCommandBusPlugin) {
$globalPlugin->reset();
$commandTypePlugin->reset();
$mainCommandBusPlugin->reset();
};

/* @var $mainCommandBus CommandBus */
$mainCommandBus = $container->get('prooph_service_bus.main_command_bus');

/* @var $secondCommandBus CommandBus */
$secondCommandBus = $container->get('prooph_service_bus.second_command_bus');

/* @var $mainEventBus EventBus */
$mainEventBus = $container->get('prooph_service_bus.main_event_bus');

try {
$mainCommandBus->dispatch('a message');
} catch (CommandDispatchException $ex) {
//ignore
}

$this->assertTrue($globalPlugin->wasFired());
$this->assertTrue($commandTypePlugin->wasFired());
$this->assertTrue($mainCommandBusPlugin->wasFired());

$reset();

try {
$secondCommandBus->dispatch('a message');
} catch (CommandDispatchException $ex) {
//ignore
}

$this->assertTrue($globalPlugin->wasFired());
$this->assertTrue($commandTypePlugin->wasFired());
$this->assertFalse($mainCommandBusPlugin->wasFired());

$reset();

try {
$mainEventBus->dispatch('a message');
} catch (MessageDispatchException $ex) {
//ignore
}

$this->assertTrue($globalPlugin->wasFired());
$this->assertFalse($commandTypePlugin->wasFired());
$this->assertFalse($mainCommandBusPlugin->wasFired());
}

/**
* @test
*/
Expand Down
15 changes: 15 additions & 0 deletions test/DependencyInjection/Fixture/Model/CommandTypePlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* prooph (http://getprooph.org/)
*
* @see https://github.com/prooph/service-bus-symfony-bundle for the canonical source repository
* @copyright Copyright (c) 2016 prooph software GmbH (http://prooph-software.com/)
* @license https://github.com/prooph/service-bus-symfony-bundle/blob/master/LICENSE.md New BSD License
*/
declare (strict_types = 1);

namespace ProophTest\Bundle\ServiceBus\DependencyInjection\Fixture\Model;

final class CommandTypePlugin extends MockPlugin
{
}
15 changes: 15 additions & 0 deletions test/DependencyInjection/Fixture/Model/GlobalPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* prooph (http://getprooph.org/)
*
* @see https://github.com/prooph/service-bus-symfony-bundle for the canonical source repository
* @copyright Copyright (c) 2016 prooph software GmbH (http://prooph-software.com/)
* @license https://github.com/prooph/service-bus-symfony-bundle/blob/master/LICENSE.md New BSD License
*/
declare (strict_types = 1);

namespace ProophTest\Bundle\ServiceBus\DependencyInjection\Fixture\Model;

final class GlobalPlugin extends MockPlugin
{
}
15 changes: 15 additions & 0 deletions test/DependencyInjection/Fixture/Model/MainCommandBusPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* prooph (http://getprooph.org/)
*
* @see https://github.com/prooph/service-bus-symfony-bundle for the canonical source repository
* @copyright Copyright (c) 2016 prooph software GmbH (http://prooph-software.com/)
* @license https://github.com/prooph/service-bus-symfony-bundle/blob/master/LICENSE.md New BSD License
*/
declare (strict_types = 1);

namespace ProophTest\Bundle\ServiceBus\DependencyInjection\Fixture\Model;

final class MainCommandBusPlugin extends MockPlugin
{
}
47 changes: 47 additions & 0 deletions test/DependencyInjection/Fixture/Model/MockPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* prooph (http://getprooph.org/)
*
* @see https://github.com/prooph/service-bus-symfony-bundle for the canonical source repository
* @copyright Copyright (c) 2016 prooph software GmbH (http://prooph-software.com/)
* @license https://github.com/prooph/service-bus-symfony-bundle/blob/master/LICENSE.md New BSD License
*/
declare (strict_types = 1);

namespace ProophTest\Bundle\ServiceBus\DependencyInjection\Fixture\Model;

use Prooph\Common\Event\ActionEvent;
use Prooph\Common\Event\ActionEventEmitter;
use Prooph\Common\Event\ActionEventListenerAggregate;
use Prooph\Common\Event\DetachAggregateHandlers;
use Prooph\ServiceBus\MessageBus;

class MockPlugin implements ActionEventListenerAggregate
{
use DetachAggregateHandlers;

private $fired = false;

public function wasFired(): bool
{
return $this->fired;
}

/**
* @param ActionEventEmitter $dispatcher
*/
public function attach(ActionEventEmitter $dispatcher)
{
$this->trackHandler($dispatcher->attachListener(MessageBus::EVENT_INITIALIZE, [$this, 'onInitialize']));
}

public function onInitialize(ActionEvent $event)
{
$this->fired = true;
}

public function reset()
{
$this->fired = false;
}
}
49 changes: 49 additions & 0 deletions test/DependencyInjection/Fixture/config/xml/plugins.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" ?>

<srv:container xmlns="http://getprooph.org/schemas/symfony-dic/prooph"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:srv="http://symfony.com/schema/dic/services"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://getprooph.org/schemas/symfony-dic/prooph http://getprooph.org/schemas/symfony-dic/prooph/service_bus-5.1.xsd">

<config>
<command_bus name="main_command_bus" message_factory="prooph_service_bus.message_factory">
<plugin>Prooph\ServiceBus\Plugin\InvokeStrategy\OnEventStrategy</plugin>
<router type="prooph_service_bus.command_bus_router">
<route command="Prooph\ProophessorDo\Model\User\Command\RegisterUser">Prooph\ProophessorDo\Model\User\Handler\RegisterUserHandler</route>
<route command="Prooph\ProophessorDo\Model\User\Command\UpdateUser">Prooph\ProophessorDo\Model\User\Handler\UpdateUserHandler</route>
</router>
</command_bus>
<!-- uses default values -->
<command_bus name="second_command_bus">
<plugin>Prooph\ServiceBus\Plugin\InvokeStrategy\OnEventStrategy</plugin>
<plugin>Prooph\ServiceBus\Plugin\InvokeStrategy\OnEventStrategy</plugin>
<router>
<route command="Prooph\ProophessorDo\Model\User\Command\RegisterUser">Prooph\ProophessorDo\Model\User\Handler\RegisterUserHandler</route>
<route command="Prooph\ProophessorDo\Model\User\Command\UpdateUser">Prooph\ProophessorDo\Model\User\Handler\UpdateUserHandler</route>
</router>
</command_bus>

<event_bus name="main_event_bus" message_factory="prooph_service_bus.message_factory">
<plugin>Prooph\ServiceBus\Plugin\InvokeStrategy\OnEventStrategy</plugin>
<router type="prooph_service_bus.event_bus_router">
<route event="Prooph\ProophessorDo\Model\Todo\Event\TodoWasPosted">
<listener>Prooph\ProophessorDo\Projection\Todo\TodoProjector</listener>
<listener>Prooph\ProophessorDo\Projection\Todo\UserProjector</listener>
</route>
</router>
</event_bus>
</config>

<srv:services>
<srv:service id="global_plugin" class="ProophTest\Bundle\ServiceBus\DependencyInjection\Fixture\Model\GlobalPlugin">
<srv:tag name="prooph_service_bus.plugin" />
</srv:service>
<srv:service id="command_type_plugin" class="ProophTest\Bundle\ServiceBus\DependencyInjection\Fixture\Model\CommandTypePlugin">
<srv:tag name="prooph_service_bus.command_bus.plugin" />
</srv:service>
<srv:service id="main_command_bus_plugin" class="ProophTest\Bundle\ServiceBus\DependencyInjection\Fixture\Model\MainCommandBusPlugin">
<srv:tag name="prooph_service_bus.main_command_bus.plugin" />
</srv:service>
</srv:services>
</srv:container>
44 changes: 44 additions & 0 deletions test/DependencyInjection/Fixture/config/yml/plugins.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
prooph_service_bus:
command_buses:
main_command_bus:
message_factory: 'prooph_service_bus.message_factory'
router:
type: 'prooph_service_bus.command_bus_router'
routes:
'Prooph\ProophessorDo\Model\User\Command\RegisterUser': 'Prooph\ProophessorDo\Model\User\Handler\RegisterUserHandler'
'Prooph\ProophessorDo\Model\User\Command\UpdateUser': 'Prooph\ProophessorDo\Model\User\Handler\UpdateUserHandler'

# uses default values
second_command_bus:
router:
routes:
'Prooph\ProophessorDo\Model\User\Command\RegisterUser': 'Prooph\ProophessorDo\Model\User\Handler\RegisterUserHandler'
'Prooph\ProophessorDo\Model\User\Command\UpdateUser': 'Prooph\ProophessorDo\Model\User\Handler\UpdateUserHandler'

event_buses:
main_event_bus:
plugins:
- 'Prooph\ServiceBus\Plugin\InvokeStrategy\OnEventStrategy'
message_factory: 'prooph_service_bus.message_factory'
router:
type: 'prooph_service_bus.event_bus_router'
routes:
'Prooph\ProophessorDo\Model\Todo\Event\TodoWasPosted':
- 'Prooph\ProophessorDo\Projection\Todo\TodoProjector'
- 'Prooph\ProophessorDo\Projection\User\UserProjector'

services:
'global_plugin':
class: 'ProophTest\Bundle\ServiceBus\DependencyInjection\Fixture\Model\GlobalPlugin'
tags:
- { name: prooph_service_bus.plugin }

'command_type_plugin':
class: 'ProophTest\Bundle\ServiceBus\DependencyInjection\Fixture\Model\CommandTypePlugin'
tags:
- { name: prooph_service_bus.command_bus.plugin }

'main_command_bus_plugin':
class: 'ProophTest\Bundle\ServiceBus\DependencyInjection\Fixture\Model\MainCommandBusPlugin'
tags:
- { name: prooph_service_bus.main_command_bus.plugin }

0 comments on commit 558cc1e

Please sign in to comment.