Skip to content

Commit

Permalink
Merge pull request #164 from basz/fix-listener-appending
Browse files Browse the repository at this point in the history
avoid array_merge due to appending
  • Loading branch information
prolic authored Jul 2, 2017
2 parents 9c58750 + c4f4bff commit 2f8a9a7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Plugin/ServiceLocatorPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ function (ActionEvent $actionEvent): void {
$currentEventListeners = $actionEvent->getParam(EventBus::EVENT_PARAM_EVENT_LISTENERS, []);
$newEventListeners = [];

foreach ($currentEventListeners as $eventListenerAlias) {
foreach ($currentEventListeners as $key => $eventListenerAlias) {
if (is_string($eventListenerAlias) && $this->serviceLocator->has($eventListenerAlias)) {
$newEventListeners[] = $this->serviceLocator->get($eventListenerAlias);
$newEventListeners[$key] = $this->serviceLocator->get($eventListenerAlias);
}
}

$actionEvent->setParam(EventBus::EVENT_PARAM_EVENT_LISTENERS, array_merge($currentEventListeners, $newEventListeners));
// merge array whilst preserving numeric keys and giving priority to newEventListeners
$actionEvent->setParam(EventBus::EVENT_PARAM_EVENT_LISTENERS, $newEventListeners + $currentEventListeners);
},
MessageBus::PRIORITY_LOCATE_HANDLER
);
Expand Down
38 changes: 38 additions & 0 deletions tests/Plugin/ServiceLocatorPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,42 @@ public function it_doesnt_override_previous_event_handlers(): void
$this->assertTrue($handledOne);
$this->assertSame(1, $handlerTwo->getInvokeCounter());
}

/**
* @test
*/
public function make_sure_servicenames_do_not_end_up_as_listener_instance(): void
{
$handlerOne = new MessageHandler();
$handlerTwo = new MessageHandler();
$eventBus = new EventBus();
$router = new EventRouter();

$container = $this->prophesize(ContainerInterface::class);

$container->has('handler-one')->willReturn(true)->shouldBeCalled();
$container->get('handler-one')->willReturn($handlerOne)->shouldBeCalled();

$container->has('handler-two')->willReturn(true)->shouldBeCalled();
$container->get('handler-two')->willReturn($handlerTwo)->shouldBeCalled();

$router->route(SomethingDone::class)->to('handler-one');
$router->route(SomethingDone::class)->to('handler-two');

$router->attachToMessageBus($eventBus);

$locatorPlugin = new ServiceLocatorPlugin($container->reveal());

$locatorPlugin->attachToMessageBus($eventBus);

$eventBus->attach(EventBus::EVENT_DISPATCH,
function (ActionEvent $actionEvent): void {
$listeners = $actionEvent->getParam(EventBus::EVENT_PARAM_EVENT_LISTENERS);

$this->assertCount(2, $listeners);
$this->assertContainsOnly(MessageHandler::class, $listeners);
}, PHP_INT_MIN);

$eventBus->dispatch(new SomethingDone(['foo' => 'bar']));
}
}

0 comments on commit 2f8a9a7

Please sign in to comment.