...
 
Commits (3)
language: php
php:
- 5.6
- 7.0
- 7.1
- 7.2
......
......@@ -17,7 +17,7 @@
}
],
"require": {
"php": "^7.0",
"php": "^5.6|^7.0",
"narrator/narrator": "^0.4",
"symfony/config": "^2.3|^3.0",
"symfony/dependency-injection": "^2.3|^3.0",
......
......@@ -51,6 +51,13 @@ class ListenerPass implements \Symfony\Component\DependencyInjection\Compiler\Co
$this->registerListener($container, $eventName, $busName, $serviceId, $methodName);
}
/**
* @param ContainerBuilder $container
* @param string $eventName
* @param string $emitterName
* @param string $listenerServiceId
* @param string $methodName
*/
private function registerListener(ContainerBuilder $container, $eventName, $emitterName, $listenerServiceId, $methodName)
{
$emitterDefinition = $container->getDefinition('narrator.event_bus.' . $emitterName);
......@@ -66,7 +73,14 @@ class ListenerPass implements \Symfony\Component\DependencyInjection\Compiler\Co
$emitterDefinition->replaceArgument(1, $listeners);
}
private function extractEventName($container, $tag, $serviceId, $methodName)
/**
* @param ContainerBuilder $container
* @param array $tag
* @param string $serviceId
* @param string $methodName
* @return null|string
*/
private function extractEventName(ContainerBuilder $container, $tag, $serviceId, $methodName)
{
if (!isset($tag['event'])) {
return $this->inferEventName($container, $serviceId, $methodName);
......@@ -75,6 +89,12 @@ class ListenerPass implements \Symfony\Component\DependencyInjection\Compiler\Co
return $tag['event'];
}
/**
* @param ContainerBuilder $container
* @param string $serviceId
* @param string $methodName
* @return null|string
*/
private function inferEventName(ContainerBuilder $container, $serviceId, $methodName)
{
$listenerClass = $container->getDefinition($serviceId)->getClass();
......@@ -87,10 +107,10 @@ class ListenerPass implements \Symfony\Component\DependencyInjection\Compiler\Co
return null;
}
$firstParameter = $parameters[0];
$firstParameterType = $firstParameter->getType();
$firstParameterType = $firstParameter->getClass();
if (null === $firstParameterType) {
return null;
}
return (string)$firstParameterType;
return $firstParameterType->getName();
}
}
......@@ -14,6 +14,7 @@ namespace Mleko\Narrator\Bundle\DependencyInjection\Configuration;
use Mleko\Narrator\BasicEventBus;
use Mleko\Narrator\ListenerResolver\InstanceOfResolver;
use Mleko\Narrator\ListenerResolver\NameBasedResolver;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
......@@ -29,11 +30,13 @@ class EventBusConfiguration
foreach ($buses as $busName => $busConfig) {
$resolverServiceId = "narrator.event_bus.$busName.resolver";
$resolverConfig = isset($busConfig['resolver']) ? $busConfig['resolver'] : [];
$container->setDefinition(
$resolverServiceId,
$this->buildResolver($resolverConfig)
);
$resolver = $this->buildResolver($resolverConfig);
if ($resolver instanceof Definition) {
$container->setDefinition($resolverServiceId, $resolver);
} elseif ($resolver instanceof Alias) {
$container->setAlias($resolverServiceId, $resolver);
}
$container->setDefinition(
"narrator.event_bus.$busName",
new Definition(
......@@ -47,6 +50,10 @@ class EventBusConfiguration
}
}
/**
* @param array $resolverConfig
* @return Alias|Definition
*/
private function buildResolver($resolverConfig)
{
$resolverType = isset($resolverConfig['type']) ? $resolverConfig['type'] : 'name';
......@@ -54,7 +61,7 @@ class EventBusConfiguration
case 'instanceof':
return new Definition(InstanceOfResolver::class);
case 'service':
return new Reference($resolverType['service_id']);
return new Alias($resolverConfig['service_id'], false);
default:
$nameExtractorId = isset($resolverConfig['name_extractor']) ? $resolverConfig['name_extractor'] : "narrator.name_extractor.class_name";
return new Definition(
......
......@@ -11,7 +11,9 @@
namespace Mleko\Narrator\Bundle\Tests\Integration;
use Mleko\Narrator\EventBus;
use Mleko\Narrator\EventEmitter;
use Mleko\Narrator\Listener\EventTrap;
use org\bovigo\vfs\vfsStream;
/**
......@@ -80,4 +82,32 @@ EOF
/** @var EventEmitter $eventEmitter */
$this->assertNotNull($container->get("narrator.event_bus.inheritance"));
}
public function testServiceResolver()
{
$servicesConfig = vfsStream::newFile("config.yml");
$servicesConfig->setContent(<<<'EOF'
services:
custom_resolver:
class: Mleko\Narrator\ListenerResolver\InstanceOfResolver
narrator:
event_bus:
default:
resolver:
type: service
service_id: custom_resolver
EOF
);
$this->root->addChild($servicesConfig);
$this->kernel->setConfigPath($servicesConfig->url());
$this->kernel->boot();
$container = $this->kernel->getContainer();
$this->assertTrue($container->has("narrator.event_bus.default"));
/** @var EventBus $bus */
$bus = $container->get("narrator.event_bus.default");
$bus->subscribe(\Exception::class, $trap = new EventTrap());
$bus->emit($e = new \RuntimeException());
$this->assertContains($e, $trap->getTrappedEvents());
}
}