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.
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.