Skip to content

Commit

Permalink
adds generic handler
Browse files Browse the repository at this point in the history
  • Loading branch information
nixilla committed Aug 30, 2018
1 parent 5ffd8c1 commit 39ffdb5
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 27 deletions.
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Set your event repository service for persisting events to data store
changeset:
event_repository: '@your.preferred.implementation'
event_bus: '@your.preferred.event_bus.implementation'
command_handler: '@your.preferred.command_handler.implementation'

```

Expand All @@ -50,9 +51,4 @@ Wire your command handlers

services:

AppBundle\Command\Handler\:
resource: '%kernel.root_dir%/../src/AppBundle/Command/Handler'
public: true
autowire: true
tags: [ 'changeset.command.handler' ]
```
14 changes: 7 additions & 7 deletions src/DependencyInjection/ChangesetExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ class ChangesetExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$this->processConfiguration(new Configuration(), $configs);

foreach($configs[0] as $key => $id)
{
if(strpos(trim($id), '@') == 0) $id = substr($id, 1);
$container->setAlias(sprintf('changeset.%s', $key), $id);
}
$config = $this->processConfiguration(new Configuration(), $configs);

$loader = new Loader\YamlFileLoader(
$container,
new FileLocator(__DIR__ . '/../Resources/config')
);

$loader->load('services.yml');

foreach($config as $key => $id)
{
if(strpos(trim($id), '@') == 0) $id = substr($id, 1);
$container->setAlias(sprintf('changeset.%s', $key), $id);
}
}
}
12 changes: 1 addition & 11 deletions src/DependencyInjection/Compiler/HandlerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@ public function process(ContainerBuilder $container)
$definition = $container->findDefinition(CommandBusInterface::class);

$definition->addMethodCall('setEventBus', [ new Reference('changeset.event_bus' )]);

$taggedServices = $container->findTaggedServiceIds('changeset.command.handler');

foreach ($taggedServices as $id => $tags)
{
$definition->addMethodCall('addHandler', [ new Reference($id) ]);

$container
->findDefinition($id)
->addMethodCall('setRepository', [ new Reference('changeset.event_repository') ]);
}
$definition->addMethodCall('setHandler', [ new Reference('changeset.command_handler' )]);
}
}
10 changes: 6 additions & 4 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ public function getConfigTreeBuilder()
$rootNode = $treeBuilder->root('changeset');

$rootNode
->addDefaultsIfNotSet()
->children()
->scalarNode('event_repository')
->isRequired()
->cannotBeEmpty()
->defaultValue('@Changeset\Event\RepositoryInterface')
->end()
->scalarNode('event_bus')
->isRequired()
->cannotBeEmpty()
->defaultValue('@Changeset\Communication\EventBusInterface')
->end()
->scalarNode('command_handler')
->defaultValue('@Changeset\Command\HandlerInterface')
->end()
->end()
;
Expand Down
7 changes: 7 additions & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
services:

Changeset\Command\HandlerInterface:
public: true
class: Changeset\Command\Handler
arguments: [ '@event_dispatcher']

Changeset\Communication\CommandBusInterface:
public: true
class: Changeset\Communication\InMemoryCommandBus
calls:
- { method: 'setHandler', arguments: [ '@changeset.command_handler' ]}

Changeset\Communication\EventBusInterface:
public: true
Expand Down

0 comments on commit 39ffdb5

Please sign in to comment.