From bfc457115ca06b4313a3572610473d14e748e223 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Wed, 19 Sep 2018 21:33:10 +0200 Subject: [PATCH] Split controllers per application --- Dispatcher/Dispatcher.php | 8 +------- Localization/L11nManager.php | 13 +++++++++++-- Module/ModuleFactory.php | 2 +- Module/ModuleManager.php | 2 +- tests/Account/AccountTest.php | 2 +- tests/Dispatcher/DispatcherTest.php | 2 +- tests/Localization/L11nManagerTest.php | 10 +++++----- tests/Localization/LocalizationTest.php | 2 +- tests/Module/ModuleFactoryTest.php | 4 ++-- tests/Module/ModuleManagerTest.php | 5 +++-- tests/Views/ViewTest.php | 5 +++-- 11 files changed, 30 insertions(+), 25 deletions(-) diff --git a/Dispatcher/Dispatcher.php b/Dispatcher/Dispatcher.php index 184a4d1bd..e0bd401ed 100644 --- a/Dispatcher/Dispatcher.php +++ b/Dispatcher/Dispatcher.php @@ -182,13 +182,7 @@ final class Dispatcher private function getController(string $controller) : object { if (!isset($this->controllers[$controller])) { - // If module controller use module manager for initialization - if (\strpos('\Modules\Controller', $controller) === 0) { - $split = \explode('\\', $controller); - $this->controllers[$controller] = $this->app->moduleManager->get($split[2]); - } else { - $this->controllers[$controller] = new $controller($this->app); - } + $this->controllers[$controller] = new $controller($this->app); } return $this->controllers[$controller]; diff --git a/Localization/L11nManager.php b/Localization/L11nManager.php index 04a5f5eec..da889acad 100644 --- a/Localization/L11nManager.php +++ b/Localization/L11nManager.php @@ -36,13 +36,22 @@ final class L11nManager */ private $language = []; + /** + * App Name. + * + * @var string + * @since 1.0.0 + */ + private $appName = ''; + /** * Construct. * * @since 1.0.0 */ - public function __construct() + public function __construct(string $appName) { + $this->appName = $appName; } /** @@ -152,7 +161,7 @@ final class L11nManager if (!isset($this->language[$code][$module][$translation])) { try { /** @var ModuleAbstract $class */ - $class = '\Modules\\' . $module . '\\Controller'; + $class = '\Modules\\' . $module . '\\Controller\\' . $this->appName . 'Controller'; $this->loadLanguage($code, $module, $class::getLocalization($code, $theme)); if (!isset($this->language[$code][$module][$translation])) { diff --git a/Module/ModuleFactory.php b/Module/ModuleFactory.php index dc1f54076..b870b2497 100644 --- a/Module/ModuleFactory.php +++ b/Module/ModuleFactory.php @@ -70,7 +70,7 @@ final class ModuleFactory */ public static function getInstance(string $module, ApplicationAbstract $app) : ModuleAbstract { - $class = '\\Modules\\' . $module . '\\Controller'; + $class = '\\Modules\\' . $module . '\\Controller\\' . $app->appName . 'Controller'; if (!isset(self::$loaded[$module])) { if (Autoloader::exists($class) !== false) { diff --git a/Module/ModuleManager.php b/Module/ModuleManager.php index 2d3f8c857..c0e463930 100644 --- a/Module/ModuleManager.php +++ b/Module/ModuleManager.php @@ -651,7 +651,7 @@ final class ModuleManager { try { $this->running[$module] = ModuleFactory::getInstance($module, $this->app); - $this->app->dispatcher->set($this->running[$module], '\Modules\\' . $module . '\\Controller'); + $this->app->dispatcher->set($this->running[$module], '\Modules\\Controller\\' . $module . '\\' . $this->app->appName . 'Controller'); } catch (\Exception $e) { throw $e; } diff --git a/tests/Account/AccountTest.php b/tests/Account/AccountTest.php index 379469489..f6d4eb2c0 100644 --- a/tests/Account/AccountTest.php +++ b/tests/Account/AccountTest.php @@ -32,7 +32,7 @@ class AccountTest extends \PHPUnit\Framework\TestCase protected function setUp() { - $this->l11nManager = new L11nManager(); + $this->l11nManager = new L11nManager('Api'); } public function testAttributes() diff --git a/tests/Dispatcher/DispatcherTest.php b/tests/Dispatcher/DispatcherTest.php index 90b8d0d16..9caed0cbf 100644 --- a/tests/Dispatcher/DispatcherTest.php +++ b/tests/Dispatcher/DispatcherTest.php @@ -32,7 +32,7 @@ class DispatcherTest extends \PHPUnit\Framework\TestCase protected function setUp() { - $this->app = new class extends ApplicationAbstract {}; + $this->app = new class extends ApplicationAbstract { protected $appName = 'Api'; }; $this->app->router = new Router(); $this->app->dispatcher = new Dispatcher($this->app); } diff --git a/tests/Localization/L11nManagerTest.php b/tests/Localization/L11nManagerTest.php index c9eeca15d..ad65cdd09 100644 --- a/tests/Localization/L11nManagerTest.php +++ b/tests/Localization/L11nManagerTest.php @@ -22,13 +22,13 @@ class L11nManagerTest extends \PHPUnit\Framework\TestCase { public function testAttributes() { - $l11nManager = new L11nManager(); + $l11nManager = new L11nManager('Api'); self::assertObjectHasAttribute('language', $l11nManager); } public function testDefault() { - $l11nManager = new L11nManager(); + $l11nManager = new L11nManager('Api'); self::assertFalse($l11nManager->isLanguageLoaded('en')); self::assertEquals([], $l11nManager->getModuleLanguage('en')); self::assertEquals([], $l11nManager->getModuleLanguage('en', 'Admin')); @@ -49,7 +49,7 @@ class L11nManagerTest extends \PHPUnit\Framework\TestCase ] ]; - $localization = new L11nManager(); + $localization = new L11nManager('Api'); $localization->loadLanguage('en', 'doesNotExist', $expected); } @@ -71,7 +71,7 @@ class L11nManagerTest extends \PHPUnit\Framework\TestCase ] ]; - $l11nManager = new L11nManager(); + $l11nManager = new L11nManager('Api'); $l11nManager->loadLanguage('en', 'Admin', $expected['en']); $l11nManager->loadLanguage('en', 'Admin', $expected2['en']); self::assertTrue($l11nManager->isLanguageLoaded('en')); @@ -82,7 +82,7 @@ class L11nManagerTest extends \PHPUnit\Framework\TestCase public function testGetSetFromFile() { - $l11nManager2 = new L11nManager(); + $l11nManager2 = new L11nManager('Api'); $l11nManager2->loadLanguageFromFile('en', 'Test', __DIR__ . '/langTestFile.php'); self::assertEquals('value', $l11nManager2->getHtml('en', 'Test', 'RandomThemeDoesNotMatterAlreadyLoaded', 'key')); diff --git a/tests/Localization/LocalizationTest.php b/tests/Localization/LocalizationTest.php index 4f40d8b18..1076d8b0e 100644 --- a/tests/Localization/LocalizationTest.php +++ b/tests/Localization/LocalizationTest.php @@ -32,7 +32,7 @@ class LocalizationTest extends \PHPUnit\Framework\TestCase protected function setUp() { - $this->l11nManager = new L11nManager(); + $this->l11nManager = new L11nManager('Api'); } public function testAttributes() diff --git a/tests/Module/ModuleFactoryTest.php b/tests/Module/ModuleFactoryTest.php index 9436b5264..714f3bfc2 100644 --- a/tests/Module/ModuleFactoryTest.php +++ b/tests/Module/ModuleFactoryTest.php @@ -25,9 +25,9 @@ class ModuleFactoryTest extends \PHPUnit\Framework\TestCase { $instance = NullModule::class; if (\file_exists(__DIR__ . '/../../../Modules')) { - $instance = \Modules\Admin\Controller::class; + $instance = \Modules\Admin\Controller\ApiController::class; } - self::assertInstanceOf($instance, ModuleFactory::getInstance('Admin', new class extends ApplicationAbstract {})); + self::assertInstanceOf($instance, ModuleFactory::getInstance('Admin', new class extends ApplicationAbstract { protected $appName = 'Api'; })); } } diff --git a/tests/Module/ModuleManagerTest.php b/tests/Module/ModuleManagerTest.php index 2c4d398e7..8de82d093 100644 --- a/tests/Module/ModuleManagerTest.php +++ b/tests/Module/ModuleManagerTest.php @@ -28,7 +28,8 @@ class ModuleManagerTest extends \PHPUnit\Framework\TestCase protected function setUp() { - $this->app = new class extends ApplicationAbstract {}; + $this->app = new class extends ApplicationAbstract { protected $appName = 'Api'; }; + $this->app->appName = 'Api'; $this->app->dbPool = $GLOBALS['dbpool']; $this->app->dispatcher = new Dispatcher($this->app); } @@ -82,6 +83,6 @@ class ModuleManagerTest extends \PHPUnit\Framework\TestCase self::assertNotEmpty($installed); self::assertInstanceOf('\phpOMS\Module\ModuleAbstract', $moduleManager->get('Admin')); - self::assertInstanceOf('\Modules\Admin\Controller', $moduleManager->get('Admin')); + self::assertInstanceOf('\Modules\Admin\Controller\ApiController', $moduleManager->get('Admin')); } } diff --git a/tests/Views/ViewTest.php b/tests/Views/ViewTest.php index 503bed021..192eead85 100644 --- a/tests/Views/ViewTest.php +++ b/tests/Views/ViewTest.php @@ -39,9 +39,10 @@ class ViewTest extends \PHPUnit\Framework\TestCase $this->app = new class extends ApplicationAbstract { + protected $appName = 'Api'; }; - $this->app->l11nManager = new L11nManager(); + $this->app->l11nManager = new L11nManager($this->app->appName); $this->app->dbPool = $this->dbPool; } @@ -72,7 +73,7 @@ class ViewTest extends \PHPUnit\Framework\TestCase ] ]; - $this->app->l11nManager = new L11nManager(); + $this->app->l11nManager = new L11nManager('Api'); $this->app->l11nManager->loadLanguage('en', 'Admin', $expected['en']); self::assertEquals('Test', $view->getText('Test'));