From 99e42122f223a18de03ec14bca4a189c88d73b62 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Mon, 29 Feb 2016 13:33:47 -0600 Subject: [PATCH] Better ServiceLocatorAwareInterface duck typing Do not check if ServiceLocatorAwareInterface exists, as that will skip the initializer when it does, but the instance does not implement it and *does* fit duck typing rules. --- src/Controller/ControllerManager.php | 8 ++++++-- src/Service/ServiceManagerConfig.php | 2 +- test/Service/ServiceManagerConfigTest.php | 6 +++--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Controller/ControllerManager.php b/src/Controller/ControllerManager.php index 7471ec429..af85c08ae 100644 --- a/src/Controller/ControllerManager.php +++ b/src/Controller/ControllerManager.php @@ -54,8 +54,11 @@ public function __construct($configOrContainerInstance, array $v3config = []) $this->addInitializer([$this, 'injectEventManager']); $this->addInitializer([$this, 'injectConsole']); $this->addInitializer([$this, 'injectPluginManager']); - $this->addInitializer([$this, 'injectServiceLocator']); parent::__construct($configOrContainerInstance, $v3config); + + // Added after parent construction, as v2 abstract plugin managers add + // one during construction. + $this->addInitializer([$this, 'injectServiceLocator']); } /** @@ -204,6 +207,7 @@ public function injectPluginManager($first, $second) */ public function injectServiceLocator($first, $second) { + printf("In %s\n", __METHOD__); if ($first instanceof ContainerInterface) { $container = $first; $controller = $second; @@ -217,7 +221,7 @@ public function injectServiceLocator($first, $second) $container = $container->getServiceLocator() ?: $container; } - if (! interface_exists(ServiceLocatorAwareInterface::class) + if (! $controller instanceof ServiceLocatorAwareInterface && method_exists($controller, 'setServiceLocator') ) { trigger_error(sprintf( diff --git a/src/Service/ServiceManagerConfig.php b/src/Service/ServiceManagerConfig.php index b502ac08e..b20dc6aec 100644 --- a/src/Service/ServiceManagerConfig.php +++ b/src/Service/ServiceManagerConfig.php @@ -142,7 +142,7 @@ public function __construct(array $config = []) $instance->setServiceLocator($container); } - if (! interface_exists(ServiceLocatorAwareInterface::class) + if (! $instance instanceof ServiceLocatorAwareInterface && method_exists($instance, 'setServiceLocator') ) { trigger_error(sprintf( diff --git a/test/Service/ServiceManagerConfigTest.php b/test/Service/ServiceManagerConfigTest.php index 2b9940f53..13ea77059 100644 --- a/test/Service/ServiceManagerConfigTest.php +++ b/test/Service/ServiceManagerConfigTest.php @@ -219,10 +219,10 @@ public function testEventManagerInitializerCanBeReplaced() public function testServiceLocatorAwareInitializerInjectsDuckTypedImplementations() { - $serviceManager = new ServiceManager(['factories' => [ + $serviceManager = new ServiceManager(); + (new ServiceManagerConfig(['factories' => [ TestAsset\DuckTypedServiceLocatorAware::class => InvokableFactory::class, - ]]); - (new ServiceManagerConfig())->configureServiceManager($serviceManager); + ]]))->configureServiceManager($serviceManager); $instance = $serviceManager->get(TestAsset\DuckTypedServiceLocatorAware::class); $this->assertInstanceOf(TestAsset\DuckTypedServiceLocatorAware::class, $instance);