pluginmanager createOptions for factory instance or service instances

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

pluginmanager createOptions for factory instance or service instances

Bas Kamer
Hi,

When I use the a the plugin manager i noticed something regarding the creation options.

The plugin manager can be used to pass creation options while getting a service.

$service = $pm->get(’service’, array(...));

However I am confused about the indented behavior of this feature. Looking at the code of createFromInvokable and createFromFactory these options are passed to the constructors of the invokable and the factory. Thus not constantly to the constructor of the service i am trying to retrieve.

This difference isn’t noticeable until you use 'not shared' services and factories together.

call this the first time;
$plgman->get(’service’, array(’some_option’=>1));
1. factory is created, createOptions are passed to __construct of factory
2. service instance is created, factory may do something useful with createOptions.

call this the first time and some_option is still 1.
$plgman->get(’service’, array(’some_option’=>2));
1. factory already exists, createOptions are not passed to factory
2. service instance is created, factory may do something useful with incorrect createOptions.

I am not sure if this is intended or not, but this makes more sense to me;

$instance = $this->createServiceViaCallback(array($factory, 'createService'), $canonicalName, $requestedName, $this->creationOptions);

where the createService method may pass the creationOptions to the actual service being created.


Thank you

Bas Kamer


from AbstractPluginManager

    protected function createFromInvokable($canonicalName, $requestedName)
    {
        $invokable = $this->invokableClasses[$canonicalName];

        if (null === $this->creationOptions
            || (is_array($this->creationOptions) && empty($this->creationOptions))
        ) {
            $instance = new $invokable();
        } else {
            $instance = new $invokable($this->creationOptions);
        }

        return $instance;
    }


protected function createFromFactory($canonicalName, $requestedName)
    {
        $factory            = $this->factories[$canonicalName];
        $hasCreationOptions = !(null === $this->creationOptions || (is_array($this->creationOptions) && empty($this->creationOptions)));

        if (is_string($factory) && class_exists($factory, true)) {
            if (!$hasCreationOptions) {
                $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;
    }
--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]