Hopefully this post will explain and answer this question.
For this example, I will trace the initialization of the FilterManager Plugin.
For those who are not familiar with the plugin functionality of ZF2, it is basically an extension of the Service Manager that you can use for organization and other things.
As an example, you are calling a plugin manager when you do something like below:
// from Controller
$stringTrimFilter = $this->getServiceLocator()->get('FilterManager')->get('stringTrim');
// from inside a View. basePath is a View Helper Plugin
$this->basePath('/some/uri');
Now lets get down to business. So how does a plugin gets created and initialized?
1.) We will start from the ModuleManagerFactory for simplicity's sake. In the ModuleManagerFactory (
https://github.com/zendframework/zf2/blob/master/library/Zend/Mvc/Service/ModuleManagerFactory.php) the Service Listener Factory gets initialized. (see line 38 and called on 44)
2.) In the Service Listener Factory (
https://github.com/zendframework/zf2/blob/master/library/Zend/Mvc/Service/ServiceListenerFactory.php) a bunch of Plugin Factories gets initialized also.
One of the plugin factories that gets initialized is the FilterManager factory which looks like this. See line 54:
'FilterManager' => 'Zend\Mvc\Service\FilterManagerFactory',
3.) Lets go to the FilterManagerFactory (
https://github.com/zendframework/zf2/blob/master/library/Zend/Mvc/Service/FilterManagerFactory.php).The class is basically a ServiceManager Factory implementation class.
4.) The FilterManagerFactory file extends AbstractPluginManagerFactory (
https://github.com/zendframework/zf2/blob/master/library/Zend/Mvc/Service/AbstractPluginManagerFactory.php) which is basically an implementation of FactoryInterface.php. That means we are instantiating an object. What object?
5) The FilterManagerFactory will INSTANTIATE and RETURN an instance of FilterPluginManager (
https://github.com/zendframework/zf2/blob/master/library/Zend/Filter/FilterPluginManager.php) see line 17:
const PLUGIN_MANAGER_CLASS = 'Zend\Filter\FilterPluginManager';
6.) Now an instance of FilterPluginManager is created. It is important to note that FilterPluginManager extends AbstractPluginManager (
https://github.com/zendframework/zf2/blob/master/library/Zend/ServiceManager/AbstractPluginManager.php) which is basically an own ServiceManager class!
The FilterPluginManager class also invokes a bunch of ZF2 filters. The class also gets mapped to the service key "FilterManager" key. Take a look at step 2 again.
Moreover this class implements the abstract method
validatePlugin() which is used to validate the actual plugin object that you will inject to this manager like for example, StringTrim!
public function validatePlugin($plugin)
{
if ($plugin instanceof FilterInterface) {
// we're okay
return;
}
if (is_callable($plugin)) {
// also okay
return;
}
throw new Exception\RuntimeException(sprintf(
'Plugin of type %s is invalid; must implement %s\FilterInterface or be callable',
(is_object($plugin) ? get_class($plugin) : gettype($plugin)),
__NAMESPACE__
));
}
7.) Now we are back in the ModuleManagerFactory (see step 1). Since we now have an instance of the FilterPluginManager via the key FilterManager, this instance is added to the Service Listener (see line 76 of ModuleManagerFactory.php) or below:
$serviceListener->addServiceManager(
'FilterManager',
'filters',
'Zend\ModuleManager\Feature\FilterProviderInterface',
'getFilterConfig'
);
8.) Lets take a look at the FilterProviderInterface (
https://github.com/zendframework/zf2/blob/master/library/Zend/ModuleManager/Feature/FilterProviderInterface.php) which difines the method
getFilterConfig(). What is this method again? This is where you register your custom filters remember in Module.php?
public function getFilterConfig()
{
return array(
'factories' => array(
'customFilter' => function ($sm) {
// create your filter...
}
),
)
}
9.) Thats pretty much the workflow and registration of a ServiceManager Plugin. Now you can call it like this:
$stringTrimFilter = $this->getServiceLocator()->get('FilterManager')->get('stringTrim');
// or your custom filter
$customfilter = $this->getServiceLocator()->get('FilterManager')->get('customFilter');