From 3df8f36c2dd72841e9a1de9acd19c1526830373b Mon Sep 17 00:00:00 2001 From: Sebastian Hopfe Date: Fri, 8 Apr 2022 15:10:34 +0200 Subject: [PATCH] fix - loadModule reinitializes modules if the module has already been loaded #32 Signed-off-by: Sebastian Hopfe --- src/ModuleManager.php | 33 +++++++++++++++++++++++++++++++++ test/ModuleManagerTest.php | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/src/ModuleManager.php b/src/ModuleManager.php index d388ba3..39e6227 100644 --- a/src/ModuleManager.php +++ b/src/ModuleManager.php @@ -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. @@ -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; + } } diff --git a/test/ModuleManagerTest.php b/test/ModuleManagerTest.php index d92dfc3..ec6d504 100644 --- a/test/ModuleManagerTest.php +++ b/test/ModuleManagerTest.php @@ -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);