Permalink
Browse files

Adds the ability to the plugin manager to instanciate by factory AND …

…have options passed to the factory constructor
  • Loading branch information...
1 parent 0289b27 commit 5e971ef4f49aa6c61b4131a83dcf9d8bd859ad0f @basz committed Oct 12, 2012
Showing with 38 additions and 0 deletions.
  1. +38 −0 library/Zend/ServiceManager/AbstractPluginManager.php
@@ -181,4 +181,42 @@ protected function createFromInvokable($canonicalName, $requestedName)
return $instance;
}
+
+ /**
+ * Attempt to create an instance via a factory class
+ *
+ * Overrides parent implementation by passing $creationOptions to the
+ * constructor, if non-null.
+ *
+ * @param string $canonicalName
+ * @param string $requestedName
+ * @return mixed
+ * @throws Exception\ServiceNotCreatedException If factory is not callable
+ */
+ protected function createFromFactory($canonicalName, $requestedName)
+ {
+ $factory = $this->factories[$canonicalName];
+ if (is_string($factory) && class_exists($factory, true)) {
+ if (null === $this->creationOptions || (is_array($this->creationOptions) && empty($this->creationOptions))) {
+ $factory = new $factory();
+ } else {
+ $factory = new $factory($this->creationOptions);
+ }
+
+ $this->factories[$canonicalName] = $factory;
+ }
+
+ if ($factory instanceof FactoryInterface) {
+ $instance = $this->createServiceViaCallback(array($factory, 'createService'), $canonicalName, $requestedName);
+ } elseif (is_callable($factory)) {
+ $instance = $this->createServiceViaCallback($factory, $canonicalName, $requestedName);
+ } else {
+ throw new Exception\ServiceNotCreatedException(sprintf(
+ 'While attempting to create %s%s an invalid factory was registered for this instance type.', $canonicalName, ($requestedName ? '(alias: ' . $requestedName . ')' : '')
+ ));
+ }
+
+ return $instance;
+ }
+
}

0 comments on commit 5e971ef

Please sign in to comment.