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

Commit

Permalink
Merge branch 'master' into feature/math-biginteger
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
8 changes: 8 additions & 0 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public function __construct($configuration = array())
$this->configuration = $configuration;
}

public function getAllowOverride()
{
return (isset($this->configuration['allow_override'])) ? $this->configuration['allow_override'] : null;
}

public function getFactories()
{
return (isset($this->configuration['factories'])) ? $this->configuration['factories'] : array();
Expand Down Expand Up @@ -43,6 +48,9 @@ public function getShared()

public function configureServiceManager(ServiceManager $serviceManager)
{
$allowOverride = $this->getAllowOverride();
isset($allowOverride) ? $serviceManager->setAllowOverride($allowOverride) : null;

foreach ($this->getFactories() as $name => $factory) {
$serviceManager->setFactory($name, $factory);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Di/DiServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function get($name, array $params = array())
if ($this->useServiceLocator == self::USE_SL_AFTER_DI && $this->serviceLocator->has($name)) {
return $this->serviceLocator->get($name);
} else {
throw new Exception\ServiceNotCreatedException(
throw new Exception\ServiceNotFoundException(
sprintf('Service %s was not found in this DI instance', $name),
null,
$e
Expand Down
35 changes: 20 additions & 15 deletions src/ServiceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,34 +264,41 @@ public function get($name, $usePeeringServiceManagers = true)
do {
$cName = $this->aliases[$cName];
} while ($this->hasAlias($cName));

if (!$this->has($cName)) {
throw new Exception\ServiceNotFoundException(sprintf(
'An alias "%s" was requested but no service could be found.',
$name
));
}
}

$instance = null;

if (isset($this->instances[$cName])) {
$instance = $this->instances[$cName];
}
}

$selfException = null;

if (!$instance && !is_array($instance)) {
try {
$instance = $this->create(array($cName, $rName));
} catch (Exception\ServiceNotCreatedException $selfException) {
foreach ($this->peeringServiceManagers as $peeringServiceManager) {
try {
$instance = $peeringServiceManager->get($name);
break;
} catch (Exception\ServiceNotCreatedException $e) {
continue;
} catch (Exception\ServiceNotFoundException $selfException) {
if ($usePeeringServiceManagers) {
foreach ($this->peeringServiceManagers as $peeringServiceManager) {
if ($peeringServiceManager->has($name)) {
$instance = $peeringServiceManager->get($name);
break;
}
}
}
}
}

// Still no instance? raise an exception
if (!$instance && !is_array($instance)) {
throw new Exception\ServiceNotCreatedException(sprintf(
throw new Exception\ServiceNotFoundException(sprintf(
'%s was unable to fetch or create an instance for %s',
__METHOD__,
$name
Expand Down Expand Up @@ -383,7 +390,7 @@ public function create($name)
}

if ($this->throwExceptionInCreate == true && $instance === false) {
throw new Exception\ServiceNotCreatedException(sprintf(
throw new Exception\ServiceNotFoundException(sprintf(
'No valid instance was found for %s%s',
$cName,
($rName ? '(alias: ' . $rName . ')' : '')
Expand Down Expand Up @@ -436,7 +443,7 @@ public function has($nameOrAlias, $usePeeringServiceManagers = true)
if (!$abstractFactory instanceof AbstractFactoryInterface) {
continue;
}

if ($abstractFactory->canCreateServiceWithName($cName, $rName)) {
return true;
}
Expand Down Expand Up @@ -477,10 +484,6 @@ public function setAlias($alias, $nameOrAlias)
throw new Exception\InvalidServiceNameException('An alias by this name already exists');
}

if (!$this->has($nameOrAlias)) {
throw new Exception\ServiceNotFoundException('A target service or target alias could not be located');
}

$this->aliases[$alias] = $nameOrAlias;
return $this;
}
Expand Down Expand Up @@ -540,6 +543,8 @@ protected function createServiceViaCallback($callable, $cName, $rName)
$circularDependencyResolver[spl_object_hash($this) . '-' . $cName] = true;
try {
$instance = call_user_func($callable, $this, $cName, $rName);
} catch (Exception\ServiceNotFoundException $e) {
throw $e;
} catch (\Exception $e) {
throw new Exception\ServiceNotCreatedException(
sprintf('Abstract factory raised an exception when creating "%s"; no instance returned', $rName),
Expand Down
24 changes: 22 additions & 2 deletions test/ServiceManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public function testGetDoesNotThrowExceptionOnEmptyArray()
*/
public function testGetThrowsExceptionOnUnknownService()
{
$this->setExpectedException('Zend\ServiceManager\Exception\ServiceNotCreatedException');
$this->setExpectedException('Zend\ServiceManager\Exception\ServiceNotFoundException');
$this->assertEquals('bar', $this->serviceManager->get('foo'));
}

Expand Down Expand Up @@ -303,10 +303,30 @@ public function testSetAliasThrowsExceptionOnDuplicateAlias()
/**
* @covers Zend\ServiceManager\ServiceManager::setAlias
*/
public function testSetAliasThrowExceptionOnServiceNotFound()
public function testSetAliasDoesNotThrowExceptionOnServiceNotFound()
{
$this->serviceManager->setAlias('foo', 'bar');
}

/**
* @covers Zend\ServiceManager\ServiceManager::get
*/
public function testGetServiceThrowsExceptionOnAliasWithNoSetService()
{
$this->setExpectedException('Zend\ServiceManager\Exception\ServiceNotFoundException');
$this->serviceManager->setAlias('foo', 'bar');
$this->serviceManager->get('foo');
}

/**
* @cover Zend\ServiceManager\ServiceManager::get
*/
public function testGetServiceThrowsExceptionOnMultipleAliasesWithNoSetService()
{
$this->setExpectedException('Zend\ServiceManager\Exception\ServiceNotFoundException');
$this->serviceManager->setAlias('foo', 'bar');
$this->serviceManager->setAlias('baz', 'foo');
$this->serviceManager->get('foo');
}

/**
Expand Down

0 comments on commit 9d55623

Please sign in to comment.