From 486ac07eea07c63db85d3c9746598c9bd858c089 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 2 Jul 2016 14:17:01 +0200 Subject: [PATCH] Optimized language display Language is no longer passed in "10" different ways. --- Localization/L11nManager.php | 31 +++++++++++++++----- Localization/Localization.php | 54 ++++++++++++++++------------------- Localization/lang/en.lang.php | 45 ----------------------------- Message/Http/Request.php | 8 ++++-- Message/Http/Response.php | 7 ++++- Message/RequestAbstract.php | 8 ------ Message/ResponseAbstract.php | 8 ------ Module/InstallerAbstract.php | 32 +++++++++++++++++++++ Module/ModuleManager.php | 43 +++++++++++++++++++++++----- Views/View.php | 4 +-- 10 files changed, 129 insertions(+), 111 deletions(-) delete mode 100644 Localization/lang/en.lang.php diff --git a/Localization/L11nManager.php b/Localization/L11nManager.php index e08eadeb8..969b3d2cf 100644 --- a/Localization/L11nManager.php +++ b/Localization/L11nManager.php @@ -58,9 +58,9 @@ class L11nManager * One module can only be loaded once. Once the module got loaded it's not * possible to load more language files later on. * - * @param string $language Language iso code - * @param string $from Module name - * @param string[][] $files Language files content + * @param string $language Language iso code + * @param string $from Module name + * @param string[][] $translation Language files content * * @return void * @@ -69,17 +69,17 @@ class L11nManager * @since 1.0.0 * @author Dennis Eichhorn */ - public function loadLanguage(string $language, string $from, array $files) + public function loadLanguage(string $language, string $from, array $translation) { - if (!isset($files[$from])) { + if (!isset($translation[$from])) { throw new \Exception('Unexpected language key: ' . $from); } if (!isset($this->language[$language][$from])) { - $this->language[$language][$from] = $files[$from]; + $this->language[$language][$from] = $translation[$from]; } else { /** @noinspection PhpWrongStringConcatenationInspection */ - $this->language[$language][$from] = $files[$from] + $this->language[$language][$from]; + $this->language[$language][$from] = $translation[$from] + $this->language[$language][$from]; } } @@ -104,4 +104,21 @@ class L11nManager return []; } } + + /** + * Get translation. + * + * @param string $code Country code + * @param string $module Module name + * @param string $translation Text + * + * @return array + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getText(string $code, string $module, string $translation) + { + return $this->language[$code][$module][$translation] ?? 'ERROR'; + } } diff --git a/Localization/Localization.php b/Localization/Localization.php index 89002c0e8..a29e89f44 100644 --- a/Localization/Localization.php +++ b/Localization/Localization.php @@ -14,6 +14,7 @@ * @link http://orange-management.com */ namespace phpOMS\Localization; + use phpOMS\Datatypes\Exception\InvalidEnumValue; /** @@ -87,21 +88,24 @@ class Localization private $datetime = 'Y-m-d H:i:s'; /** - * Language array. + * Localization manager. * - * @var string[] + * @var L11nManager * @since 1.0.0 */ - public $lang = []; + public $l11nManager = null; /** * Constructor. * + * @param L11nManager $l11nManager Localization manager + * * @since 1.0.0 * @author Dennis Eichhorn */ - public function __construct() + public function __construct(L11nManager $l11nManager) { + $this->l11nManager = $l11nManager; } /** @@ -146,7 +150,7 @@ class Localization /** * @param string $timezone * - * @todo: maybe make parameter int + * @todo : maybe make parameter int * * @since 1.0.0 * @author Dennis Eichhorn @@ -188,30 +192,6 @@ class Localization $this->language = $language; } - /** - * @return array - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public function getLang() : array - { - return $this->lang; - } - - /** - * @param array $lang - * - * @return void - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public function setLang(array $lang) - { - $this->lang = $lang; - } - /** * @return string * @@ -311,4 +291,20 @@ class Localization { $this->thousands = $thousands; } + + /** + * Get translation. + * + * @param string $module Module name + * @param string $translation Text + * + * @return array + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getText(string $module, string $translation) + { + return $this->l11nManager->getText($this->language, $module, $translation); + } } diff --git a/Localization/lang/en.lang.php b/Localization/lang/en.lang.php deleted file mode 100644 index 5c08ad623..000000000 --- a/Localization/lang/en.lang.php +++ /dev/null @@ -1,45 +0,0 @@ - - * @author Dennis Eichhorn - * @copyright 2013 Dennis Eichhorn - * @license OMS License 1.0 - * @version 1.0.0 - * @link http://orange-management.com - */ -$CORELANG[0] = [ - 'Add' => 'Add', - 'Cancel' => 'Cancel', - 'Close' => 'Close', - 'Create' => 'Create', - 'Delete' => 'Delete', - 'Edit' => 'Edit', - 'Email' => 'Email', - 'Empty' => 'Empty', - 'Filter' => 'Filter', - 'Find' => 'Find', - 'ID' => 'ID', - 'More' => 'More', - 'Navigation' => 'Navigation', - 'Password' => 'Password', - 'Reset' => 'Reset', - 'Save' => 'Save', - 'Search' => 'Search', - 'Select' => 'Select', - 'Send' => 'Send', - 'Submit' => 'Submit', - 'e:empty' => "This field mustn't be empty.", - 'Monday' => 'Monday', - 'Tuesday' => 'Tuesday', - 'Wednesday' => 'Wednesday', - 'Thursday' => 'Thursday', - 'Friday' => 'Friday', - 'Saturday' => 'Saturday', - 'Sunday' => 'Sunday', -]; diff --git a/Message/Http/Request.php b/Message/Http/Request.php index 7b2f6023d..48f2130a9 100644 --- a/Message/Http/Request.php +++ b/Message/Http/Request.php @@ -15,6 +15,7 @@ */ namespace phpOMS\Message\Http; +use phpOMS\Localization\L11nManager; use phpOMS\Localization\Localization; use phpOMS\Message\RequestAbstract; use phpOMS\Uri\Http; @@ -87,15 +88,16 @@ class Request extends RequestAbstract /** * Constructor. * - * @param UriInterface $uri Uri + * @param L11nManager $l11nManager Localization manager + * @param UriInterface $uri Uri * * @since 1.0.0 * @author Dennis Eichhorn */ - public function __construct(UriInterface $uri = null) + public function __construct(L11nManager $l11nManager, UriInterface $uri = null) { + $this->l11n = new Localization($l11nManager); $this->uri = $uri; - $this->l11n = new Localization(); } /** diff --git a/Message/Http/Response.php b/Message/Http/Response.php index fa12828d7..62bcd66dd 100644 --- a/Message/Http/Response.php +++ b/Message/Http/Response.php @@ -15,6 +15,8 @@ */ namespace phpOMS\Message\Http; +use phpOMS\Localization\L11nManager; +use phpOMS\Localization\Localization; use phpOMS\System\MimeType; use phpOMS\Contract\RenderableInterface; use phpOMS\Message\ResponseAbstract; @@ -36,12 +38,15 @@ class Response extends ResponseAbstract implements RenderableInterface /** * Constructor. * + * @param L11nManager $l11nManager Localization manager + * * @since 1.0.0 * @author Dennis Eichhorn */ - public function __construct() + public function __construct(L11nManager $l11nManager) { $this->header = new Header(); + $this->l11n = new Localization($l11nManager); } /** diff --git a/Message/RequestAbstract.php b/Message/RequestAbstract.php index c80d2e817..917a3f2b3 100644 --- a/Message/RequestAbstract.php +++ b/Message/RequestAbstract.php @@ -277,14 +277,6 @@ abstract class RequestAbstract implements MessageInterface } } - /** - * {@inheritdoc} - */ - public function setLocalization(Localization $l11n) - { - return $this->l11n = $l11n; - } - /** * {@inheritdoc} */ diff --git a/Message/ResponseAbstract.php b/Message/ResponseAbstract.php index f3ac6fa13..d9888eb20 100644 --- a/Message/ResponseAbstract.php +++ b/Message/ResponseAbstract.php @@ -80,14 +80,6 @@ abstract class ResponseAbstract implements MessageInterface return $this->l11n; } - /** - * {@inheritdoc} - */ - public function setL11n(Localization $l11n) - { - return $this->l11n = $l11n; - } - /** * Get response by ID. * diff --git a/Module/InstallerAbstract.php b/Module/InstallerAbstract.php index 3a1022a2e..2b187a461 100644 --- a/Module/InstallerAbstract.php +++ b/Module/InstallerAbstract.php @@ -99,6 +99,38 @@ class InstallerAbstract public static function install(Pool $dbPool, InfoManager $info) { self::registerInDatabase($dbPool, $info); + self::initRoutes($info); + } + + /** + * Re-init module. + * + * @param InfoManager $info Module info + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function reInit(InfoManager $info) + { + self::initRoutes($info); + } + + /** + * Init routes. + * + * @param InfoManager $info Module info + * + * @return void + * + * @throws PermissionException + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + private static function initRoutes(InfoManager $info) + { self::installRoutes(ROOT_PATH . '/Web/Routes.php', ROOT_PATH . '/Modules/' . $info->getDirectory() . '/Admin/Routes/http.php'); self::installRoutes(ROOT_PATH . '/Socket/Routes.php', ROOT_PATH . '/Modules/' . $info->getDirectory() . '/Admin/Routes/socket.php'); self::installRoutes(ROOT_PATH . '/Console/Routes.php', ROOT_PATH . '/Modules/' . $info->getDirectory() . '/Admin/Routes/console.php'); diff --git a/Module/ModuleManager.php b/Module/ModuleManager.php index 8b289dcf4..ad6a70c2a 100644 --- a/Module/ModuleManager.php +++ b/Module/ModuleManager.php @@ -346,6 +346,37 @@ class ModuleManager } } + /** + * Re-init module. + * + * @param string $module Module name + * + * @return bool + * + * @throws + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function reInit(string $module) : bool + { + if(file_exists($path = __DIR__ . '/../Web/Routes.php')) { + unlink($path); + } + + file_put_contents($path, 'loadInfo($module); + /** @var $class InstallerAbstract */ + $class = '\\Modules\\' . $info->getDirectory() . '\\Admin\\Installer'; + + if (!Autoloader::exists($class)) { + throw new \Exception('Module installer does not exist'); + } + + $class::reInit($info); + } + /** * Install module. * @@ -569,13 +600,11 @@ class ModuleManager */ public function initModule($module) { - if (is_array($module)) { - $this->initModuleArray($module); - } elseif (is_string($module) && realpath(self::MODULE_PATH . '/' . $module . '/Controller.php') !== false) { - $this->initModuleController($module); - } else { - throw new \InvalidArgumentException('Invalid Module'); + if(!is_array($module)) { + $module = [$module]; } + + $this->initModuleArray($module); } /** @@ -592,7 +621,7 @@ class ModuleManager { foreach ($modules as $module) { try { - $this->initModule($module); + $this->initModuleController($module); } catch (\InvalidArgumentException $e) { $this->app->logger->warning(FileLogger::MSG_FULL, [ 'message' => 'Trying to initialize ' . $module . ' without controller.', diff --git a/Views/View.php b/Views/View.php index 9f67e0cca..58607de75 100644 --- a/Views/View.php +++ b/Views/View.php @@ -107,8 +107,7 @@ class View implements \Serializable $this->app = $app; $this->request = $request; $this->response = $response; - - $this->l11n = new Localization(); + $this->l11n = $response->getL11n(); } /** @@ -259,7 +258,6 @@ class View implements \Serializable */ public function render() : string { - $this->l11n->setLang($this->app->l11nManager->getLanguage($this->response->getL11n()->getLanguage())); $path = realpath($oldPath = __DIR__ . '/../..' . $this->template . '.tpl.php'); if ($path === false || Validator::startsWith($path, ROOT_PATH) === false) {