-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for symphony/dependency-injection
- Loading branch information
Showing
9 changed files
with
297 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ICanBoogie package. | ||
* | ||
* (c) Olivier Laviale <olivier.laviale@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ICanBoogie\MessageBus\Symfony; | ||
|
||
use ICanBoogie\MessageBus\PSR\ContainerMessageHandlerProvider; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; | ||
use Symfony\Component\DependencyInjection\Exception\LogicException; | ||
use Symfony\Component\DependencyInjection\TypedReference; | ||
|
||
/** | ||
* Registers command bus handlers. | ||
*/ | ||
class AddCommandBusPass implements CompilerPassInterface | ||
{ | ||
const DEFAULT_PROVIDER_SERVICE = 'message_bus.message_handler_provider'; | ||
const DEFAULT_HANDLER_TAG = 'message_bus.handler'; | ||
const DEFAULT_MESSAGE_PROPERTY = 'message'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $providerId; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $handlerTag; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $messageProperty; | ||
|
||
/** | ||
* @param string $providerId | ||
* @param string $handlerTag | ||
* @param string $messageProperty | ||
*/ | ||
public function __construct( | ||
$providerId = self::DEFAULT_PROVIDER_SERVICE, | ||
$handlerTag = self::DEFAULT_HANDLER_TAG, | ||
$messageProperty = self::DEFAULT_MESSAGE_PROPERTY | ||
) { | ||
$this->providerId = $providerId; | ||
$this->handlerTag = $handlerTag; | ||
$this->messageProperty = $messageProperty; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
$handlers = $container->findTaggedServiceIds($this->handlerTag, true); | ||
$messageProperty = $this->messageProperty; | ||
$mapping = []; | ||
$handlerRefs = []; | ||
|
||
foreach ($handlers as $id => $tags) { | ||
if (empty($tags[0][$messageProperty])) | ||
{ | ||
throw new InvalidArgumentException( | ||
"The `$messageProperty` property is required for service `$id`." | ||
); | ||
} | ||
|
||
$command = $tags[0][$messageProperty]; | ||
|
||
if (isset($mapping[$command])) | ||
{ | ||
throw new LogicException( | ||
"The command `$command` already has an handler: `{$mapping[$command]}`." | ||
); | ||
} | ||
|
||
$mapping[$command] = $id; | ||
$handlerRefs[$id] = new TypedReference($id, $container->getDefinition($id)->getClass()); | ||
} | ||
|
||
$container | ||
->register($this->providerId, ContainerMessageHandlerProvider::class) | ||
->setArguments([ | ||
$mapping, | ||
ServiceLocatorTagPass::register($container, $handlerRefs) | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace ICanBoogie\MessageBus; | ||
|
||
class HandlerA | ||
{ | ||
public function __invoke() | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace ICanBoogie\MessageBus; | ||
|
||
class HandlerB | ||
{ | ||
public function __invoke() | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ICanBoogie package. | ||
* | ||
* (c) Olivier Laviale <olivier.laviale@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ICanBoogie\MessageBus\Symfony; | ||
|
||
use ICanBoogie\MessageBus\HandlerA; | ||
use ICanBoogie\MessageBus\HandlerB; | ||
use ICanBoogie\MessageBus\MessageA; | ||
use ICanBoogie\MessageBus\MessageB; | ||
use ICanBoogie\MessageBus\PSR\ContainerMessageHandlerProvider; | ||
use Symfony\Component\Config\FileLocator; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder as SymfonyContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; | ||
|
||
class AddCommandBusPassTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @expectedException \InvalidArgumentException | ||
* @expectedExceptionMessage The `message` property is required for service `handler.message_a` | ||
*/ | ||
public function test_should_error_on_missing_message() | ||
{ | ||
$this->makeContainer(__DIR__ . '/resources/missing-message.yml'); | ||
} | ||
|
||
/** | ||
* @expectedException \LogicException | ||
* @expectedExceptionMessage The command `ICanBoogie\MessageBus\MessageA` already has an handler: `handler.message_a`. | ||
*/ | ||
public function test_should_error_message_duplicated() | ||
{ | ||
$this->makeContainer(__DIR__ . '/resources/message-duplicate.yml'); | ||
} | ||
|
||
public function test_should_return_handler() | ||
{ | ||
/* @var ContainerMessageHandlerProvider $provider */ | ||
$container = $this->makeContainer(__DIR__ . '/resources/ok.yml'); | ||
$provider = $container->get(AddCommandBusPass::DEFAULT_PROVIDER_SERVICE); | ||
|
||
$this->assertInstanceOf(ContainerMessageHandlerProvider::class, $provider); | ||
$this->assertInstanceOf(HandlerA::class, $provider(new MessageA())); | ||
$this->assertInstanceOf(HandlerB::class, $provider(new MessageB())); | ||
} | ||
|
||
/** | ||
* @param string $config | ||
* | ||
* @return SymfonyContainerBuilder | ||
*/ | ||
private function makeContainer($config) | ||
{ | ||
$container = new SymfonyContainerBuilder(); | ||
$loader = new YamlFileLoader($container, new FileLocator(__DIR__)); | ||
$loader->load($config); | ||
$container->addCompilerPass(new AddCommandBusPass); | ||
$container->compile(); | ||
|
||
return $container; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
services: | ||
|
||
handler.message_a: | ||
class: ICanBoogie\MessageBus\HandlerA | ||
tags: | ||
- name: message_bus.handler | ||
message: ICanBoogie\MessageBus\MessageA | ||
|
||
handler.message_b: | ||
class: ICanBoogie\MessageBus\HandlerB | ||
tags: | ||
- name: message_bus.handler | ||
message: ICanBoogie\MessageBus\MessageA |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
services: | ||
|
||
handler.message_a: | ||
class: ICanBoogie\MessageBus\HandlerA | ||
tags: | ||
- name: message_bus.handler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
services: | ||
|
||
handler.message_a: | ||
class: ICanBoogie\MessageBus\HandlerA | ||
tags: | ||
- name: message_bus.handler | ||
message: ICanBoogie\MessageBus\MessageA | ||
|
||
handler.message_b: | ||
class: ICanBoogie\MessageBus\HandlerB | ||
tags: | ||
- name: message_bus.handler | ||
message: ICanBoogie\MessageBus\MessageB |