As I delve deeper and deeper into Zf2 I find myself constantly Googling to find answers of best approaches to use when developing with Zf2. Unfortunately given the framework is still early on in its life many of these questions have yet to be asked (… or if they have been asked there sometimes aren’t answers).
One such question I was looking to solve was, how exactly do you create your own custom plugin manager in Zf2?
At first this seemed like a rather trivial task, inject the plugin manager configuration into the service manager using the module.config.php file. This seemed like too much overhead and has some unwanted limitations (eg. injecting plugins via something like getControllerPluginConfig in a Module.php file).
Looking at how the existing plugin managers are configured in Zend\Mvc\Service\ModuleManagerFactory it seemed like something that could be done easily enough to do in the onBootstrap function of the module containing the plugin manager. Unfortunately this didn’t work because onBootstrap is called too late in the initialization process for the plugin manager to be constructed correctly. Finally, I discovered the init function which is called early enough for the plugin manager to be constructed correctly, below is an example solution.