Permalink
Browse files

add callback support for delegator factories

  • Loading branch information...
1 parent da426a6 commit 8d42203bba827e100ea5f3954781d5015fbfc2ce @stefanotorresi committed Sep 30, 2013
Showing with 11 additions and 7 deletions.
  1. +11 −7 library/Zend/ServiceManager/ServiceManager.php
@@ -541,10 +541,10 @@ public function create($name)
/**
* Creates a callback that uses a delegator to create a service
*
- * @param DelegatorFactoryInterface $delegatorFactory the delegator factory
- * @param string $rName requested service name
- * @param string $cName canonical service name
- * @param callable $creationCallback callback that is responsible for instantiating the service
+ * @param DelegatorFactoryInterface|callable $delegatorFactory the delegator factory
+ * @param string $rName requested service name
+ * @param string $cName canonical service name
+ * @param callable $creationCallback callback for instantiating the real service
*
* @return callable
*/
@@ -553,7 +553,9 @@ private function createDelegatorCallback($delegatorFactory, $rName, $cName, $cre
$serviceManager = $this;
return function () use ($serviceManager, $delegatorFactory, $rName, $cName, $creationCallback) {
- return $delegatorFactory->createDelegatorWithName($serviceManager, $cName, $rName, $creationCallback);
+ return $delegatorFactory instanceof DelegatorFactoryInterface
+ ? $delegatorFactory->createDelegatorWithName($serviceManager, $cName, $rName, $creationCallback)
+ : $delegatorFactory($serviceManager, $cName, $rName, $creationCallback);
};
}
@@ -1049,7 +1051,7 @@ protected function createDelegatorFromFactory($canonicalName, $requestedName)
$this->delegators[$canonicalName][$i] = $delegatorFactory;
}
- if (!$delegatorFactory instanceof DelegatorFactoryInterface) {
+ if (!$delegatorFactory instanceof DelegatorFactoryInterface && !is_callable($delegatorFactory)) {
throw new Exception\ServiceNotCreatedException(sprintf(
'While attempting to create %s%s an invalid factory was registered for this instance type.',
$canonicalName,
@@ -1068,7 +1070,9 @@ protected function createDelegatorFromFactory($canonicalName, $requestedName)
}
}
- $delegator = $delegatorFactory->createDelegatorWithName($this, $canonicalName, $requestedName, $creationCallback);
+ $delegator = $delegatorFactory instanceof DelegatorFactoryInterface
+ ? $delegatorFactory->createDelegatorWithName($serviceManager, $canonicalName, $requestedName, $creationCallback)
+ : $delegatorFactory($serviceManager, $canonicalName, $requestedName, $creationCallback);
return $delegator;
}

0 comments on commit 8d42203

Please sign in to comment.