diff --git a/src/Service/ServiceManagerConfig.php b/src/Service/ServiceManagerConfig.php index d667c1628..94badafb2 100644 --- a/src/Service/ServiceManagerConfig.php +++ b/src/Service/ServiceManagerConfig.php @@ -111,9 +111,11 @@ public function __construct(array $config = []) }, 'ServiceManagerAwareInitializer' => function ($first, $second) { if ($first instanceof ContainerInterface) { + // zend-servicemanager v3 $container = $first; $instance = $second; } else { + // zend-servicemanager v2 $container = $second; $instance = $first; } @@ -129,10 +131,16 @@ public function __construct(array $config = []) } }, 'ServiceLocatorAwareInitializer' => function ($first, $second) { - if ($first instanceof ContainerInterface) { + if ($first instanceof AbstractPluginManager) { + // Edge case under zend-servicemanager v2 + $container = $second; + $instance = $first; + } elseif ($first instanceof ContainerInterface) { + // zend-servicemanager v3 $container = $first; $instance = $second; } else { + // zend-servicemanager v2 $container = $second; $instance = $first; } diff --git a/test/Service/ServiceManagerConfigTest.php b/test/Service/ServiceManagerConfigTest.php index 0956ad68b..286a5b19e 100644 --- a/test/Service/ServiceManagerConfigTest.php +++ b/test/Service/ServiceManagerConfigTest.php @@ -13,6 +13,7 @@ use ReflectionClass; use stdClass; use Zend\EventManager\EventManager; +use Zend\Mvc\Controller\PluginManager; use Zend\Mvc\Service\ServiceManagerConfig; use Zend\ServiceManager\Factory\InvokableFactory; use Zend\ServiceManager\ServiceManager; @@ -42,6 +43,16 @@ protected function setUp() $this->config->configureServiceManager($this->services); } + /** + * Is this a v2 service manager? + * + * @return bool + */ + public function isV2ServiceManager() + { + return (! method_exists($this->services, 'configure')); + } + /** * Create an event manager instance based on zend-eventmanager version * @@ -225,4 +236,66 @@ public function testCreatesAFactoryForTheServiceManagerThatReturnsIt() $this->assertTrue($serviceManager->has('ServiceManager'), 'Missing ServiceManager service!'); $this->assertSame($serviceManager, $serviceManager->get('ServiceManager')); } + + /** + * @see https://github.com/zendframework/zend-servicemanager/issues/109 + */ + public function testServiceLocatorAwareInitializerCanInjectPluginManagers() + { + if (! $this->isV2ServiceManager()) { + $this->markTestSkipped(sprintf( + '%s verifies backwards compatibility with the v2 series of zend-servicemanager', + __FUNCTION__ + )); + } + + $this->services->setFactory('test-plugins', function () { + return new PluginManager(); + }); + + $deprecated = false; + set_error_handler(function ($errno, $errstr) use (&$deprecated) { + $deprecated = true; + }, E_USER_DEPRECATED); + + $plugins = $this->services->get('test-plugins'); + + restore_error_handler(); + + $this->assertSame($this->services, $plugins->getServiceLocator()); + $this->assertTrue($deprecated, 'Deprecation notice for ServiceLocatorAwareInitializer was not triggered'); + } + + /** + * @see https://github.com/zendframework/zend-servicemanager/issues/109 + */ + public function testServiceLocatorAwareInitializerWillNotReinjectPluginManagers() + { + if (! $this->isV2ServiceManager()) { + $this->markTestSkipped(sprintf( + '%s verifies backwards compatibility with the v2 series of zend-servicemanager', + __FUNCTION__ + )); + } + + $altServices = new ServiceManager(); + $this->services->setFactory('test-plugins', function () use ($altServices) { + return new PluginManager($altServices); + }); + + $deprecated = false; + set_error_handler(function ($errno, $errstr) use (&$deprecated) { + $deprecated = true; + }, E_USER_DEPRECATED); + + $plugins = $this->services->get('test-plugins'); + + restore_error_handler(); + + $this->assertSame($altServices, $plugins->getServiceLocator()); + $this->assertFalse( + $deprecated, + 'Deprecation notice for ServiceLocatorAwareInitializer was triggered, but should not have been' + ); + } }