-
Notifications
You must be signed in to change notification settings - Fork 27
ModuleManager load module childs dependentsLoad childs modules #50
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,9 +40,9 @@ class ModuleManager implements ModuleManagerInterface | |
protected $event; | ||
|
||
/** | ||
* @var bool | ||
* @var int | ||
*/ | ||
protected $loadFinished; | ||
protected $loadFinished = 0; | ||
|
||
/** | ||
* modules | ||
|
@@ -51,6 +51,18 @@ class ModuleManager implements ModuleManagerInterface | |
*/ | ||
protected $modules = []; | ||
|
||
/** | ||
* Childs dependency modules | ||
* | ||
* @var array | ||
*/ | ||
protected $childsModules = []; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $parentModule; | ||
|
||
/** | ||
* True if modules have already been loaded | ||
* | ||
|
@@ -100,6 +112,24 @@ public function onLoadModules() | |
$this->modulesAreLoaded = true; | ||
} | ||
|
||
/** | ||
* Load childs dependency modules | ||
* | ||
* @param ModuleEvent $e | ||
* @return void | ||
*/ | ||
public function onLoadChildsModules(ModuleEvent $e) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be renamed to |
||
{ | ||
if (!$this->parentModule || !isset($this->childsModules[$this->parentModule])) { | ||
return; | ||
} | ||
|
||
foreach ($this->childsModules[$this->parentModule] as $module) { | ||
$this->loadModule($module); | ||
} | ||
unset($this->childsModules[$this->parentModule]); | ||
} | ||
|
||
/** | ||
* Load the provided modules. | ||
* | ||
|
@@ -140,7 +170,7 @@ public function loadModules() | |
* @triggers loadModule | ||
* @return mixed Module's Module class | ||
*/ | ||
public function loadModule($module) | ||
public function loadModule($module, $afterCurrent = false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Document the new |
||
{ | ||
$moduleName = $module; | ||
if (is_array($module)) { | ||
|
@@ -162,8 +192,12 @@ public function loadModule($module) | |
* To load a module, we clone the event if we are inside a nested | ||
* loadModule() call, and use the original event otherwise. | ||
*/ | ||
if (!isset($this->loadFinished)) { | ||
$this->loadFinished = 0; | ||
if ($this->loadFinished > 0 && $this->parentModule && $afterCurrent) { | ||
$childModule = is_object($module) | ||
? [$moduleName=>$module] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Run this against phpcs; we require spaces around all operators. |
||
: $moduleName; | ||
$this->childsModules[$this->parentModule][] = $childModule; | ||
return; | ||
} | ||
|
||
$event = ($this->loadFinished > 0) ? clone $this->getEvent() : $this->getEvent(); | ||
|
@@ -178,6 +212,7 @@ public function loadModule($module) | |
$event->setName(ModuleEvent::EVENT_LOAD_MODULE); | ||
|
||
$this->loadedModules[$moduleName] = $module; | ||
$this->parentModule = $event->getModuleName(); | ||
$this->getEventManager()->triggerEvent($event); | ||
|
||
$this->loadFinished--; | ||
|
@@ -339,5 +374,6 @@ public function getEventManager() | |
protected function attachDefaultListeners($events) | ||
{ | ||
$events->attach(ModuleEvent::EVENT_LOAD_MODULES, [$this, 'onLoadModules']); | ||
$events->attach(ModuleEvent::EVENT_LOAD_MODULE, [$this, 'onLoadChildsModules'], -100); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,7 @@ public function testDefaultListenerAggregateCanAttachItself() | |
'Zend\ModuleManager\Listener\OnBootstrapListener', | ||
'Zend\ModuleManager\Listener\ConfigListener', | ||
'Zend\ModuleManager\Listener\LocatorRegistrationListener', | ||
'Zend\ModuleManager\ModuleManager', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this already be registered (due to registration of the |
||
], | ||
]; | ||
foreach ($expectedEvents as $event => $expectedListeners) { | ||
|
@@ -86,12 +87,12 @@ public function testDefaultListenerAggregateCanDetachItself() | |
$moduleManager = new ModuleManager(['ListenerTestModule']); | ||
$events = $moduleManager->getEventManager(); | ||
|
||
$this->assertEquals(1, count($this->getEventsFromEventManager($events))); | ||
$this->assertEquals(2, count($this->getEventsFromEventManager($events))); | ||
|
||
$listenerAggregate->attach($events); | ||
$this->assertEquals(4, count($this->getEventsFromEventManager($events))); | ||
|
||
$listenerAggregate->detach($events); | ||
$this->assertEquals(1, count($this->getEventsFromEventManager($events))); | ||
$this->assertEquals(2, count($this->getEventsFromEventManager($events))); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace LoadChildsModule; | ||
|
||
|
||
class Module | ||
{ | ||
public function init($moduleManager) | ||
{ | ||
$moduleManager->loadModule('LoadChildsModule2', 'after'); | ||
$moduleManager->loadModule(['SomeModule' => new \SomeModule\Module()], 'after'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't the second argument be a boolean |
||
} | ||
|
||
public function getConfig() | ||
{ | ||
return [ | ||
'bar' => 'SubModule', | ||
]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace LoadChildsModule2; | ||
|
||
|
||
class Module | ||
{ | ||
public function init($moduleManager) | ||
{ | ||
$moduleManager->loadModule('BarModule', 'after'); | ||
} | ||
|
||
public function getConfig() | ||
{ | ||
return [ | ||
'bar' => 'SubModule', | ||
]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?php | ||
return [ | ||
'some' => 'thing', | ||
]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use
$childModules
throughout ("childs" is not grammatically correct).