Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid array_merge due to appending #164

Merged
merged 2 commits into from
Jul 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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']));
}
}