Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

ModuleManager load module childs dependentsLoad childs modules #50

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions src/ModuleManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ class ModuleManager implements ModuleManagerInterface
protected $event;

/**
* @var bool
* @var int
*/
protected $loadFinished;
protected $loadFinished = 0;

/**
* modules
Expand All @@ -51,6 +51,18 @@ class ModuleManager implements ModuleManagerInterface
*/
protected $modules = [];

/**
* Childs dependency modules
*
* @var array
*/
protected $childsModules = [];
Copy link
Member

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


/**
* @var string
*/
protected $parentModule;

/**
* True if modules have already been loaded
*
Expand Down Expand Up @@ -100,6 +112,24 @@ public function onLoadModules()
$this->modulesAreLoaded = true;
}

/**
* Load childs dependency modules
*
* @param ModuleEvent $e
* @return void
*/
public function onLoadChildsModules(ModuleEvent $e)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be renamed to onLoadChildModules.

{
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.
*
Expand Down Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Document the new $afterCurrent argument, please. What are valid values?

{
$moduleName = $module;
if (is_array($module)) {
Expand All @@ -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]
Copy link
Member

Choose a reason for hiding this comment

The 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();
Expand All @@ -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--;
Expand Down Expand Up @@ -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);
}
}
4 changes: 2 additions & 2 deletions test/Listener/ConfigListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,12 +346,12 @@ public function testConfigListenerFunctionsAsAggregateListener()

$moduleManager = $this->moduleManager;
$events = $moduleManager->getEventManager();
$this->assertEquals(2, count($this->getEventsFromEventManager($events)));
$this->assertEquals(3, count($this->getEventsFromEventManager($events)));

$configListener->attach($events);
$this->assertEquals(4, count($this->getEventsFromEventManager($events)));

$configListener->detach($events);
$this->assertEquals(2, count($this->getEventsFromEventManager($events)));
$this->assertEquals(3, count($this->getEventsFromEventManager($events)));
}
}
5 changes: 3 additions & 2 deletions test/Listener/DefaultListenerAggregateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function testDefaultListenerAggregateCanAttachItself()
'Zend\ModuleManager\Listener\OnBootstrapListener',
'Zend\ModuleManager\Listener\ConfigListener',
'Zend\ModuleManager\Listener\LocatorRegistrationListener',
'Zend\ModuleManager\ModuleManager',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this already be registered (due to registration of the onLoadModules listener)? Why is this change necessary?

],
];
foreach ($expectedEvents as $event => $expectedListeners) {
Expand All @@ -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)));
}
}
17 changes: 17 additions & 0 deletions test/ModuleManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,21 @@ public function testGetEventWillLazyLoadOneWithTargetSetToModuleManager()
$this->assertInstanceOf(ModuleEvent::class, $event);
$this->assertSame($moduleManager, $event->getTarget());
}

public function testLoadChildsModules()
{
$moduleManager = new ModuleManager(['LoadChildsModule'], $this->events);
$this->defaultListeners->attach($this->events);
$moduleManager->loadModules();

$this->assertSame(
['LoadChildsModule', 'LoadChildsModule2', 'BarModule', 'SomeModule'],
array_keys($moduleManager->getLoadedModules())
);

$this->assertArraySubset(
['bar' => 'foo', 'some' => 'thing'],
$this->defaultListeners->getConfigListener()->getMergedConfig(false)
);
}
}
27 changes: 27 additions & 0 deletions test/TestAsset/LoadChildsModule/Module.php
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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the second argument be a boolean true? While non-empty strings evaluate as truthy, using them for boolean conditionals is brittle.

}

public function getConfig()
{
return [
'bar' => 'SubModule',
];
}
}
26 changes: 26 additions & 0 deletions test/TestAsset/LoadChildsModule2/Module.php
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',
];
}
}
4 changes: 4 additions & 0 deletions test/TestAsset/LoadChildsModule2/configs/config-c832f68.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
return [
'some' => 'thing',
];