Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix - loadModule reinitializes modules if the module has already been… #34

Open
wants to merge 3 commits into
base: 2.12.x
Choose a base branch
from
Open
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
35 changes: 35 additions & 0 deletions src/ModuleManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

use Laminas\EventManager\EventManager;
use Laminas\EventManager\EventManagerInterface;
use ReflectionClass;
use Traversable;

use function class_exists;
use function current;
use function get_class;
use function is_array;
Expand Down Expand Up @@ -128,10 +130,26 @@ public function loadModule($module)
$module = current($module);
}

// search for a specific name
if (isset($this->loadedModules[$moduleName])) {
return $this->loadedModules[$moduleName];
}

// when no specific name is found, try the complete class-string
$verifiedModulName = $this->getVerifiedModuleName($moduleName);
if (isset($this->loadedModules[$verifiedModulName])) {
return $this->loadedModules[$verifiedModulName];
}

// when no class-string is found, try search for a namespace
if (class_exists($verifiedModulName)) {
$moduleReflection = new ReflectionClass($verifiedModulName);

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

/*
* Keep track of nested module loading using the $loadFinished
* property.
Expand Down Expand Up @@ -317,4 +335,21 @@ protected function attachDefaultListeners($events)
{
$events->attach(ModuleEvent::EVENT_LOAD_MODULES, [$this, 'onLoadModules']);
}

/**
* determines the class string of the module
*
* @param string $moduleName
* @return string
*/
private function getVerifiedModuleName($moduleName)
Copy link
Member

Choose a reason for hiding this comment

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

Add the types to the method because it is private and supported.

{
$verifiedModulName = $moduleName;

if (! class_exists($moduleName) && class_exists($moduleName . '\Module')) {
$verifiedModulName = $moduleName . '\Module';
}

return $verifiedModulName;
}
}
28 changes: 28 additions & 0 deletions test/ModuleManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,34 @@ public function testModuleLoadingBehavior()
self::assertSame(1, count($modules));
}

public function testModuleLoadingBehaviorWithModuleClassStrings()
{
$moduleManager = new ModuleManager(['SomeModule'], $this->events);
$this->defaultListeners->attach($this->events);
$modules = $moduleManager->getLoadedModules();
self::assertSame(0, count($modules));
$modules = $moduleManager->getLoadedModules(true);
self::assertSame(1, count($modules));
$moduleManager->loadModules(); // should not cause any problems
$moduleManager->loadModule(Module::class); // should not cause any problems
$modules = $moduleManager->getLoadedModules(true); // BarModule already loaded so nothing happens
self::assertSame(1, count($modules));
}

public function testModuleLoadingBehaviorWithModuleClassStringsVersion2()
{
$moduleManager = new ModuleManager([Module::class], $this->events);
$this->defaultListeners->attach($this->events);
$modules = $moduleManager->getLoadedModules();
self::assertSame(0, count($modules));
$modules = $moduleManager->getLoadedModules(true);
self::assertSame(1, count($modules));
$moduleManager->loadModules(); // should not cause any problems
$moduleManager->loadModule('SomeModule'); // should not cause any problems
$modules = $moduleManager->getLoadedModules(true); // BarModule already loaded so nothing happens
self::assertSame(1, count($modules));
}
Comment on lines +103 to +129
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 a data provider here and add some blank lines to break the wall of code / text.


public function testConstructorThrowsInvalidArgumentException()
{
$this->expectException(InvalidArgumentException::class);
Expand Down