Previous Next

The Module Manager

The module manager, Zend\Module\Manager, is a very simple class which is responsible for iterating over an array of module names and triggering a sequence of events for each. The module manager is simply responsible for looping through the module names provided to it and firing events; location and instantiation of module classes, and initialization and configuration of module classes, is performed by attached event listeners.

Module Manager Events

Events triggered by Zend\Module\Manager
loadModules.pre

Triggered prior to any modules being loaded. This event is useful for registering a module autoloader. ZF2 ships with a default module autoloader which is automatically attached to this event if you are using Zend\Module\Listener\DefaultListenerAggregate, both of which are covered in detail later.

loadModule.resolve

Triggered for each module that is to be loaded. The listener(s) to this event are responsible for taking a module name and resolving it to an instance of some class. The default module resolver shipped with ZF2 simply looks for the class {modulename}\Module, instantiating and returning it if it exists.

The name of the module may be retrieved by listeners using the getModuleName() method of the Event object; a listener should then take that name and resolve it to an object instance representing the given module. Multiple listeners can be attached to this event, and the module manager will trigger them in order of their priority until one returns an object. This allows you to attach additional listeners which have alternative methods of resolving modules from a given module name.

loadModule

Once a module resolver listener has resolved the module name to an object, the module manager then triggers this event, passing the newly created object to all listeners.

loadModules.post

This event is triggered by the module manager to allow any listeners to perform work after every module has finished loading. For example, the default configuration listener, Zend\Module\Listener\ConfigListener (covered later), attaches to this event to merge additional user-supplied configuration which is meant to override the default supplied configurations of installed modules.

Module Manager Listeners

By default, Zend Framework provides several useful module manager listeners.

Provided Module Manager Listeners
Zend\Module\Listener\DefaultListenerAggregate

To help simplify the most common use case of the module manager, ZF2 provides this default aggregate listener. In most cases, this will be the only listener you will need to attach to use the module manager, as it will take care of properly attaching the requisite listeners (those listed below) for the module system to function properly.

Zend\Module\Listener\ModuleResolverListener

This is the default module resolver. It attaches to the "loadModule.resolve" event and simply returns an instance of {moduleName}\Module.

Zend\Module\Listener\ConfigListener

If a module class has a getConfig() method, this listener will call it and merge the returned array (or Traversable object) into the main application configuration.

Zend\Module\Listener\InitTrigger

If a module class has an init() method, this listener will call it and pass the current instance of Zend\Module\Manager as the sole parameter. The init() method is called for every module on every page request and should only be used for performing lightweight tasks such as registering event listeners.

Zend\Module\Listener\AutoloaderListener

This listener checks each module to see if it has implemented the Zend\Module\Consumer\AutoloaderProvider interface. If so, it calls the getAutoloaderConfig() method on the module class and passes the returned array to Zend\Loader\AutoloaderFactory.

Previous Next