From a4247e1525ed5783f801436b5d89ea2be994351a Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Wed, 30 Mar 2016 13:28:49 -0500 Subject: [PATCH] Do not raise an exception if a given plugin manager is not defined. The `ServiceListener::onLoadModulesPost()` listener raises an exception if a given plugin manager instance is unavailable, and does not exist in the application service manager. This impacts migrating the various plugin managers to their respective components: - If they are registered as part of the module configuration, but that component is not present, then an exception is raised. - We cannot add delegator factories on the `ServiceListener` from component module classes, as the `ServiceListener` is retrieved *before* loading modules. One solution is to assume that if the plugin manager is not present, nothing needs to be done. This allows components to map the plugin loader services they expose via their `Module` classes, ensuring that once the listener triggers, it *can* access the service and configure it. Ideally, however, the component should also be able to notify the `ServiceListener` of plugin managers; hopefully a later commit can address that problem. --- src/Listener/ServiceListener.php | 6 ++-- test/Listener/ServiceListenerTest.php | 29 +++++++++++++++++++ .../TestAsset/UndefinedProviderInterface.php | 13 +++++++++ 3 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 test/Listener/TestAsset/UndefinedProviderInterface.php diff --git a/src/Listener/ServiceListener.php b/src/Listener/ServiceListener.php index d9f8e5c..1959b24 100644 --- a/src/Listener/ServiceListener.php +++ b/src/Listener/ServiceListener.php @@ -199,10 +199,8 @@ public function onLoadModulesPost(ModuleEvent $e) if (! $sm['service_manager'] instanceof ServiceManager) { if (! $this->defaultServiceManager->has($sm['service_manager'])) { - throw new Exception\RuntimeException(sprintf( - 'Could not find a valid ServiceManager for %s', - $sm['service_manager'] - )); + // No plugin manager registered by that name; nothing to configure. + continue; } $instance = $this->defaultServiceManager->get($sm['service_manager']); diff --git a/test/Listener/ServiceListenerTest.php b/test/Listener/ServiceListenerTest.php index 83e768b..ae8a014 100644 --- a/test/Listener/ServiceListenerTest.php +++ b/test/Listener/ServiceListenerTest.php @@ -398,4 +398,33 @@ public function testListenerCanOverrideServicesInServiceManagers() $this->assertEquals(['foo' => 'bar'], $services->get('config'), 'Config service was not overridden'); $this->assertInstanceOf(stdClass::class, $services->get('foo'), 'Foo service was not overridden'); } + + public function testOnLoadModulesPostShouldNotRaiseExceptionIfNamedServiceManagerDoesNotExist() + { + $services = new ServiceManager(); + $services->setService('config', []); + $listener = new ServiceListener($services); + $listener->addServiceManager( + 'UndefinedPluginManager', + 'undefined', + TestAsset\UndefinedProviderInterface::class, + 'getUndefinedConfig' + ); + + $module = new TestAsset\ServiceProviderModule([]); + + $event = new ModuleEvent(); + $configListener = new ConfigListener(); + $event->setConfigListener($configListener); + + $event->setModule($module); + $listener->onLoadModule($event); + + try { + $listener->onLoadModulesPost($event); + $this->assertFalse($services->has('UndefinedPluginManager')); + } catch (\Exception $e) { + $this->fail('Exception should not be raised when encountering unknown plugin manager services'); + } + } } diff --git a/test/Listener/TestAsset/UndefinedProviderInterface.php b/test/Listener/TestAsset/UndefinedProviderInterface.php new file mode 100644 index 0000000..4720089 --- /dev/null +++ b/test/Listener/TestAsset/UndefinedProviderInterface.php @@ -0,0 +1,13 @@ +