Skip to content

Commit ad7d87f

Browse files
authored
Merge pull request #8 from prgTW/feat/custom-event-strategy
Support for custom event publishing strategy
2 parents 41cb103 + da995ef commit ad7d87f

4 files changed

Lines changed: 49 additions & 17 deletions

File tree

doc/getting_started.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,22 @@ This will log consumed messages to the `asynchronous_command_bus` and `asynchron
7171

7272
## Choose event strategy
7373

74-
When handling events you have two strategies to choose from. Either you publish *all* events to the message queue or
75-
you only publish the events that have a registered subscriber. If your application is the only one that consuming messages
76-
you should consider using the **predefined** strategy. This will reduce the message overhead on the message queue.
74+
When handling events you have two predefined strategies to choose from. Either you publish *all* events to the message
75+
queue (*always* strategy) or you only publish the events that have a registered asynchronous subscriber
76+
(*predefined* strategy). If your application is the only one that is consuming messages you should consider using the
77+
**predefined** strategy. This will reduce the message overhead on the message queue.
7778

7879
```yaml
7980
simple_bus_asynchronous:
8081
events:
8182
strategy: 'predefined' # default: 'always'
8283
```
84+
85+
You can also use Your own strategy by defining custom **strategy_service_id**
86+
87+
```yaml
88+
simple_bus_asynchronous:
89+
events:
90+
strategy:
91+
strategy_service_id: your_strategy_service
92+
```

src/DependencyInjection/Configuration.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,27 @@ public function getConfigTreeBuilder()
4343
->arrayNode('events')
4444
->canBeEnabled()
4545
->children()
46-
->enumNode('strategy')
46+
->arrayNode('strategy')
47+
->addDefaultsIfNotSet()
48+
->beforeNormalization()
49+
->ifInArray(['always', 'predefined'])
50+
->then(function ($v) {
51+
$map = [
52+
'always' => 'simple_bus.asynchronous.always_publishes_messages_middleware',
53+
'predefined' => 'simple_bus.asynchronous.publishes_predefined_messages_middleware',
54+
];
55+
56+
return [
57+
'strategy_service_id' => $map[$v]
58+
];
59+
})
60+
->end()
4761
->info('What strategy to use to publish messages')
48-
->defaultValue('always')
49-
->values(['always', 'predefined'])
62+
->children()
63+
->scalarNode('strategy_service_id')
64+
->defaultValue('simple_bus.asynchronous.always_publishes_messages_middleware')
65+
->end()
66+
->end()
5067
->end()
5168
->scalarNode('publisher_service_id')
5269
->info('Service id of an instance of Publisher')

src/DependencyInjection/SimpleBusAsynchronousExtension.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,7 @@ private function loadAsynchronousEventBus(array $config, ContainerBuilder $conta
9696
$loader->load('asynchronous_events_logging.yml');
9797
}
9898

99-
100-
if ($config['strategy'] === 'always') {
101-
$eventMiddleware = 'simple_bus.asynchronous.always_publishes_messages_middleware';
102-
} else {
103-
$eventMiddleware = 'simple_bus.asynchronous.publishes_predefined_messages_middleware';
104-
}
99+
$eventMiddleware = $config['strategy']['strategy_service_id'];
105100

106101
// insert before the middleware that actually notifies a message subscriber of the message
107102
$container->getDefinition($eventMiddleware)->addTag('event_bus_middleware', ['priority' => 0]);

tests/Unit/DepencencyInjection/SimpleBusAsynchronousExtensionTest.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
77
use SimpleBus\AsynchronousBundle\DependencyInjection\SimpleBusAsynchronousExtension;
8-
use Symfony\Component\DependencyInjection\ContainerBuilder;
98

109
class SimpleBusAsynchronousExtensionTest extends AbstractExtensionTestCase
1110
{
@@ -25,7 +24,7 @@ protected function getMinimalConfiguration()
2524
/**
2625
* @test
2726
*/
28-
public function it_uses_strategy_allways_by_default()
27+
public function it_uses_strategy_always_by_default()
2928
{
3029
$this->container->setParameter('kernel.bundles', ['SimpleBusCommandBusBundle'=>true, 'SimpleBusEventBusBundle'=>true]);
3130
$this->load();
@@ -43,7 +42,18 @@ public function it_uses_strategy_predefined_when_configured()
4342

4443
$this->assertContainerBuilderHasServiceDefinitionWithTag('simple_bus.asynchronous.publishes_predefined_messages_middleware', 'event_bus_middleware', ['priority'=>0]);
4544
}
46-
45+
46+
/**
47+
* @test
48+
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
49+
* @expectedExceptionMessageRegExp ".*custom_strategy.*"
50+
*/
51+
public function it_uses_custom_strategy_when_configured()
52+
{
53+
$this->container->setParameter('kernel.bundles', ['SimpleBusCommandBusBundle'=>true, 'SimpleBusEventBusBundle'=>true]);
54+
$this->load(['events'=>['strategy'=>['strategy_service_id'=>'custom_strategy']]]);
55+
}
56+
4757
/**
4858
* @test
4959
* @expectedException \LogicException
@@ -54,7 +64,7 @@ public function it_throws_exception_if_command_bus_bundle_is_missing()
5464
$this->container->setParameter('kernel.bundles', ['SimpleBusEventBusBundle'=>true]);
5565
$this->load(['events'=>['strategy'=>'predefined']]);
5666
}
57-
67+
5868
/**
5969
* @test
6070
* @expectedException \LogicException
@@ -65,4 +75,4 @@ public function it_throws_exception_if_event_bus_bundle_is_missing()
6575
$this->container->setParameter('kernel.bundles', ['SimpleBusCommandBusBundle'=>true]);
6676
$this->load(['events'=>['strategy'=>'predefined']]);
6777
}
68-
}
78+
}

0 commit comments

Comments
 (0)