...
 
Commits (5)
......@@ -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 private services, it is possible to change that on per-bus basis using `public` parameter.
......@@ -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",
......
......@@ -31,11 +31,14 @@ class Configuration implements ConfigurationInterface
$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' => false]])
->requiresAtLeastOneElement()
->useAttributeAsKey('name')
->prototype("array")
->children()
->children()
->booleanNode('public')
->defaultValue(false)
->end()
->arrayNode('resolver')
->children()
->enumNode('type')
......
......@@ -37,16 +37,15 @@ class EventBusConfiguration
} 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);
}
}
......
......@@ -23,6 +23,16 @@ class SimpleEventListenerTest extends IntegrationTest
{
public function testDefaultEventBus()
{
$config = vfsStream::newFile("config.yml");
$config->setContent(<<<'EOF'
imports:
- { resource: services.xml }
narrator:
event_bus:
default:
public: true
EOF
);
$servicesConfig = vfsStream::newFile("services.xml");
$servicesConfig->setContent(<<<'EOF'
<?xml version="1.0" encoding="UTF-8" ?>
......@@ -31,15 +41,16 @@ class SimpleEventListenerTest extends IntegrationTest
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>
</container>
EOF
);
$this->root->addChild($config);
$this->root->addChild($servicesConfig);
$this->kernel->setConfigPath($servicesConfig->url());
$this->kernel->setConfigPath($config->url());
$this->kernel->boot();
$container = $this->kernel->getContainer();
......@@ -71,6 +82,7 @@ narrator:
inheritance:
resolver:
type: instanceof
public: true
EOF
);
$this->root->addChild($servicesConfig);
......@@ -90,12 +102,16 @@ EOF
services:
custom_resolver:
class: Mleko\Narrator\ListenerResolver\InstanceOfResolver
private_service:
class: stdClass
public: false
narrator:
event_bus:
default:
resolver:
type: service
service_id: custom_resolver
public: true
EOF
);
$this->root->addChild($servicesConfig);
......@@ -109,5 +125,33 @@ EOF
$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);
}
}