Skip to content

Commit

Permalink
Merge pull request #3 from mleko/feature/bus-visibility
Browse files Browse the repository at this point in the history
Make buses public by default, allow modification via config
  • Loading branch information
mleko authored Jul 31, 2018
2 parents 59d0034 + 81562c6 commit 5c58094
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 15 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,10 @@ narrator:
default:
resolver:
type: instanceof
public: true
named: ~
```
This configuration defines two buses: "default" and "named". These buses will be registered as `narrator.event_bus.default` and `narrator.event_bus.named`.
`narrator.event_bus.default` will use `InstanceOf` resolver, therefore it will support event inheritance;
`narrator.event_bus.named` will use default configuration based on strict event name comparison.
By default all buses are registered as public services, it is possible to change that on per-bus basis using `public` parameter.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
"require": {
"php": "^5.6|^7.0",
"narrator/narrator": "^0.4",
"symfony/config": "^2.3|^3.0",
"symfony/dependency-injection": "^2.3|^3.0",
"symfony/http-kernel": "^2.3|^3.0"
"symfony/config": "^2.3|^3.0|^4.0",
"symfony/dependency-injection": "^2.3|^3.0|^4.0",
"symfony/http-kernel": "^2.3|^3.0|^4.0"
},
"require-dev": {
"mikey179/vfsStream": "^1.6",
Expand Down
7 changes: 5 additions & 2 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ public function getConfigTreeBuilder()
$rootNode
->children()
->arrayNode('event_bus')
->defaultValue(['default' => ['resolver' => ['type' => 'name', "name_extractor" => "narrator.name_extractor.class_name"]]])
->defaultValue(['default' => ['resolver' => ['type' => 'name', "name_extractor" => "narrator.name_extractor.class_name"], 'public' => true]])
->requiresAtLeastOneElement()
->useAttributeAsKey('name')
->prototype("array")
->children()
->children()
->booleanNode('public')
->defaultValue(true)
->end()
->arrayNode('resolver')
->children()
->enumNode('type')
Expand Down
17 changes: 8 additions & 9 deletions src/DependencyInjection/Configuration/EventBusConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,15 @@ public function configureEventBus($buses, ContainerBuilder $container)
} elseif ($resolver instanceof Alias) {
$container->setAlias($resolverServiceId, $resolver);
}
$container->setDefinition(
"narrator.event_bus.$busName",
new Definition(
BasicEventBus::class,
[
new Reference($resolverServiceId),
[]
]
)
$busDefinition = new Definition(
BasicEventBus::class,
[
new Reference($resolverServiceId),
[]
]
);
$busDefinition->setPublic($busConfig['public']);
$container->setDefinition("narrator.event_bus.$busName", $busDefinition);
}
}

Expand Down
33 changes: 32 additions & 1 deletion tests/Integration/SimpleEventListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function testDefaultEventBus()
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service class="Mleko\Narrator\Bundle\Tests\Integration\TestApp\Counter" id="narrator.listener.counter">
<service class="Mleko\Narrator\Bundle\Tests\Integration\TestApp\Counter" id="narrator.listener.counter" public="true">
<tag name="narrator.listener" event="stdClass"/>
</service>
</services>
Expand Down Expand Up @@ -90,6 +90,9 @@ public function testServiceResolver()
services:
custom_resolver:
class: Mleko\Narrator\ListenerResolver\InstanceOfResolver
private_service:
class: stdClass
public: false
narrator:
event_bus:
default:
Expand All @@ -109,5 +112,33 @@ class: Mleko\Narrator\ListenerResolver\InstanceOfResolver
$bus->subscribe(\Exception::class, $trap = new EventTrap());
$bus->emit($e = new \RuntimeException());
$this->assertContains($e, $trap->getTrappedEvents());
$this->assertFalse($container->has("private_service"));
}

public function testPrivateBus()
{
$servicesConfig = vfsStream::newFile("config.yml");
$servicesConfig->setContent(<<<'EOF'
services:
test:
alias: narrator.event_bus.default
public: true
narrator:
event_bus:
default:
resolver:
type: instanceof
public: false
EOF
);
$this->root->addChild($servicesConfig);
$this->kernel->setConfigPath($servicesConfig->url());
$this->kernel->boot();

$container = $this->kernel->getContainer();
$this->assertFalse($container->has("narrator.event_bus.default"), "Bus should not be visible directly");
$this->assertTrue($container->has("test"), "Bus should available via alias");
$bus = $container->get("test");
$this->assertTrue($bus instanceof EventBus);
}
}

0 comments on commit 5c58094

Please sign in to comment.