Setting up additional Service Managers for custom plug-ins

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

Setting up additional Service Managers for custom plug-ins

demiankatz

Hello,

 

I would like to create some special-purpose service managers for my application so that I can load application-specific plug-ins in the same way that the framework loads view helpers, controller plug-ins, etc.

 

I have found the code in Zend\Mvc\Service\ModuleManagerFactory which sets up the existing managers, but I wasn’t sure how best to extend this to add my own.

 

I tried grabbing the ServiceListener in my module’s onBootstrap method and calling addServiceManager(), but that has no effect (presumably my call is occurring too late to actually do anything).  Perhaps I need to do this from init() instead, but if so, I wasn’t sure what event to hook.

 

Tracing through the code seems to suggest that the hard-coded list of managers is tightly bound up with the logic that initializes them, so perhaps this isn’t as extensible as it could be… though it’s also entirely possible I’m not understanding the code well enough.

 

Is there a simple way to do this that I am overlooking, or does some of this code need to be refactored to make it easier to attach additional managers?

 

thanks,

Demian

Reply | Threaded
Open this post in threaded view
|

Re: Setting up additional Service Managers for custom plug-ins

Jurian Sluiman
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

RE: Setting up additional Service Managers for custom plug-ins

demiankatz

Thanks – this should be a big help in getting me started.

 

However, one thing that’s still unclear -- the code in Zend\Mvc\Service\ModuleManagerFactory uses an addServiceManager() method of the ServiceListener to set up some useful things: the config section used for the manager, the module method that can be used to obtain manager-specific settings, and the expected interface for the plug-ins.  Is there any way I can benefit from this method, or do I just have to set up all of that stuff manually?

 

In any case, I’ll start experimenting with this tomorrow…  but if you’re aware of any shortcuts for integrating the managers into the module system (i.e. for overriding default settings in a secondary module), it would be good to know.

 

- Demian

 

All plugin managers (for example, the controller plugin manager and the view helper manager) extend the Zend\ServiceManager\AbstractPluginManager class. If you want to have your own plugin manager, I suggest you create an arbitrary class ("Demian\ServiceManager\ApplicationPluginManager") which extends the AbstractPluginManager (mind the abstract validatePlugin() method in there).

 

In your "DemianApplication" module you instantiate the plugin manager on bootstrap (just with $manager = new ApplicationPluginManager or with a factory) and set this as a service inside the main service manager: Inside your bootstrap:

 

public function onBoostrap($e)

{

  $app = $e->getApplication();

  $sm  = $app->getServiceManager();

 

  $myPluginManager = createTheManager(); // your instantiation here

  $sm->setService('ApplicationPluginManager', $myPluginManager);

}

 

When your classes need the application plugin manager, just inject the ApplicationPluginManager with a factory or pull it from the service locator if your class has the main service manager. Inside your custom plugin manager you can already set up some default services, just like the controller plugin manager of the view helper manager.

 

Note that the above onBootstrap() method should reside in a module registered earlier than the modules pulling the ApplicationPluginManager from the service locator.

-- 

Jurian Sluiman

Reply | Threaded
Open this post in threaded view
|

RE: Setting up additional Service Managers for custom plug-ins

demiankatz
To (at least partially) answer my own question, I see now that I have easy access to the merged configuration by using the application's getConfig() method within my module's onBootstrap method...  so it's easy enough to put all my configurations in the module config and then load them in onBootstrap.

Or, in case anyone needs a tangible example, here's the diff where I figured it out:

http://vufind.git.sourceforge.net/git/gitweb.cgi?p=vufind/vufind;a=commitdiff;h=cd0979bc0f1b7ee4272105c627e8bb9649b5ac57

- Demian

From: Demian Katz
Sent: Tuesday, August 28, 2012 2:29 PM
To: Jurian Sluiman
Cc: [hidden email]
Subject: RE: [zf-contributors] Setting up additional Service Managers for custom plug-ins

Thanks – this should be a big help in getting me started.

 

However, one thing that’s still unclear -- the code in Zend\Mvc\Service\ModuleManagerFactory uses an addServiceManager() method of the ServiceListener to set up some useful things: the config section used for the manager, the module method that can be used to obtain manager-specific settings, and the expected interface for the plug-ins.  Is there any way I can benefit from this method, or do I just have to set up all of that stuff manually?

 

In any case, I’ll start experimenting with this tomorrow…  but if you’re aware of any shortcuts for integrating the managers into the module system (i.e. for overriding default settings in a secondary module), it would be good to know.

 

- Demian

 

All plugin managers (for example, the controller plugin manager and the view helper manager) extend the Zend\ServiceManager\AbstractPluginManager class. If you want to have your own plugin manager, I suggest you create an arbitrary class ("Demian\ServiceManager\ApplicationPluginManager") which extends the AbstractPluginManager (mind the abstract validatePlugin() method in there).

 

In your "DemianApplication" module you instantiate the plugin manager on bootstrap (just with $manager = new ApplicationPluginManager or with a factory) and set this as a service inside the main service manager: Inside your bootstrap:

 

public function onBoostrap($e)

{

  $app = $e->getApplication();

  $sm  = $app->getServiceManager();

 

  $myPluginManager = createTheManager(); // your instantiation here

  $sm->setService('ApplicationPluginManager', $myPluginManager);

}

 

When your classes need the application plugin manager, just inject the ApplicationPluginManager with a factory or pull it from the service locator if your class has the main service manager. Inside your custom plugin manager you can already set up some default services, just like the controller plugin manager of the view helper manager.

 

Note that the above onBootstrap() method should reside in a module registered earlier than the modules pulling the ApplicationPluginManager from the service locator.

-- 

Jurian Sluiman