Skip to content

Commit

Permalink
Move loading module by name in an other method
Browse files Browse the repository at this point in the history
  • Loading branch information
blanchonvincent committed Mar 8, 2013
1 parent 1ae15b0 commit 2055fb5
Showing 1 changed file with 33 additions and 20 deletions.
53 changes: 33 additions & 20 deletions library/Zend/ModuleManager/ModuleManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ public function loadModules()
/**
* Load a specific module by name.
*
* @param string|array $module
* @throws Exception\RuntimeException
* @param string|array $module
* @throws Exception\RuntimeException
* @triggers loadModule.resolve
* @triggers loadModule
* @return mixed Module's Module class
* @return mixed Module's Module class
*/
public function loadModule($module)
{
Expand All @@ -135,29 +135,18 @@ public function loadModule($module)
$moduleName = key($module);
$module = current($module);
}

if (isset($this->loadedModules[$moduleName])) {
return $this->loadedModules[$moduleName];
}

$event = ($this->loadFinished === false) ? clone $this->getEvent() : $this->getEvent();
$event->setModuleName($moduleName);

if (!is_object($module)) {
$this->loadFinished = false;

$result = $this->getEventManager()->trigger(ModuleEvent::EVENT_LOAD_MODULE_RESOLVE, $this, $event, function ($r) {
return (is_object($r));
});
$this->loadFinished = false;

$module = $result->last();

if (!is_object($module)) {
throw new Exception\RuntimeException(sprintf(
'Module (%s) could not be initialized.',
$moduleName
));
}
if (!is_object($module)) {
$module = $this->loadModuleByName($event);
}
$event->setModule($module);

Expand All @@ -169,17 +158,41 @@ public function loadModule($module)
return $module;
}

/**
* Load a module with the name
* @param Zend\EventManager\EventInterface $event
* @return mixed module instance
* @throws Exception\RuntimeException
*/
protected function loadModuleByName($event)
{
$result = $this->getEventManager()->trigger(ModuleEvent::EVENT_LOAD_MODULE_RESOLVE, $this, $event, function ($r) {
return (is_object($r));
});

$module = $result->last();
if (!is_object($module)) {
throw new Exception\RuntimeException(sprintf(
'Module (%s) could not be initialized.',
$event->getModuleName()
));
}

return $module;
}

/**
* Get an array of the loaded modules.
*
* @param bool $loadModules If true, load modules if they're not already
* @param bool $loadModules If true, load modules if they're not already
* @return array An array of Module objects, keyed by module name
*/
public function getLoadedModules($loadModules = false)
{
if (true === $loadModules) {
$this->loadModules();
}

return $this->loadedModules;
}

Expand Down

0 comments on commit 2055fb5

Please sign in to comment.