Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/5100'
Browse files Browse the repository at this point in the history
Close #5100
  • Loading branch information
weierophinney committed Oct 22, 2013
2 parents 4c884e1 + 2a93df1 commit 5bbe1c8
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\ServiceManager\Exception;

class CircularReferenceException extends RuntimeException
{
}
69 changes: 64 additions & 5 deletions library/Zend/ServiceManager/ServiceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,32 @@ public function setShared($name, $isShared)
return $this;
}

/**
* Resolve the alias for the given canonical name
*
* @param string $cName The canonical name to resolve
* @return string The resolved canonical name
*/
protected function resolveAlias($cName)
{
$stack = array();

while ($this->hasAlias($cName)) {
if (isset($stack[$cName])) {
throw new Exception\CircularReferenceException(sprintf(
'Circular alias reference: %s -> %s',
implode(' -> ', $stack),
$cName
));
}

$stack[$cName] = $cName;
$cName = $this->aliases[$cName];
}

return $cName;
}

/**
* Retrieve a registered instance
*
Expand All @@ -448,12 +474,9 @@ public function get($name, $usePeeringServiceManagers = true)

$isAlias = false;

if (isset($this->aliases[$cName])) {
if ($this->hasAlias($cName)) {
$isAlias = true;

do {
$cName = $this->aliases[$cName];
} while ($this->hasAlias($cName));
$cName = $this->resolveAlias($cName);
}

$instance = null;
Expand Down Expand Up @@ -731,6 +754,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
Expand Down Expand Up @@ -759,6 +814,10 @@ public function setAlias($alias, $nameOrAlias)
));
}

if ($this->hasAlias($alias)) {
$this->checkForCircularAliasReference($cAlias, $nameOrAlias);
}

$this->aliases[$cAlias] = $nameOrAlias;
return $this;
}
Expand Down
42 changes: 42 additions & 0 deletions tests/ZendTest/ServiceManager/ServiceManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -810,4 +810,46 @@ public function testUsesMultipleDelegates()
$this->assertInstanceOf('stdClass', array_shift($fooDelegator->instances));
$this->assertSame($fooDelegator, array_shift($barDelegator->instances));
}

/**
* @covers Zend\ServiceManager\ServiceManager::resolveAlias
*/
public function testSetCircularAliasReferenceThrowsException()
{
$this->setExpectedException('Zend\ServiceManager\Exception\CircularReferenceException');

// Only affects service managers that allow overwriting definitions
$this->serviceManager->setAllowOverride(true);
$this->serviceManager->setInvokableClass('foo-service', 'stdClass');
$this->serviceManager->setAlias('foo-alias', 'foo-service');
$this->serviceManager->setAlias('bar-alias', 'foo-alias');
$this->serviceManager->setAlias('baz-alias', 'bar-alias');

// 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');
}
}

0 comments on commit 5bbe1c8

Please sign in to comment.