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

Commit

Permalink
Merge branch 'hotfix/3006'
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Nov 19, 2012
2 parents dbd9ca8 + 6aaf045 commit 34e7aec
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 18 deletions.
48 changes: 32 additions & 16 deletions src/ServiceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand All @@ -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;
}

Expand All @@ -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
*/
Expand Down Expand Up @@ -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;
}

Expand Down
21 changes: 19 additions & 2 deletions test/ServiceManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
Expand Down Expand Up @@ -637,4 +636,22 @@ public function testAbstractFactoryNotUsedIfNotAbleToCreate()

$this->assertSame($service, $this->serviceManager->create('test'));
}

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

0 comments on commit 34e7aec

Please sign in to comment.