Commit 93e632a4 authored by Daniel Król's avatar Daniel Król

Update to narrator v0.3

parent 50088b09
......@@ -36,16 +36,16 @@ class ListenerPass implements \Symfony\Component\DependencyInjection\Compiler\Co
throw new \RuntimeException('The narrator.listener must have event attribute');
}
$eventName = $tag['event'];
$emitterName = isset($tag['emitter']) ? $tag['emitter'] : 'default';
$busName = isset($tag['bus']) ? $tag['bus'] : 'default';
$methodName = isset($tag['method']) ? $tag['method'] : null;
$this->registerListener($container, $eventName, $emitterName, $serviceId, $methodName);
$this->registerListener($container, $eventName, $busName, $serviceId, $methodName);
}
private function registerListener(\Symfony\Component\DependencyInjection\ContainerBuilder $container, $eventName, $emitterName, $listenerServiceId, $methodName)
private function registerListener(\Symfony\Component\DependencyInjection\ContainerBuilder $container, $busName, $emitterName, $listenerServiceId, $methodName)
{
$emitterDefinition = $container->getDefinition('narrator.emitter.' . $emitterName);
$emitterDefinition = $container->getDefinition('narrator.event_bus.' . $emitterName);
$listeners = $emitterDefinition->getArgument(1);
$listeners[$eventName][] = ['serviceId' => $listenerServiceId, 'methodName' => $methodName];
$listeners[$busName][] = ['serviceId' => $listenerServiceId, 'methodName' => $methodName];
$emitterDefinition->replaceArgument(1, $listeners);
}
}
......@@ -7,28 +7,31 @@
namespace Mleko\Narrator\Bundle;
class EmitterFactory
use Mleko\Narrator\EventNameExtractor;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EventBusFactory
{
/**
* @var \Symfony\Component\DependencyInjection\ContainerInterface
* @var ContainerInterface
*/
private $container;
/**
* EmitterFactory constructor.
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* EventBusFactory constructor.
* @param ContainerInterface $container
*/
public function __construct(\Symfony\Component\DependencyInjection\ContainerInterface $container)
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
/**
* @param \Mleko\Narrator\EventNameExtractor $nameExtractor
* @param EventNameExtractor $nameExtractor
* @param $listenerDefinitions
* @return \Mleko\Narrator\SimpleEmitter
* @return \Mleko\Narrator\BasicEventBus
*/
public function createEmitter(\Mleko\Narrator\EventNameExtractor $nameExtractor, $listenerDefinitions)
public function createEventBus(EventNameExtractor $nameExtractor, $listenerDefinitions)
{
$listeners = [];
foreach ($listenerDefinitions as $eventName => $listenerDefinition) {
......@@ -36,7 +39,7 @@ class EmitterFactory
$listeners[$eventName][] = new Listener\ListenerService($listenerData['serviceId'], $this->container, $listenerData['methodName']);
}
}
return new \Mleko\Narrator\SimpleEmitter($nameExtractor, $listeners);
return new \Mleko\Narrator\BasicEventBus($nameExtractor, $listeners);
}
}
......@@ -5,16 +5,16 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="narrator.emitter" alias="narrator.emitter.default"/>
<service id="narrator.emitter.default" class="Mleko\Narrator\SimpleEmitter">
<factory service="narrator.emitter_factory" method="createEmitter"/>
<service id="narrator.event_bus" alias="narrator.event_bus.default"/>
<service id="narrator.event_bus.default" class="Mleko\Narrator\BasicEventBus">
<factory service="narrator.event_bus_factory" method="createEventBus"/>
<argument type="service" id="narrator.name_extractor.class_name"/>
<argument type="collection"/>
</service>
<service id="narrator.name_extractor.class_name" class="Mleko\Narrator\EventNameExtractor\ClassNameExtractor"/>
<service id="narrator.emitter_factory" class="Mleko\Narrator\Bundle\EmitterFactory">
<service id="narrator.event_bus_factory" class="Mleko\Narrator\Bundle\EventBusFactory">
<argument type="service" id="service_container"/>
</service>
</services>
......
......@@ -22,7 +22,7 @@ class ListenerPassTest extends \PHPUnit_Framework_TestCase
$this->container = $this->getMockBuilder(\Symfony\Component\DependencyInjection\ContainerBuilder::class)->getMock();
$this->defaultEmitterDefinition = new \Symfony\Component\DependencyInjection\Definition(null, [null, []]);
$this->container->method('getDefinition')->with('narrator.emitter.default')->willReturn($this->defaultEmitterDefinition);
$this->container->method('getDefinition')->with('narrator.event_bus.default')->willReturn($this->defaultEmitterDefinition);
$this->compilerPass = new \Mleko\Narrator\Bundle\DependencyInjection\Compiler\ListenerPass();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment