How create factories for AbstractPluginManager's?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How create factories for AbstractPluginManager's?

Александр Шелковский
Hello.

What is best way to create factories for AbstractPluginManager's?

Now I have a problem with it, beacuse AbstractPluginManager imlement ServiceLocatorAwareInterface, so you can call getServiceLocator() to get ServiceManager instance. BUT! Zend\ServiceManager\FactoryInterface::createService use Zend\ServiceManager\ServiceLocatorInterface for $serviceLocator, so you can't call getServiceLocator() on it.

Regards,
Alex.
Reply | Threaded
Open this post in threaded view
|

Re: How create factories for AbstractPluginManager's?

weierophinney
Administrator
-- Александр Шелковский <[hidden email]> wrote
(on Monday, 29 October 2012, 04:06 AM +0700):
> What is best way to create factories for AbstractPluginManager's?
>
> Now I have a problem with it, beacuse AbstractPluginManager
> imlement ServiceLocatorAwareInterface, so you can call getServiceLocator() to
> get ServiceManager instance. BUT! Zend\ServiceManager\
> FactoryInterface::createService use Zend\ServiceManager\ServiceLocatorInterface
> for $serviceLocator, so you can't call getServiceLocator() on it.

Not sure I understand. Can you provide an example of what you want to
work, and how it fails, please?

--
Matthew Weier O'Phinney
Project Lead            | [hidden email]
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc
Reply | Threaded
Open this post in threaded view
|

Re: How create factories for AbstractPluginManager's?

Александр Шелковский
Hello.

Thx for answer Matthew I try to explain what I mean.

For example, in our modules config we have key 'view_helpers', which allow register new view helpers: https://gist.github.com/3974392 . And all works just fine. But same config key allow use Zend\ServiceManager\FactoryInterface as factory for view helpers: https://gist.github.com/3974520 . So you can create a factory which must implement Zend\ServiceManager\FactoryInterface and create view helper in it, but in that case you will have some problems (check the code and comments in it): https://gist.github.com/3974486 .

So I want to ask, how it is better to create factories for AbstractPluginManagers (like zend HelperPluginManager)?

Regards,
Alex.
Reply | Threaded
Open this post in threaded view
|

Re: How create factories for AbstractPluginManager's?

weierophinney
Administrator
-- Александр Шелковский <[hidden email]> wrote
(on Monday, 29 October 2012, 11:21 PM +0700):

> Thx for answer Matthew I try to explain what I mean.
>
> For example, in our modules config we have key 'view_helpers', which allow
> register new view helpers: https://gist.github.com/3974392 . And all works just
> fine. But same config key allow use Zend\ServiceManager\FactoryInterface as
> factory for view helpers: https://gist.github.com/3974520 . So you can create a
> factory which must implement Zend\ServiceManager\FactoryInterface and create
> view helper in it, but in that case you will have some problems (check the code
> and comments in it): https://gist.github.com/3974486 .
>
> So I want to ask, how it is better to create factories for
> AbstractPluginManagers (like zend HelperPluginManager)?

99.99% of the time, you can depend on the service locator being passed
to a factory in a plugin manager being a service manager instance -- if
nothing else, because you're attaching it to a plugin manager instance,
and these extend ServiceManager.

If you want to program defensively, add a check:

    public function createService(ServiceLocatorInterface $helpers)
    {
        if (!$helpers instanceof ServiceManager) {
            // or test against AbstractPluginManager

            throw SomeException('detailing that we need services we cannot access');
        }
        $services = $helpers->getServiceLocator();
        // continue on...
    }

The main thing is to consider the context you're in, and whether there
will ever be a possibility where you attach the factory to something
other than the context-appropriate plugin manager.

--
Matthew Weier O'Phinney
Project Lead            | [hidden email]
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc