Split controllers per application

This commit is contained in:
Dennis Eichhorn 2018-09-19 21:33:10 +02:00
parent 430be41982
commit bfc457115c
11 changed files with 30 additions and 25 deletions

View File

@ -182,13 +182,7 @@ final class Dispatcher
private function getController(string $controller) : object private function getController(string $controller) : object
{ {
if (!isset($this->controllers[$controller])) { if (!isset($this->controllers[$controller])) {
// If module controller use module manager for initialization $this->controllers[$controller] = new $controller($this->app);
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);
}
} }
return $this->controllers[$controller]; return $this->controllers[$controller];

View File

@ -36,13 +36,22 @@ final class L11nManager
*/ */
private $language = []; private $language = [];
/**
* App Name.
*
* @var string
* @since 1.0.0
*/
private $appName = '';
/** /**
* Construct. * Construct.
* *
* @since 1.0.0 * @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])) { if (!isset($this->language[$code][$module][$translation])) {
try { try {
/** @var ModuleAbstract $class */ /** @var ModuleAbstract $class */
$class = '\Modules\\' . $module . '\\Controller'; $class = '\Modules\\' . $module . '\\Controller\\' . $this->appName . 'Controller';
$this->loadLanguage($code, $module, $class::getLocalization($code, $theme)); $this->loadLanguage($code, $module, $class::getLocalization($code, $theme));
if (!isset($this->language[$code][$module][$translation])) { if (!isset($this->language[$code][$module][$translation])) {

View File

@ -70,7 +70,7 @@ final class ModuleFactory
*/ */
public static function getInstance(string $module, ApplicationAbstract $app) : ModuleAbstract 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 (!isset(self::$loaded[$module])) {
if (Autoloader::exists($class) !== false) { if (Autoloader::exists($class) !== false) {

View File

@ -651,7 +651,7 @@ final class ModuleManager
{ {
try { try {
$this->running[$module] = ModuleFactory::getInstance($module, $this->app); $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) { } catch (\Exception $e) {
throw $e; throw $e;
} }

View File

@ -32,7 +32,7 @@ class AccountTest extends \PHPUnit\Framework\TestCase
protected function setUp() protected function setUp()
{ {
$this->l11nManager = new L11nManager(); $this->l11nManager = new L11nManager('Api');
} }
public function testAttributes() public function testAttributes()

View File

@ -32,7 +32,7 @@ class DispatcherTest extends \PHPUnit\Framework\TestCase
protected function setUp() 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->router = new Router();
$this->app->dispatcher = new Dispatcher($this->app); $this->app->dispatcher = new Dispatcher($this->app);
} }

View File

@ -22,13 +22,13 @@ class L11nManagerTest extends \PHPUnit\Framework\TestCase
{ {
public function testAttributes() public function testAttributes()
{ {
$l11nManager = new L11nManager(); $l11nManager = new L11nManager('Api');
self::assertObjectHasAttribute('language', $l11nManager); self::assertObjectHasAttribute('language', $l11nManager);
} }
public function testDefault() public function testDefault()
{ {
$l11nManager = new L11nManager(); $l11nManager = new L11nManager('Api');
self::assertFalse($l11nManager->isLanguageLoaded('en')); self::assertFalse($l11nManager->isLanguageLoaded('en'));
self::assertEquals([], $l11nManager->getModuleLanguage('en')); self::assertEquals([], $l11nManager->getModuleLanguage('en'));
self::assertEquals([], $l11nManager->getModuleLanguage('en', 'Admin')); 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); $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', $expected['en']);
$l11nManager->loadLanguage('en', 'Admin', $expected2['en']); $l11nManager->loadLanguage('en', 'Admin', $expected2['en']);
self::assertTrue($l11nManager->isLanguageLoaded('en')); self::assertTrue($l11nManager->isLanguageLoaded('en'));
@ -82,7 +82,7 @@ class L11nManagerTest extends \PHPUnit\Framework\TestCase
public function testGetSetFromFile() public function testGetSetFromFile()
{ {
$l11nManager2 = new L11nManager(); $l11nManager2 = new L11nManager('Api');
$l11nManager2->loadLanguageFromFile('en', 'Test', __DIR__ . '/langTestFile.php'); $l11nManager2->loadLanguageFromFile('en', 'Test', __DIR__ . '/langTestFile.php');
self::assertEquals('value', $l11nManager2->getHtml('en', 'Test', 'RandomThemeDoesNotMatterAlreadyLoaded', 'key')); self::assertEquals('value', $l11nManager2->getHtml('en', 'Test', 'RandomThemeDoesNotMatterAlreadyLoaded', 'key'));

View File

@ -32,7 +32,7 @@ class LocalizationTest extends \PHPUnit\Framework\TestCase
protected function setUp() protected function setUp()
{ {
$this->l11nManager = new L11nManager(); $this->l11nManager = new L11nManager('Api');
} }
public function testAttributes() public function testAttributes()

View File

@ -25,9 +25,9 @@ class ModuleFactoryTest extends \PHPUnit\Framework\TestCase
{ {
$instance = NullModule::class; $instance = NullModule::class;
if (\file_exists(__DIR__ . '/../../../Modules')) { 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'; }));
} }
} }

View File

@ -28,7 +28,8 @@ class ModuleManagerTest extends \PHPUnit\Framework\TestCase
protected function setUp() 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->dbPool = $GLOBALS['dbpool'];
$this->app->dispatcher = new Dispatcher($this->app); $this->app->dispatcher = new Dispatcher($this->app);
} }
@ -82,6 +83,6 @@ class ModuleManagerTest extends \PHPUnit\Framework\TestCase
self::assertNotEmpty($installed); self::assertNotEmpty($installed);
self::assertInstanceOf('\phpOMS\Module\ModuleAbstract', $moduleManager->get('Admin')); 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'));
} }
} }

View File

@ -39,9 +39,10 @@ class ViewTest extends \PHPUnit\Framework\TestCase
$this->app = new class extends ApplicationAbstract $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; $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']); $this->app->l11nManager->loadLanguage('en', 'Admin', $expected['en']);
self::assertEquals('<a href="test">Test</a>', $view->getText('Test')); self::assertEquals('<a href="test">Test</a>', $view->getText('Test'));