From a9ff117107979610c3704ffa5e6cbae5082f6b45 Mon Sep 17 00:00:00 2001 From: tux-rampage Date: Wed, 11 Sep 2013 09:42:29 +0200 Subject: [PATCH 1/5] cs fix --- src/ServiceManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ServiceManager.php b/src/ServiceManager.php index 5c743f3e..f345a334 100644 --- a/src/ServiceManager.php +++ b/src/ServiceManager.php @@ -433,7 +433,7 @@ public function setShared($name, $isShared) /** * Resolve the alias for the given canonical name * - * @param string $cName The canonical name to resolve + * @param string $cName The canonical name to resolve * @return string The resolved canonical name */ protected function resolveAlias($cName) From 8a8c72bacb0208353bc3e37284687e6017a6149f Mon Sep 17 00:00:00 2001 From: tux-rampage Date: Wed, 11 Sep 2013 09:46:55 +0200 Subject: [PATCH 2/5] Removed unneccessary import for exception class --- src/ServiceManager.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ServiceManager.php b/src/ServiceManager.php index f345a334..0b2cbcbb 100644 --- a/src/ServiceManager.php +++ b/src/ServiceManager.php @@ -10,7 +10,6 @@ namespace Zend\ServiceManager; use ReflectionClass; -use Zend\ServiceManager\Exception\CircularReferenceException; class ServiceManager implements ServiceLocatorInterface { @@ -442,7 +441,7 @@ protected function resolveAlias($cName) while ($this->hasAlias($cName)) { if (isset($stack[$cName])) { - throw new CircularReferenceException(sprintf( + throw new Exception\CircularReferenceException(sprintf( 'Circular alias reference: %s -> %s', implode(' -> ', $stack), $cName )); From 45013201cf17a68e28fcde37ae0fd86d6022c5c5 Mon Sep 17 00:00:00 2001 From: tux-rampage Date: Wed, 11 Sep 2013 14:46:01 +0200 Subject: [PATCH 3/5] Fixed typo in unit test covers annotation --- test/ServiceManagerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/ServiceManagerTest.php b/test/ServiceManagerTest.php index 33380bc8..ae45ff23 100644 --- a/test/ServiceManagerTest.php +++ b/test/ServiceManagerTest.php @@ -812,7 +812,7 @@ public function testUsesMultipleDelegates() } /** - * @covers Zend\ServiceMnager\ServiceManager::resolveAlias + * @covers Zend\ServiceManager\ServiceManager::resolveAlias */ public function testCircularAliasReferenceThrowsException() { From 87725b3c7b396b30a218ac3161565444087f52a7 Mon Sep 17 00:00:00 2001 From: tux-rampage Date: Thu, 12 Sep 2013 14:14:52 +0200 Subject: [PATCH 4/5] Added check for circular alias reference to setAlias() --- src/ServiceManager.php | 36 ++++++++++++++++++++++++++++++++++++ test/ServiceManagerTest.php | 25 +++++++++++++++++++++++-- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/src/ServiceManager.php b/src/ServiceManager.php index 0b2cbcbb..504807d6 100644 --- a/src/ServiceManager.php +++ b/src/ServiceManager.php @@ -753,6 +753,38 @@ public function canCreateFromAbstractFactory($cName, $rName) return false; } + /** + * Ensure the alias definition will not result in a circular reference + * + * @param string $alias + * @param string $nameOrAlias + * @throws Exception\CircularReferenceException + * @return self + */ + protected function checkForCircularAliasReference($alias, $nameOrAlias) + { + $aliases = $this->aliases; + $aliases[$alias] = $nameOrAlias; + $stack = array(); + + while (isset($aliases[$alias])) { + if (isset($stack[$alias])) { + throw new Exception\CircularReferenceException(sprintf( + 'The alias definition "%s" : "%s" results in a circular reference: "%s" -> "%s"', + $alias, + $nameOrAlias, + implode('" -> "', $stack), + $alias + )); + } + + $stack[$alias] = $alias; + $alias = $aliases[$alias]; + } + + return $this; + } + /** * @param string $alias * @param string $nameOrAlias @@ -781,6 +813,10 @@ public function setAlias($alias, $nameOrAlias) )); } + if ($this->hasAlias($alias)) { + $this->checkForCircularAliasReference($cAlias, $nameOrAlias); + } + $this->aliases[$cAlias] = $nameOrAlias; return $this; } diff --git a/test/ServiceManagerTest.php b/test/ServiceManagerTest.php index ae45ff23..0176de14 100644 --- a/test/ServiceManagerTest.php +++ b/test/ServiceManagerTest.php @@ -814,7 +814,7 @@ public function testUsesMultipleDelegates() /** * @covers Zend\ServiceManager\ServiceManager::resolveAlias */ - public function testCircularAliasReferenceThrowsException() + public function testSetCircularAliasReferenceThrowsException() { $this->setExpectedException('Zend\ServiceManager\Exception\CircularReferenceException'); @@ -825,8 +825,29 @@ public function testCircularAliasReferenceThrowsException() $this->serviceManager->setAlias('bar-alias', 'foo-alias'); $this->serviceManager->setAlias('baz-alias', 'bar-alias'); - // This will now cause a cyclic reference + // This will now cause a cyclic reference and should throw an exception $this->serviceManager->setAlias('foo-alias', 'bar-alias'); + } + + /** + * @covers Zend\ServiceManager\ServiceManager::checkForCircularAliasReference + */ + public function testResolveCircularAliasReferenceThrowsException() + { + $this->setExpectedException('Zend\ServiceManager\Exception\CircularReferenceException'); + + // simulate an inconsistent state of $servicemanager->aliases as it could be + // caused by derived classes + $cyclicAliases = array( + 'fooalias' => 'bazalias', + 'baralias' => 'fooalias', + 'bazalias' => 'baralias' + ); + + $reflection = new \ReflectionObject($this->serviceManager); + $propertyReflection = $reflection->getProperty('aliases'); + $propertyReflection->setAccessible(true); + $propertyReflection->setValue($this->serviceManager, $cyclicAliases); // This should throw the exception $this->serviceManager->get('baz-alias'); From b65505379f135ed3d5679479644c4a40c6800ecf Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Tue, 22 Oct 2013 16:15:13 -0500 Subject: [PATCH 5/5] [zendframework/zf2#5100] CS fixes - one argument per line (sprintf call) --- src/ServiceManager.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ServiceManager.php b/src/ServiceManager.php index 504807d6..67d1a441 100644 --- a/src/ServiceManager.php +++ b/src/ServiceManager.php @@ -443,7 +443,8 @@ protected function resolveAlias($cName) if (isset($stack[$cName])) { throw new Exception\CircularReferenceException(sprintf( 'Circular alias reference: %s -> %s', - implode(' -> ', $stack), $cName + implode(' -> ', $stack), + $cName )); }