From d66fd64cdc6a6e3cdb2cd78bbb45d8777f7570dc Mon Sep 17 00:00:00 2001 From: Evan Coury Date: Mon, 18 Feb 2013 11:46:49 -0700 Subject: [PATCH] Bug fix for GlobIterator extending service Reported by andrer on IRC... If a service extends GlobIterator, there was a problem where we were checking if (!$instance) which would yield a catchable fatal error that GlobIterator cannot be casted to a boolean. To solve this, the comparison operators should be strict so PHP does not try to convert the type internally. The types checked against are the expected types if no services have been returned, so the overall behavior should not be changed by this fix at all. --- .../Zend/ServiceManager/ServiceManager.php | 6 +++--- .../ServiceManager/ServiceManagerTest.php | 12 ++++++++++++ .../TestAsset/GlobIteratorService.php | 19 +++++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 tests/ZendTest/ServiceManager/TestAsset/GlobIteratorService.php diff --git a/library/Zend/ServiceManager/ServiceManager.php b/library/Zend/ServiceManager/ServiceManager.php index b897acbe497..21bb9309ce8 100644 --- a/library/Zend/ServiceManager/ServiceManager.php +++ b/library/Zend/ServiceManager/ServiceManager.php @@ -445,7 +445,7 @@ public function get($name, $usePeeringServiceManagers = true) } // Still no instance? raise an exception - if (!$instance && !is_array($instance)) { + if ($instance === null && !is_array($instance)) { if ($isAlias) { throw new Exception\ServiceNotFoundException(sprintf( 'An alias "%s" was requested but no service could be found.', @@ -494,11 +494,11 @@ public function create($name) $instance = $this->createFromFactory($cName, $rName); } - if (!$instance && isset($this->invokableClasses[$cName])) { + if ($instance === false && isset($this->invokableClasses[$cName])) { $instance = $this->createFromInvokable($cName, $rName); } - if (!$instance && $this->canCreateFromAbstractFactory($cName, $rName)) { + if ($instance === false && $this->canCreateFromAbstractFactory($cName, $rName)) { $instance = $this->createFromAbstractFactory($cName, $rName); } diff --git a/tests/ZendTest/ServiceManager/ServiceManagerTest.php b/tests/ZendTest/ServiceManager/ServiceManagerTest.php index 6fc287d0994..611e3d92925 100644 --- a/tests/ZendTest/ServiceManager/ServiceManagerTest.php +++ b/tests/ZendTest/ServiceManager/ServiceManagerTest.php @@ -560,6 +560,18 @@ public function testShouldRaiseExceptionIfInitializerClassIsNotAnInitializerInte $result = $this->serviceManager->addInitializer(get_class($this)); } + public function testGetGlobIteratorServiceWorksProperly() + { + $config = new Config(array( + 'invokables' => array( + 'foo' => 'ZendTest\ServiceManager\TestAsset\GlobIteratorService', + ), + )); + $serviceManager = new ServiceManager($config); + $foo = $serviceManager->get('foo'); + $this->assertInstanceOf('ZendTest\ServiceManager\TestAsset\GlobIteratorService', $foo); + } + public function duplicateService() { $self = $this; diff --git a/tests/ZendTest/ServiceManager/TestAsset/GlobIteratorService.php b/tests/ZendTest/ServiceManager/TestAsset/GlobIteratorService.php new file mode 100644 index 00000000000..588c3e29d80 --- /dev/null +++ b/tests/ZendTest/ServiceManager/TestAsset/GlobIteratorService.php @@ -0,0 +1,19 @@ +