Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
The ServiceListener needs to be able to overwrite existing configuration
Browse files Browse the repository at this point in the history
In testing zend-mvc for forwards compatibility, I ran into an issue with
the ServiceListener: if a service was already configured when the
ServiceListener ran, an exception was thrown due to being unable to
overwrite services. This should always be allowed in the
ServiceListener.
  • Loading branch information
weierophinney committed Feb 28, 2016
1 parent cfcdf32 commit e89b921
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Listener/ServiceListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,15 @@ public function onLoadModulesPost(ModuleEvent $e)
}

$serviceConfig = new ServiceConfig($smConfig);

// The service listener is meant to operate during bootstrap, and, as such,
// needs to be able to override existing configuration.
$allowOverride = $sm['service_manager']->getAllowOverride();
$sm['service_manager']->setAllowOverride(true);

$serviceConfig->configureServiceManager($sm['service_manager']);

$sm['service_manager']->setAllowOverride($allowOverride);
}
}

Expand Down
40 changes: 40 additions & 0 deletions test/Listener/ServiceListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,4 +358,44 @@ public function testCanDetachListeners(array $dependencies)
$listeners = $this->getArrayOfListenersForEvent(ModuleEvent::EVENT_LOAD_MODULES_POST, $events);
$this->assertCount(0, $listeners);
}

public function testListenerCanOverrideServicesInServiceManagers()
{
$services = new ServiceManager();
$services->setService('config', []);
$services->setFactory('foo', function ($services) {
return $services;
});
$listener = new ServiceListener($services);
$listener->addServiceManager(
$services,
'service_manager',
ServiceProviderInterface::class,
'getServiceConfig'
);

$module = new TestAsset\ServiceProviderModule([
'services' => [
'config' => [ 'foo' => 'bar'],
],
'factories' => [
'foo' => function ($services) {
return new stdClass();
},
],
]);

$event = new ModuleEvent();
$configListener = new ConfigListener();
$event->setConfigListener($configListener);

$event->setModule($module);
$listener->onLoadModule($event);
$listener->onLoadModulesPost($event);

$this->assertTrue($services->has('config'));
$this->assertTrue($services->has('foo'));
$this->assertEquals(['foo' => 'bar'], $services->get('config'), 'Config service was not overridden');
$this->assertInstanceOf(stdClass::class, $services->get('foo'), 'Foo service was not overridden');
}
}

0 comments on commit e89b921

Please sign in to comment.