From 8a70226bd287a3f13b27932e2ec88d16c51c7e39 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Tue, 24 Nov 2020 04:14:09 +0100 Subject: [PATCH] Continued: - moved language instance methods from monolithic and wrong registry-usage to FrameworkBootstrap - added type-hints for primitive variables (also maybe in last 3 commits) MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../bootstrap/class_FrameworkBootstrap.php | 25 +++++++++++++++++++ .../classes/class_BaseFrameworkSystem.php | 22 ---------------- .../classes/commands/class_BaseCommand.php | 3 ++- .../html/class_HtmlConfirmCommand.php | 3 ++- .../commands/html/class_HtmlHomeCommand.php | 3 ++- .../html/class_HtmlLoginAreaCommand.php | 2 +- .../commands/html/class_HtmlLoginCommand.php | 3 ++- .../html/class_HtmlLoginFailedCommand.php | 3 ++- .../html/class_HtmlLogoutDoneCommand.php | 3 ++- .../html/class_HtmlRegisterCommand.php | 3 ++- .../commands/html/class_HtmlStatusCommand.php | 3 ++- .../html/blocks/class_HtmlBlockHelper.php | 6 ++--- .../html/links/class_HtmlLinkHelper.php | 8 +++--- .../classes/language/class_LanguageSystem.php | 11 +++----- .../classes/response/class_BaseResponse.php | 5 +--- .../utils/number/class_NumberUtils.php | 2 -- 16 files changed, 54 insertions(+), 51 deletions(-) diff --git a/framework/bootstrap/class_FrameworkBootstrap.php b/framework/bootstrap/class_FrameworkBootstrap.php index b102c976..ad1bf8a2 100644 --- a/framework/bootstrap/class_FrameworkBootstrap.php +++ b/framework/bootstrap/class_FrameworkBootstrap.php @@ -11,6 +11,7 @@ use Org\Mxchange\CoreFramework\EntryPoint\ApplicationEntryPoint; use Org\Mxchange\CoreFramework\Factory\ObjectFactory; use Org\Mxchange\CoreFramework\Generic\NullPointerException; use Org\Mxchange\CoreFramework\Helper\Application\ApplicationHelper; +use Org\Mxchange\CoreFramework\Localization\ManageableLanguage; use Org\Mxchange\CoreFramework\Loader\ClassLoader; use Org\Mxchange\CoreFramework\Manager\ManageableApplication; use Org\Mxchange\CoreFramework\Middleware\Debug\DebugMiddleware; @@ -74,6 +75,11 @@ final class FrameworkBootstrap { */ private static $databaseInstance = NULL; + /** + * Language system instance + */ + private static $languageInstance = NULL; + /* * Includes applications may have. They will be tried in the given order, * some will become soon deprecated. @@ -715,4 +721,23 @@ final class FrameworkBootstrap { return self::$databaseInstance; } + /** + * Private getter for language instance + * + * @return $languageInstance An instance of a ManageableLanguage class + */ + public static function getLanguageInstance () { + return self::$languageInstance; + } + + /** + * Setter for language instance + * + * @param $languageInstance An instance of a ManageableLanguage class + * @return void + */ + public static function setLanguageInstance (ManageableLanguage $languageInstance) { + self::$languageInstance = $languageInstance; + } + } diff --git a/framework/main/classes/class_BaseFrameworkSystem.php b/framework/main/classes/class_BaseFrameworkSystem.php index 0c7e737e..00dd1f3b 100644 --- a/framework/main/classes/class_BaseFrameworkSystem.php +++ b/framework/main/classes/class_BaseFrameworkSystem.php @@ -17,7 +17,6 @@ use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException; use Org\Mxchange\CoreFramework\Handler\Handleable; use Org\Mxchange\CoreFramework\Helper\Helper; use Org\Mxchange\CoreFramework\Loader\ClassLoader; -use Org\Mxchange\CoreFramework\Localization\ManageableLanguage; use Org\Mxchange\CoreFramework\Manager\ManageableApplication; use Org\Mxchange\CoreFramework\Middleware\Debug\DebugMiddleware; use Org\Mxchange\CoreFramework\Registry\GenericRegistry; @@ -510,27 +509,6 @@ abstract class BaseFrameworkSystem extends stdClass implements FrameworkInterfac return $webOutputInstance; } - /** - * Private getter for language instance - * - * @return $langInstance An instance to the language sub-system - */ - protected final function getLanguageInstance () { - $langInstance = GenericRegistry::getRegistry()->getInstance('language'); - return $langInstance; - } - - /** - * Setter for language instance - * - * @param $langInstance An instance to the language sub-system - * @return void - * @see LanguageSystem - */ - public final function setLanguageInstance (ManageableLanguage $langInstance) { - GenericRegistry::getRegistry()->addInstance('language', $langInstance); - } - /** * Protected setter for user instance * diff --git a/framework/main/classes/commands/class_BaseCommand.php b/framework/main/classes/commands/class_BaseCommand.php index 79482c98..6d6a9ebc 100644 --- a/framework/main/classes/commands/class_BaseCommand.php +++ b/framework/main/classes/commands/class_BaseCommand.php @@ -3,6 +3,7 @@ namespace Org\Mxchange\CoreFramework\Command; // Import framework stuff +use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap; use Org\Mxchange\CoreFramework\Factory\ObjectFactory; use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem; use Org\Mxchange\CoreFramework\Registry\GenericRegistry; @@ -154,7 +155,7 @@ abstract class BaseCommand extends BaseFrameworkSystem { $this->getTemplateInstance()->loadCodeTemplate($masterTemplate); // Set title - $this->getTemplateInstance()->assignVariable('title', $this->getLanguageInstance()->getMessage('page_' . $applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName() . '_title')); + $this->getTemplateInstance()->assignVariable('title', FrameworkBootstrap::getLanguageInstance()->getMessage('page_' . $applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName() . '_title')); // Construct the menu in every command. We could do this in BaseCommand class. But this means // *every* command has a navigation system and that is want we don't want. diff --git a/framework/main/classes/commands/html/class_HtmlConfirmCommand.php b/framework/main/classes/commands/html/class_HtmlConfirmCommand.php index 3401e1eb..f22cd9cf 100644 --- a/framework/main/classes/commands/html/class_HtmlConfirmCommand.php +++ b/framework/main/classes/commands/html/class_HtmlConfirmCommand.php @@ -3,6 +3,7 @@ namespace Org\Mxchange\CoreFramework\Command\Guest; // Import framework stuff +use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap; use Org\Mxchange\CoreFramework\Command\BaseCommand; use Org\Mxchange\CoreFramework\Command\Commandable; use Org\Mxchange\CoreFramework\Controller\Controller; @@ -108,7 +109,7 @@ class HtmlConfirmCommand extends BaseCommand implements Commandable { $this->getTemplateInstance()->loadCodeTemplate($masterTemplate); // Set title - $this->getTemplateInstance()->assignVariable('title', $this->getLanguageInstance()->getMessage('page_confirm_link_title')); + $this->getTemplateInstance()->assignVariable('title', FrameworkBootstrap::getLanguageInstance()->getMessage('page_confirm_link_title')); // Get user instance try { diff --git a/framework/main/classes/commands/html/class_HtmlHomeCommand.php b/framework/main/classes/commands/html/class_HtmlHomeCommand.php index 91a69c31..5213906d 100644 --- a/framework/main/classes/commands/html/class_HtmlHomeCommand.php +++ b/framework/main/classes/commands/html/class_HtmlHomeCommand.php @@ -3,6 +3,7 @@ namespace Org\Mxchange\CoreFramework\Command\Guest; // Import framework stuff +use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap; use Org\Mxchange\CoreFramework\Command\BaseCommand; use Org\Mxchange\CoreFramework\Command\Commandable; use Org\Mxchange\CoreFramework\Controller\Controller; @@ -104,7 +105,7 @@ class HtmlHomeCommand extends BaseCommand implements Commandable { $this->getTemplateInstance()->loadCodeTemplate($masterTemplate); // Set title - $this->getTemplateInstance()->assignVariable('title', $this->getLanguageInstance()->getMessage('page_home_title')); + $this->getTemplateInstance()->assignVariable('title', FrameworkBootstrap::getLanguageInstance()->getMessage('page_home_title')); // Construct the menu in every command. We could do this in BaseCommand class. But this means // *every* command has a navigation system and that is want we don't want. diff --git a/framework/main/classes/commands/html/class_HtmlLoginAreaCommand.php b/framework/main/classes/commands/html/class_HtmlLoginAreaCommand.php index 266ba7c4..dd1aa58a 100644 --- a/framework/main/classes/commands/html/class_HtmlLoginAreaCommand.php +++ b/framework/main/classes/commands/html/class_HtmlLoginAreaCommand.php @@ -143,7 +143,7 @@ class HtmlLoginAreaCommand extends BaseCommand implements Commandable { $this->getTemplateInstance()->loadCodeTemplate($masterTemplate); // Set title - $this->getTemplateInstance()->assignVariable('title', $this->getLanguageInstance()->getMessage($this->actionName . '_title')); + $this->getTemplateInstance()->assignVariable('title', FrameworkBootstrap::getLanguageInstance()->getMessage($this->actionName . '_title')); // Construct the menu in every command. We could do this in BaseCommand class. But this means // *every* command has a navigation system and that is want we don't want. diff --git a/framework/main/classes/commands/html/class_HtmlLoginCommand.php b/framework/main/classes/commands/html/class_HtmlLoginCommand.php index cef2596d..77f9be23 100644 --- a/framework/main/classes/commands/html/class_HtmlLoginCommand.php +++ b/framework/main/classes/commands/html/class_HtmlLoginCommand.php @@ -3,6 +3,7 @@ namespace Org\Mxchange\CoreFramework\Command\Login; // Import framework stuff +use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap; use Org\Mxchange\CoreFramework\Command\BaseCommand; use Org\Mxchange\CoreFramework\Command\Commandable; use Org\Mxchange\CoreFramework\Controller\Controller; @@ -109,7 +110,7 @@ class HtmlLoginCommand extends BaseCommand implements Commandable { $this->getTemplateInstance()->loadCodeTemplate($masterTemplate); // Set title - $this->getTemplateInstance()->assignVariable('title', $this->getLanguageInstance()->getMessage('page_login_title')); + $this->getTemplateInstance()->assignVariable('title', FrameworkBootstrap::getLanguageInstance()->getMessage('page_login_title')); // Construct the menu in every command. We could do this in BaseCommand class. But this means // *every* command has a navigation system and that is want we don't want. diff --git a/framework/main/classes/commands/html/class_HtmlLoginFailedCommand.php b/framework/main/classes/commands/html/class_HtmlLoginFailedCommand.php index 24356c36..6507520c 100644 --- a/framework/main/classes/commands/html/class_HtmlLoginFailedCommand.php +++ b/framework/main/classes/commands/html/class_HtmlLoginFailedCommand.php @@ -3,6 +3,7 @@ namespace Org\Mxchange\CoreFramework\Command\Failed; // Import framework stuff +use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap; use Org\Mxchange\CoreFramework\Command\BaseCommand; use Org\Mxchange\CoreFramework\Command\Commandable; use Org\Mxchange\CoreFramework\Controller\Controller; @@ -103,7 +104,7 @@ class HtmlLoginFailedCommand extends BaseCommand implements Commandable { $this->getTemplateInstance()->loadCodeTemplate($masterTemplate); // Set title - $this->getTemplateInstance()->assignVariable('title', $this->getLanguageInstance()->getMessage('login_failed_title')); + $this->getTemplateInstance()->assignVariable('title', FrameworkBootstrap::getLanguageInstance()->getMessage('login_failed_title')); // Assign base URL $this->getTemplateInstance()->assignConfigVariable('base_url'); diff --git a/framework/main/classes/commands/html/class_HtmlLogoutDoneCommand.php b/framework/main/classes/commands/html/class_HtmlLogoutDoneCommand.php index a7134cb6..6ef72aca 100644 --- a/framework/main/classes/commands/html/class_HtmlLogoutDoneCommand.php +++ b/framework/main/classes/commands/html/class_HtmlLogoutDoneCommand.php @@ -3,6 +3,7 @@ namespace Org\Mxchange\CoreFramework\Command\Logout; // Import framework stuff +use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap; use Org\Mxchange\CoreFramework\Command\BaseCommand; use Org\Mxchange\CoreFramework\Command\Commandable; use Org\Mxchange\CoreFramework\Controller\Controller; @@ -103,7 +104,7 @@ class HtmlLogoutDoneCommand extends BaseCommand implements Commandable { $this->getTemplateInstance()->loadCodeTemplate($masterTemplate); // Set title - $this->getTemplateInstance()->assignVariable('title', $this->getLanguageInstance()->getMessage('logout_done_title')); + $this->getTemplateInstance()->assignVariable('title', FrameworkBootstrap::getLanguageInstance()->getMessage('logout_done_title')); // Assign base URL $this->getTemplateInstance()->assignConfigVariable('base_url'); diff --git a/framework/main/classes/commands/html/class_HtmlRegisterCommand.php b/framework/main/classes/commands/html/class_HtmlRegisterCommand.php index 3a223a7d..1c65919a 100644 --- a/framework/main/classes/commands/html/class_HtmlRegisterCommand.php +++ b/framework/main/classes/commands/html/class_HtmlRegisterCommand.php @@ -3,6 +3,7 @@ namespace Org\Mxchange\CoreFramework\Command\Register; // Import framework stuff +use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap; use Org\Mxchange\CoreFramework\Command\BaseCommand; use Org\Mxchange\CoreFramework\Command\Commandable; use Org\Mxchange\CoreFramework\Controller\Controller; @@ -110,7 +111,7 @@ class HtmlRegisterCommand extends BaseCommand implements Commandable { $this->getTemplateInstance()->loadCodeTemplate($masterTemplate); // Set title - $this->getTemplateInstance()->assignVariable('title', $this->getLanguageInstance()->getMessage('page_register_title')); + $this->getTemplateInstance()->assignVariable('title', FrameworkBootstrap::getLanguageInstance()->getMessage('page_register_title')); // Construct the menu in every command. We could do this in BaseCommand class. But this means // *every* command has a navigation system and that is want we don't want. diff --git a/framework/main/classes/commands/html/class_HtmlStatusCommand.php b/framework/main/classes/commands/html/class_HtmlStatusCommand.php index e829e11a..485ca585 100644 --- a/framework/main/classes/commands/html/class_HtmlStatusCommand.php +++ b/framework/main/classes/commands/html/class_HtmlStatusCommand.php @@ -3,6 +3,7 @@ namespace Org\Mxchange\CoreFramework\Command\Status; // Import framework stuff +use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap; use Org\Mxchange\CoreFramework\Command\BaseCommand; use Org\Mxchange\CoreFramework\Command\Commandable; use Org\Mxchange\CoreFramework\Controller\Controller; @@ -103,7 +104,7 @@ class HtmlStatusCommand extends BaseCommand implements Commandable { $this->getTemplateInstance()->loadCodeTemplate($masterTemplate); // Set title - $this->getTemplateInstance()->assignVariable('title', $this->getLanguageInstance()->getMessage('page_status_title')); + $this->getTemplateInstance()->assignVariable('title', FrameworkBootstrap::getLanguageInstance()->getMessage('page_status_title')); // Construct the menu in every command. We could do this in BaseCommand class. But this means // *every* command has a navigation system and that is want we don't want. diff --git a/framework/main/classes/helper/html/blocks/class_HtmlBlockHelper.php b/framework/main/classes/helper/html/blocks/class_HtmlBlockHelper.php index 7a6a697c..346d56da 100644 --- a/framework/main/classes/helper/html/blocks/class_HtmlBlockHelper.php +++ b/framework/main/classes/helper/html/blocks/class_HtmlBlockHelper.php @@ -104,7 +104,7 @@ class HtmlBlockHelper extends BaseHtmlHelper implements HelpableTemplate { */ public function assignMessageField (string $templateVariable, string $messageId) { // Get message - $message = $this->getLanguageInstance()->getMessage($messageId); + $message = FrameworkBootstrap::getLanguageInstance()->getMessage($messageId); // And assign it $this->getTemplateInstance()->assignVariable($templateVariable, $message); @@ -129,10 +129,10 @@ class HtmlBlockHelper extends BaseHtmlHelper implements HelpableTemplate { */ protected function doFilterUserStatusTranslator (string $userStatus) { // Generate message id - $messageId = 'user_status_' . strtolower($userStatus); + $messageId = sprintf('user_status_%s', strtolower($userStatus)); // Get that message - $translated = $this->getLanguageInstance()->getMessage($messageId); + $translated = FrameworkBootstrap::getLanguageInstance()->getMessage($messageId); // Return it return $translated; diff --git a/framework/main/classes/helper/html/links/class_HtmlLinkHelper.php b/framework/main/classes/helper/html/links/class_HtmlLinkHelper.php index 13552a20..dcef00c9 100644 --- a/framework/main/classes/helper/html/links/class_HtmlLinkHelper.php +++ b/framework/main/classes/helper/html/links/class_HtmlLinkHelper.php @@ -322,10 +322,10 @@ class HtmlLinkHelper extends BaseHtmlHelper implements HelpableTemplate { */ public function addActionLinkById ($linkAction, $languageId) { // Resolve the language string - $languageResolvedText = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_text'); + $languageResolvedText = FrameworkBootstrap::getLanguageInstance()->getMessage('link_' . $languageId . '_text'); // Resolve the language string - $languageResolvedTitle = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_title'); + $languageResolvedTitle = FrameworkBootstrap::getLanguageInstance()->getMessage('link_' . $languageId . '_title'); // Add the action link $this->addActionLink($linkAction, $languageResolvedText, $languageResolvedTitle); @@ -340,10 +340,10 @@ class HtmlLinkHelper extends BaseHtmlHelper implements HelpableTemplate { */ public function addLinkWithTextById ($languageId) { // Resolve the language string - $languageResolvedText = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_text'); + $languageResolvedText = FrameworkBootstrap::getLanguageInstance()->getMessage('link_' . $languageId . '_text'); // Resolve the language string - $languageResolvedTitle = $this->getLanguageInstance()->getMessage('link_' . $languageId . '_title'); + $languageResolvedTitle = FrameworkBootstrap::getLanguageInstance()->getMessage('link_' . $languageId . '_title'); // Now add the link $linkContent = $this->renderLinkContentWithTextExtraContent($languageResolvedText, $languageResolvedTitle); diff --git a/framework/main/classes/language/class_LanguageSystem.php b/framework/main/classes/language/class_LanguageSystem.php index 4a1c0fd0..3080899b 100644 --- a/framework/main/classes/language/class_LanguageSystem.php +++ b/framework/main/classes/language/class_LanguageSystem.php @@ -79,7 +79,7 @@ class LanguageSystem extends BaseFrameworkSystem implements ManageableLanguage, * @throws LanguagePathReadProtectedException If $languageBasePath is * read-protected */ - public static final function createLanguageSystem ($languageBasePath = '') { + public static final function createLanguageSystem (string $languageBasePath = '') { // Get a new instance $langInstance = new LanguageSystem(); @@ -142,7 +142,7 @@ class LanguageSystem extends BaseFrameworkSystem implements ManageableLanguage, * @param $languageBasePath The relative base path for all language files * @return void */ - protected final function setLanguageBasePath ($languageBasePath) { + protected final function setLanguageBasePath (string $languageBasePath) { // And set it $this->languageBasePath = (string) $languageBasePath; } @@ -153,10 +153,7 @@ class LanguageSystem extends BaseFrameworkSystem implements ManageableLanguage, * @param $langCode The language code for the current application * @return void */ - protected final function setLanguageCode ($langCode) { - // Cast it - $langCode = (string) $langCode; - + protected final function setLanguageCode (string $langCode) { // And set it (only 2 chars) $this->langCode = substr($langCode, 0, 2); } @@ -211,7 +208,7 @@ class LanguageSystem extends BaseFrameworkSystem implements ManageableLanguage, * @param $messageId The message id we shall find in the cache variable * @return $messageText The plain message text */ - public function getMessage ($messageId) { + public function getMessage (string $messageId) { // Default is missing message text $messageText = sprintf('!%s!', $messageId diff --git a/framework/main/classes/response/class_BaseResponse.php b/framework/main/classes/response/class_BaseResponse.php index a52a53bf..3cc1807d 100644 --- a/framework/main/classes/response/class_BaseResponse.php +++ b/framework/main/classes/response/class_BaseResponse.php @@ -147,11 +147,8 @@ abstract class BaseResponse extends BaseFrameworkSystem { * @return void */ public final function addFatalMessage ($messageId) { - // Get application instance - $applicationInstance = GenericRegistry::getRegistry()->getInstance('application'); - // Adds the resolved message id to the fatal message list - $this->addFatalMessagePlain($applicationInstance->getLanguageInstance()->getMessage($messageId)); + $this->addFatalMessagePlain(FrameworkBootstrap::getLanguageInstance()->getMessage($messageId)); } /** diff --git a/framework/main/classes/utils/number/class_NumberUtils.php b/framework/main/classes/utils/number/class_NumberUtils.php index 2318858e..29afa81f 100644 --- a/framework/main/classes/utils/number/class_NumberUtils.php +++ b/framework/main/classes/utils/number/class_NumberUtils.php @@ -45,7 +45,6 @@ final class NumberUtils extends BaseFrameworkSystem { * * @param $value The raw value from e.g. database * @return $localized Localized value - * @todo Won't work as getLanguageInstance() is still in monolithic BaseFrameworkSystem */ public static function doFilterFormatNumber ($value) { // Generate it from config and localize dependencies @@ -70,7 +69,6 @@ final class NumberUtils extends BaseFrameworkSystem { * * @param $timestamp Timestamp to prepare (filter) for display * @return $readable A readable timestamp - * @todo Won't work as getLanguageInstance() is still in monolithic BaseFrameworkSystem */ public static function doFilterFormatTimestamp ($timestamp) { // Default value to return -- 2.39.2