Permalink
Browse files

add new method for delegators

done:
* move delegator logic from create() to separate method
* remove automatic invokable service for delegator factories
* add delegator factory class validation
* update tests
  • Loading branch information...
1 parent 3198d58 commit 0f479af10a9bdd445f746ee8cb8f3b971ac6c3dd @stefanotorresi committed Sep 26, 2013
@@ -532,25 +532,7 @@ public function create($name)
}
if (isset($this->delegators[$cName])) {
- $serviceManager = $this;
- $additionalDelegators = count($this->delegators[$cName]) - 1;
- $creationCallback = function () use ($serviceManager, $rName, $cName) {
- return $serviceManager->doCreate($rName, $cName);
- };
-
- for ($i = 0; $i < $additionalDelegators; $i += 1) {
- $creationCallback = $this->createDelegatorCallback(
- $this->delegators[$cName][$i],
- $rName,
- $cName,
- $creationCallback
- );
- }
-
- /* @var $delegatorFactory DelegatorFactoryInterface */
- $delegatorFactory = $this->get($this->delegators[$cName][$i]);
-
- return $delegatorFactory->createDelegatorWithName($this, $cName, $rName, $creationCallback);
+ return $this->createDelegatorFromFactory($cName, $rName);
}
return $this->doCreate($rName, $cName);
@@ -559,21 +541,18 @@ public function create($name)
/**
* Creates a callback that uses a delegator to create a service
*
- * @param string $delegatorFactoryName name of the delegator factory service
- * @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 $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
*
* @return callable
*/
- private function createDelegatorCallback($delegatorFactoryName, $rName, $cName, $creationCallback)
+ private function createDelegatorCallback($delegatorFactory, $rName, $cName, $creationCallback)
{
$serviceManager = $this;
- return function () use ($serviceManager, $delegatorFactoryName, $rName, $cName, $creationCallback) {
- /* @var $delegatorFactory DelegatorFactoryInterface */
- $delegatorFactory = $serviceManager->get($delegatorFactoryName);
-
+ return function () use ($serviceManager, $delegatorFactory, $rName, $cName, $creationCallback) {
return $delegatorFactory->createDelegatorWithName($serviceManager, $cName, $rName, $creationCallback);
};
}
@@ -1046,6 +1025,53 @@ protected function createFromAbstractFactory($canonicalName, $requestedName)
}
/**
+ * @param $canonicalName
+ * @param $requestedName
+ * @return mixed
+ * @throws Exception\ServiceNotCreatedException
+ */
+ protected function createDelegatorFromFactory($canonicalName, $requestedName)
+ {
+ $serviceManager = $this;
+ $delegatorsCount = count($this->delegators[$canonicalName]);
+ $creationCallback = function () use ($serviceManager, $requestedName, $canonicalName) {
+ return $serviceManager->doCreate($requestedName, $canonicalName);
+ };
+
+ for ($i = 0; $i < $delegatorsCount; $i += 1) {
+
+ $delegatorFactory = $this->delegators[$canonicalName][$i];
+
+ if (is_string($delegatorFactory) && class_exists($delegatorFactory, true)) {
+ $delegatorFactory = new $delegatorFactory;
+ $this->delegators[$canonicalName][$i] = $delegatorFactory;
+ }
+
+ if (!$delegatorFactory instanceof DelegatorFactoryInterface) {
+ throw new Exception\ServiceNotCreatedException(sprintf(
+ 'While attempting to create %s%s an invalid factory was registered for this instance type.',
+ $canonicalName,
+ ($requestedName ? '(alias: ' . $requestedName . ')' : '')
+ ));
+ }
+
+ // add additional callbacks for every delegatorFactory but the last
+ if ($delegatorsCount > 1 && $i != $delegatorsCount - 1) {
+ $creationCallback = $this->createDelegatorCallback(
+ $delegatorFactory,
+ $requestedName,
+ $canonicalName,
+ $creationCallback
+ );
+ }
+ }
+
+ $delegator = $delegatorFactory->createDelegatorWithName($this, $canonicalName, $requestedName, $creationCallback);
+
+ return $delegator;
+ }
+
+ /**
* Checks if the object has this class as one of its parents
*
* @see https://bugs.php.net/bug.php?id=53727
@@ -760,8 +760,7 @@ public function testUsesDelegatorWhenAvailable()
{
$delegator = $this->getMock('Zend\\ServiceManager\\DelegatorFactoryInterface');
- $this->serviceManager->setService('foo-delegator', $delegator);
- $this->serviceManager->addDelegator('foo-service', 'foo-delegator');
+ $this->serviceManager->addDelegator('foo-service', $delegator);
$this->serviceManager->setInvokableClass('foo-service', 'stdClass');
$delegator
@@ -783,8 +782,6 @@ public function testUsesDelegatorWhenAvailable()
)
->will($this->returnValue($delegator));
- //die(var_dump($this->serviceManager));
-
$this->assertSame($delegator, $this->serviceManager->create('foo-service'));
}
@@ -798,10 +795,8 @@ public function testUsesMultipleDelegates()
$fooDelegator = new MockSelfReturningDelegatorFactory();
$barDelegator = new MockSelfReturningDelegatorFactory();
- $this->serviceManager->setService('foo-delegate', $fooDelegator);
- $this->serviceManager->setService('bar-delegate', $barDelegator);
- $this->serviceManager->addDelegator('foo-service', 'foo-delegate');
- $this->serviceManager->addDelegator('foo-service', 'bar-delegate');
+ $this->serviceManager->addDelegator('foo-service', $fooDelegator);
+ $this->serviceManager->addDelegator('foo-service', $barDelegator);
$this->serviceManager->setInvokableClass('foo-service', 'stdClass');
$this->assertSame($barDelegator, $this->serviceManager->create('foo-service'));

0 comments on commit 0f479af

Please sign in to comment.