Skip to content

Commit

Permalink
fix - loadModule reinitializes modules if the module has already been…
Browse files Browse the repository at this point in the history
… loaded laminas#32

Signed-off-by: Sebastian Hopfe <[email protected]>
  • Loading branch information
nusphere authored and Sebastian Hopfe committed Apr 8, 2022
1 parent 2a3d943 commit 3df8f36
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/ModuleManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,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 +333,21 @@ protected function attachDefaultListeners($events)
{
$events->attach(ModuleEvent::EVENT_LOAD_MODULES, [$this, 'onLoadModules']);
}

/**
* determines the class string of the module
*
* @param $moduleName
* @return string
*/
private function getVerifiedModuleName($moduleName)
{
$verifiedModulName = $moduleName;

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

return $verifiedModulName;
}
}
32 changes: 32 additions & 0 deletions test/ModuleManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,38 @@ 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(\SomeModule\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([\SomeModule\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));
}

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

0 comments on commit 3df8f36

Please sign in to comment.