Skip to content

Instantly share code, notes, and snippets.

@PierreRambaud
Created July 31, 2013 06:37
Show Gist options
  • Select an option

  • Save PierreRambaud/6119841 to your computer and use it in GitHub Desktop.

Select an option

Save PierreRambaud/6119841 to your computer and use it in GitHub Desktop.
Load ZF2 module in module
<?php
namespace Service;
use Zend\ModuleManager\Listener;
use Zend\ModuleManager\ModuleEvent;
use Zend\ModuleManager\ModuleManager;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class ModuleManagerFactory implements FactoryInterface
{
/**
* Creates and returns the module manager
*
* Instantiates the default module listeners, providing them configuration
* from the "module_listener_options" key of the ApplicationConfig
* service. Also sets the default config glob path.
*
* Module manager is instantiated and provided with an EventManager, to which
* the default listener aggregate is attached. The ModuleEvent is also created
* and attached to the module manager.
*
* @param ServiceLocatorInterface $serviceLocator Service Manager
*
* @return ModuleManager
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
$application = $serviceLocator->get('Application');
$configuration = $serviceLocator->get('ApplicationConfig');
$listenerOptions = new Listener\ListenerOptions($configuration['module_listener_options']);
$defaultListeners = new Listener\DefaultListenerAggregate($listenerOptions);
$serviceListener = new Listener\ServiceListener($serviceLocator);
$serviceListener->addServiceManager(
$serviceLocator,
'service_manager',
'Zend\ModuleManager\Feature\ServiceProviderInterface',
'getServiceConfig'
);
$serviceListener->addServiceManager(
'ControllerLoader',
'controllers',
'Zend\ModuleManager\Feature\ControllerProviderInterface',
'getControllerConfig'
);
$serviceListener->addServiceManager(
'ControllerPluginManager',
'controller_plugins',
'Zend\ModuleManager\Feature\ControllerPluginProviderInterface',
'getControllerPluginConfig'
);
$serviceListener->addServiceManager(
'ViewHelperManager',
'view_helpers',
'Zend\ModuleManager\Feature\ViewHelperProviderInterface',
'getViewHelperConfig'
);
$serviceListener->addServiceManager(
'ValidatorManager',
'validators',
'Zend\ModuleManager\Feature\ValidatorProviderInterface',
'getValidatorConfig'
);
$serviceListener->addServiceManager(
'FilterManager',
'filters',
'Zend\ModuleManager\Feature\FilterProviderInterface',
'getFilterConfig'
);
$serviceListener->addServiceManager(
'FormElementManager',
'form_elements',
'Zend\ModuleManager\Feature\FormElementProviderInterface',
'getFormElementConfig'
);
$serviceListener->addServiceManager(
'RoutePluginManager',
'route_manager',
'Zend\ModuleManager\Feature\RouteProviderInterface',
'getRouteConfig'
);
$serviceListener->addServiceManager(
'SerializerAdapterManager',
'serializers',
'Zend\ModuleManager\Feature\SerializerProviderInterface',
'getSerializerConfig'
);
$serviceListener->addServiceManager(
'HydratorManager',
'hydrators',
'Zend\ModuleManager\Feature\HydratorProviderInterface',
'getHydratorConfig'
);
$serviceListener->addServiceManager(
'InputFilterManager',
'input_filters',
'Zend\ModuleManager\Feature\InputFilterProviderInterface',
'getInputFilterConfig'
);
$moduleManager = new ModuleManager($configuration['modules'], $application->getEventManager());
$moduleManager->getEventManager()->attachAggregate($defaultListeners);
$moduleManager->getEventManager()->attachAggregate($serviceListener);
$moduleManager->loadModules();
$config = $moduleManager->getEvent()->getConfigListener()->getMergedConfig(false);
if (is_array($config) && isset($config['view_manager'])) {
$templatePathStack = $serviceLocator->get('ViewTemplatePathStack');
$config = $config['view_manager'];
if (is_array($config)) {
if (isset($config['template_path_stack'])) {
$templatePathStack->addPaths($config['template_path_stack']);
}
if (isset($config['default_template_suffix'])) {
$templatePathStack->setDefaultSuffix($config['default_template_suffix']);
}
}
}
foreach ($moduleManager->getLoadedModules() as $module) {
if (method_exists($module, 'onBootstrap')) {
$module->onBootstrap($application->getMvcEvent());
}
}
return $moduleManager;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment