http://www.franzdeleon.me/2013/09/how-does-servicemanager-plugin-get.html
So how do we register our own custom ServiceManager Plugin?
1.) First create a Plugin Manager:
use Zend\ServiceManager\AbstractPluginManager;
class CustomPluginManager extends AbstractPluginManager
{
public function validatePlugin($plugin)
{
if (!$plugin instanceof CustomeInterface) {
throw new ErrorException('Wrong intance of plugin');
}
}
}
2.) Then create a ServiceManager Factory that returns an instance of your just created CustomPluginManager:use Zend\Mvc\Service\AbstractPluginManagerFactory;
class CustomManagerFactory extends AbstractPluginManagerFactory
{
const PLUGIN_MANAGER_CLASS = 'Namespace\to\CustomPluginManager';
public function createService(ServiceLocatorInterface $serviceLocator)
{
$plugins = parent::createService($serviceLocator);
return $plugins;
}
}
3.) Now lets define an interface for our plugin config:interface CustomPluginProviderInterface
{
public function getCustomPlugin();
}
4.) Then we need to put this all together by registering our CustomPlugin plugin in Module.php. We first need to register our Factory:
//inside Module.php
public function getServiceConfig()
{
return array(
'factories' => array(
'customPluginManager' => 'namespace\to\CustomManagerFactory',
),
)
}
5.) In order for the Plugin to be created as a plugin manager, we need to register it to the Service Listener. We do this via init() method. It is important to put this inside init() as this is called before the MVC bootstrapping.// inside Module.php
public function init(ModuleManager $moduleManager)
{
$sm = $moduleManager->getEvent()->getParam('ServiceManager');
$serviceListener = $sm->get('ServiceListener');
$serviceListener->addServiceManager(
'customPluginManager',
'custom_plugin_config',
'namespace\to\CustomPluginProviderInterface',
'getCustomPluginConfig'
);
}
6.) Now you can register to your custom plugin from other modules!
// in some other Module.php
public function getCustomPluginConfig()
{
return array(
'invokables' => array(
'AclService' => 'namespace/to/AclService'
)
)
}
7.) Then you can call it for example inside a controller:
// inside controller.php in some other module
public function someAction()
{
$aclService = $this->getServiceLocator()->get('customPluginManager')->get('AclService');
}
That is pretty much it!
Reference:
http://raing3.gshi.org/2013/05/26/creating-custom-plugin-manager-in-zend-framework-2/