From 285d3a7c5468bb16fa948d17103a958df586ae94 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sun, 18 Nov 2012 16:37:31 +0100 Subject: [PATCH 1/2] Adding failing test for zendframework/zf2zendframework/zf2#2497 --- test/ServiceManagerTest.php | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/test/ServiceManagerTest.php b/test/ServiceManagerTest.php index 92f7ff1d..cb04dbf0 100644 --- a/test/ServiceManagerTest.php +++ b/test/ServiceManagerTest.php @@ -578,13 +578,12 @@ public function testWithAllowOverrideOnRegisteringAServiceDuplicatingAnExistingA { $this->serviceManager->setAllowOverride(true); $sm = $this->serviceManager; - $this->serviceManager->setFactory('http.response', function ($services) use ($sm) { + $this->serviceManager->setFactory('http.response', function () use ($sm) { return $sm; }); $this->serviceManager->setAlias('response', 'http.response'); $this->assertSame($sm, $this->serviceManager->get('response')); - $self = $this; $this->serviceManager->{$method}('response', $service); $this->{$assertion}($expected, $this->serviceManager->get('response')); } @@ -615,4 +614,22 @@ public function testWanCreateFromAbstractFactoryWillNotInstantiateAbstractFactor $this->assertSame($count + 1, FooCounterAbstractFactory::$instantiationCount); } + + /** + * @covers Zend\ServiceManager\ServiceManager::setAlias + * @covers Zend\ServiceManager\ServiceManager::get + * @covers Zend\ServiceManager\ServiceManager::retrieveFromPeeringManager + */ + public function testCanGetAliasedServicesFromPeeringServiceManagers() + { + $service = new \stdClass(); + $peeringSm = new ServiceManager(); + + $peeringSm->setService('actual-service-name', $service); + $this->serviceManager->addPeeringServiceManager($peeringSm); + + $this->serviceManager->setAlias('alias-name', 'actual-service-name'); + + $this->assertSame($service, $this->serviceManager->get('alias-name')); + } } From be36b1f936cb204c9ccea97646ba02b085d89fb2 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sun, 18 Nov 2012 16:38:51 +0100 Subject: [PATCH 2/2] Applying fix for failing test of zendframework/zf2zendframework/zf2#2497 --- src/ServiceManager.php | 48 ++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/src/ServiceManager.php b/src/ServiceManager.php index 8d604fec..7ce23028 100644 --- a/src/ServiceManager.php +++ b/src/ServiceManager.php @@ -411,20 +411,16 @@ public function setShared($name, $isShared) */ public function get($name, $usePeeringServiceManagers = true) { - $cName = $this->canonicalizeName($name); - $rName = $name; + $cName = $this->canonicalizeName($name); + $rName = $name; + $isAlias = false; if ($this->hasAlias($cName)) { + $isAlias = true; + do { $cName = $this->aliases[$cName]; } while ($this->hasAlias($cName)); - - if (!$this->has(array($cName, $rName))) { - throw new Exception\ServiceNotFoundException(sprintf( - 'An alias "%s" was requested but no service could be found.', - $name - )); - } } if (isset($this->instances[$cName])) { @@ -447,16 +443,21 @@ public function get($name, $usePeeringServiceManagers = true) // Still no instance? raise an exception if (!$instance && !is_array($instance)) { + if ($isAlias) { + throw new Exception\ServiceNotFoundException(sprintf( + 'An alias "%s" was requested but no service could be found.', + $name + )); + } + throw new Exception\ServiceNotFoundException(sprintf( '%s was unable to fetch or create an instance for %s', - __METHOD__, - $name - ) - ); + __METHOD__, + $name + )); } - if ($this->shareByDefault() && (!isset($this->shared[$cName]) || $this->shared[$cName] === true) - ) { + if ($this->shareByDefault() && (!isset($this->shared[$cName]) || $this->shared[$cName] === true)) { $this->instances[$cName] = $instance; } @@ -467,7 +468,7 @@ public function get($name, $usePeeringServiceManagers = true) * Create an instance * * @param string|array $name - * @return false|object + * @return bool|object * @throws Exception\ServiceNotFoundException * @throws Exception\ServiceNotCreatedException */ @@ -795,6 +796,21 @@ protected function retrieveFromPeeringManager($name) return $peeringServiceManager->get($name); } } + + $name = $this->canonicalizeName($name); + + if ($this->hasAlias($name)) { + do { + $name = $this->aliases[$name]; + } while ($this->hasAlias($name)); + } + + foreach ($this->peeringServiceManagers as $peeringServiceManager) { + if ($peeringServiceManager->has($name)) { + return $peeringServiceManager->get($name); + } + } + return null; }