Question

When I trace the code in zf2 , I can't find where the Application service registers. Here is the code in application.php

public static function init($configuration = array())
{
    $smConfig = isset($configuration['service_manager']) ? $configuration['service_manager'] : array();
    $serviceManager = new ServiceManager(new Service\ServiceManagerConfig($smConfig));
    $serviceManager->setService('ApplicationConfig', $configuration);
    $serviceManager->get('ModuleManager')->loadModules();
    return $serviceManager->get('Application')->bootstrap();
}

The code "$serviceManager->get('Application')" is to get the Application service. But where did the application service register?

Then I find that the Application service is related with the code line "$this->getEventManager()->trigger(ModuleEvent::EVENT_LOAD_MODULES_POST, $this, $this->getEvent())" in ZF2/library/Zend/MoudleManager/MoudleManager.php

public function loadModules()
{
    if (true === $this->modulesAreLoaded) {
        return $this;
    }

    $this->getEventManager()->trigger(ModuleEvent::EVENT_LOAD_MODULES, $this, $this->getEvent());

    /**
     * Having a dedicated .post event abstracts the complexity of priorities from the user.
     * Users can attach to the .post event and be sure that important
     * things like config merging are complete without having to worry if
     * they set a low enough priority.
     */
    $this->getEventManager()->trigger(ModuleEvent::EVENT_LOAD_MODULES_POST, $this, $this->getEvent());

    return $this;
}

The another question is that "ModuleEvent::EVENT_LOAD_MODULES_POST,That is loadModules.post.The first param of the trigger function is a function name. So is the loadModules.post a function ? where was it defined ?

Thanks in advance.

Was it helpful?

Solution

I'll address the last question first. The first param of the trigger method is not a function, it's simply a name; by convention, that name usually mirrors the method in which it is triggered, optionally with a suffix to give more context (such as ".pre", ".post", etc.).

"loadModules.post" is an event that is triggered by ModuleManager::loadModules() once all modules have been loaded. When an event is triggered, any listeners on that event will then be triggered with the parameters provided. The event object itself will also have a "target", which will be the ModuleManager in this case.

Regarding the "Application" service, you have to look into the internals of the MVC layer. Most MVC services are defined in Zend\Mvc\Service\ServiceListenerFactory. If you look in that class, you'll see that it assigns Application to use the Zend\Mvc\Service\ApplicationFactory to create an Application instance. The ServiceListenerFactory is retrieved as part of the factory that creates the ModuleManager. It's a little indirect, but the relations are defined by the order of operations and the relations between the objects.

OTHER TIPS

Most of the Zend\ServiceManager\ServiceManager registered service are configured by the ServiceListener factory Zend\Mvc\Service\ServiceListenerFactory when creating a ServiceListener instance.

The default config is actually stored in ServiceListenerFactory::defaultServiceConfig. (If you go through the code you'll see that the 'Application' Alias is being defined there)

For you second question, the ModuleEvent::EVENT_LOAD_MODULES is just one of the ModuleEvent constants that are used to identify the different Modules loading events.

There are actually four module Events triggered at different stages by the application. I didn't have yet the opportunity to use those events, and I think that they're mostly used by the framework itself. So far, I had opportunities to use Zend\Mvc\MvcEvent related events only.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top