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' of github.com:zendframework/zf2 into feature/ro…
Browse files Browse the repository at this point in the history
…uter-http-method
  • Loading branch information
mpinkston committed May 18, 2012
2 parents cf82ea3 + e035a2a commit 83055d8
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function configureServiceManager(ServiceManager $serviceManager)
}

foreach ($this->getInvokables() as $name => $invokable) {
$serviceManager->setInvokable($name, $invokable);
$serviceManager->setInvokableClass($name, $invokable);
}

foreach ($this->getServices() as $name => $service) {
Expand Down
2 changes: 1 addition & 1 deletion src/Di/DiAbstractServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function __construct(Di $di, $useServiceLocator = self::USE_SL_NONE)
{
$this->di = $di;
if (in_array($useServiceLocator, array(self::USE_SL_BEFORE_DI, self::USE_SL_AFTER_DI, self::USE_SL_NONE))) {
$this->useServiceManager = $useServiceLocator;
$this->useServiceLocator = $useServiceLocator;
}

// since we are using this in a proxy-fashion, localize state
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\InvalidServiceNameException(
throw new Exception\ServiceNotCreatedException(
sprintf('Service %s was not found in this DI instance', $name),
null,
$e
Expand Down
1 change: 1 addition & 0 deletions src/ServiceLocatorAwareInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
interface ServiceLocatorAwareInterface
{
public function setServiceLocator(ServiceLocatorInterface $serviceLocator);
public function getServiceLocator();
}
76 changes: 58 additions & 18 deletions src/ServiceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,10 @@ public function setInvokableClass($name, $invokableClass, $shared = true)
$name = $this->canonicalizeName($name);

if ($this->allowOverride === false && $this->has($name)) {
throw new Exception\InvalidServiceNameException(
'A service by this name or alias already exists and cannot be overridden, please use an alternate name.'
);
throw new Exception\InvalidServiceNameException(sprintf(
'A service by the name or alias "%s" already exists and cannot be overridden; please use an alternate name',
$name
));
}
$this->invokableClasses[$name] = $invokableClass;
$this->shared[$name] = $shared;
Expand Down Expand Up @@ -230,13 +231,17 @@ public function setService($name, $service, $shared = true)
*/
public function setShared($name, $isShared)
{
$name = $this->canonicalizeName($name);
$cName = $this->canonicalizeName($name);

if (!isset($this->invokableClasses[$name]) && !isset($this->factories[$name])) {
throw new Exception\ServiceNotFoundException('A service by the name provided was not found and could not be marked as shared.');
if (!isset($this->invokableClasses[$cName]) && !isset($this->factories[$cName])) {
throw new Exception\ServiceNotFoundException(sprintf(
'%s: A service by the name "%s" was not found and could not be marked as shared',
__METHOD__,
$name
));
}

$this->shared[$name] = (bool) $isShared;
$this->shared[$cName] = (bool) $isShared;
return $this;
}

Expand All @@ -262,18 +267,38 @@ public function get($name, $usePeeringServiceManagers = true)

if (isset($this->instances[$cName])) {
$instance = $this->instances[$cName];
} elseif ($usePeeringServiceManagers) {
foreach ($this->peeringServiceManagers as $peeringServiceManager) {
try {
$instance = $peeringServiceManager->get($name);
} catch (Exception\ServiceNotCreatedException $e) {
continue;
}

$selfException = null;

if (!$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;
}
}
}
}

if (!$instance) {
$instance = $this->create(array($cName, $rName));

// Still no instance? raise an exception
if (!$instance) {
throw new Exception\ServiceNotCreatedException(sprintf(
'%s was unable to fetch or create an instance for %s',
__METHOD__,
$name
),
null,
($selfException === null) ? null : $selfException->getPrevious()
);
}
}

if (isset($this->shared[$cName]) && $this->shared[$cName] === true) {
Expand Down Expand Up @@ -304,6 +329,14 @@ public function create($name)

if (isset($this->invokableClasses[$cName])) {
$invokable = $this->invokableClasses[$cName];
if (!class_exists($invokable)) {
throw new Exception\ServiceNotCreatedException(sprintf(
'%s: failed retrieving "%s" via invokable class "%s"; class does not exist',
__METHOD__,
$name,
$cName
));
}
$instance = new $invokable;
}

Expand Down Expand Up @@ -357,7 +390,6 @@ public function create($name)
));
}

/** @var $initializer InitializerInterface */
foreach ($this->initializers as $initializer) {
if ($initializer instanceof InitializerInterface) {
$initializer->initialize($instance);
Expand Down Expand Up @@ -431,7 +463,7 @@ public function setAlias($alias, $nameOrAlias)
throw new Exception\InvalidServiceNameException('Invalid service name alias');
}

if ($this->hasAlias($alias)) {
if ($this->allowOverride === false && $this->hasAlias($alias)) {
throw new Exception\InvalidServiceNameException('An alias by this name already exists');
}

Expand Down Expand Up @@ -492,11 +524,19 @@ protected function createServiceViaCallback($callable, $cName, $rName)

if (isset($circularDependencyResolver[spl_object_hash($this) . '-' . $cName])) {
$circularDependencyResolver = array();
throw new Exception\CircularDependencyFoundException('Circular dependency for LazyServiceLoader was found for instance ' . $name);
throw new Exception\CircularDependencyFoundException('Circular dependency for LazyServiceLoader was found for instance ' . $rName);
}

$circularDependencyResolver[spl_object_hash($this) . '-' . $cName] = true;
$instance = call_user_func($callable, $this, $cName, $rName);
try {
$instance = call_user_func($callable, $this, $cName, $rName);
} catch (\Exception $e) {
throw new Exception\ServiceNotCreatedException(
sprintf('Abstract factory raised an exception when creating "%s"; no instance returned', $rName),
$e->getCode(),
$e
);
}
if ($instance === null) {
throw new Exception\ServiceNotCreatedException('The factory was called but did not return an instance.');
}
Expand Down

0 comments on commit 83055d8

Please sign in to comment.