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 3df8f36 commit 4b9bcae
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
10 changes: 6 additions & 4 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 @@ -141,7 +143,7 @@ public function loadModule($module)

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

if (isset($this->loadedModules[$moduleReflection->getNamespaceName()])) {
return $this->loadedModules[$moduleReflection->getNamespaceName()];
Expand Down Expand Up @@ -337,15 +339,15 @@ protected function attachDefaultListeners($events)
/**
* determines the class string of the module
*
* @param $moduleName
* @param string $moduleName
* @return string
*/
private function getVerifiedModuleName($moduleName)
{
$verifiedModulName = $moduleName;

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

return $verifiedModulName;
Expand Down
4 changes: 2 additions & 2 deletions test/ModuleManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,14 @@ public function testModuleLoadingBehaviorWithModuleClassStrings()


$moduleManager->loadModules(); // should not cause any problems
$moduleManager->loadModule(\SomeModule\Module::class); // 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([\SomeModule\Module::class], $this->events);
$moduleManager = new ModuleManager([Module::class], $this->events);
$this->defaultListeners->attach($this->events);
$modules = $moduleManager->getLoadedModules();
self::assertSame(0, count($modules));
Expand Down

0 comments on commit 4b9bcae

Please sign in to comment.