From 00225d81115763e48aa6cfb918d6ec9e9ace39e6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Thu, 16 Feb 2023 18:43:13 +0100 Subject: [PATCH] Continued: - updated ApplicationHelper class - updated getting configuration instance - emptied deprecated files to have non-cloned installations cleaned up --- .../shipsimu/class_ApplicationHelper.php | 196 +++++++----------- application/shipsimu/config.php | 5 +- application/shipsimu/data.php | 51 +---- application/shipsimu/exceptions.php | 147 +++++++++---- application/shipsimu/init.php | 47 +---- application/shipsimu/loader.php | 29 +-- application/shipsimu/starter.php | 53 +---- 7 files changed, 195 insertions(+), 333 deletions(-) diff --git a/application/shipsimu/class_ApplicationHelper.php b/application/shipsimu/class_ApplicationHelper.php index 204800f..84d5198 100644 --- a/application/shipsimu/class_ApplicationHelper.php +++ b/application/shipsimu/class_ApplicationHelper.php @@ -1,4 +1,16 @@ * @version 0.0 - * @copyright Copyright (c) 2007 - 2008 Roland Haeder, 2009 - 2012 Ship-Simu Developer Team + * @copyright Copyright (c) 2007 - 2008 Roland Haeder, 2009 - 2022 Ship-Simu Developer Team * @license GNU GPL 3.0 or any newer version * * This program is free software: you can redistribute it and/or modify @@ -38,33 +50,13 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class ApplicationHelper extends BaseFrameworkSystem implements ManageableApplication, Registerable { - /** - * The version number of this application - */ - private $appVersion = ''; - - /** - * The human-readable name for this application - */ - private $appName = ''; - - /** - * The short uni*-like name for this application - */ - private $shortName = ''; - - /** - * An instance of this class - */ - private static $selfInstance = NULL; - +class ApplicationHelper extends BaseApplication implements ManageableApplication, Registerable { /** * Private constructor * * @return void */ - protected function __construct () { + private function __construct () { // Call parent constructor parent::__construct(__CLASS__); } @@ -76,105 +68,54 @@ class ApplicationHelper extends BaseFrameworkSystem implements ManageableApplica */ public static final function getSelfInstance () { // Is the instance there? - if (is_null(self::$selfInstance)) { - self::$selfInstance = new ApplicationHelper(); - } // END - if + if (is_null(self::getApplicationInstance())) { + self::setApplicationInstance(new ApplicationHelper()); + } // Return the instance - return self::$selfInstance; + return self::getApplicationInstance(); } /** - * Getter for the version number - * - * @return $appVersion The application's version number - */ - public final function getAppVersion () { - return $this->appVersion; - } - /** - * Setter for the version number + * 1) Setups application data * - * @param $appVersion The application's version number * @return void */ - public final function setAppVersion ($appVersion) { - // Cast and set it - $this->appVersion = (string) $appVersion; + public function setupApplicationData () { + // Set all application data + $this->setAppName('Ship-Simu Shipping Simulator'); + $this->setAppVersion('0.0.0'); + $this->setAppShortName('shipsimu'); } /** - * Getter for human-readable name + * 2) Does initial stuff before starting the application * - * @return $appName The application's human-readable name - */ - public final function getAppName () { - return $this->appName; - } - - /** - * Setter for human-readable name - * - * @param $appName The application's human-readable name * @return void */ - public final function setAppName ($appName) { - // Cast and set it - $this->appName = (string) $appName;; - } + public function initApplication () { + // Get config instance + $cfg = FrameworkBootstrap::getConfigurationInstance(); - /** - * Getter for short uni*-like name - * - * @return $shortName The application's short uni*-like name - */ - public final function getAppShortName () { - return $this->shortName; - } + // Initialize output system + ApplicationHelper::createDebugInstance('ApplicationHelper'); - /** - * Setter for short uni*-like name - * - * @param $shortName The application's short uni*-like name - * @return void - */ - public final function setAppShortName ($shortName) { - // Cast and set it - $this->shortName = (string) $shortName; + /* + * This application needs a database connection then simply call init + * method. + */ + FrameworkBootstrap::initDatabaseInstance(); } /** - * Launches the application + * 3) Launches the application * * @return void */ - public final function entryPoint () { - // Set this application in registry - Registry::getRegistry()->addInstance('app', $this); - - // Default response is console - $response = self::getResponseTypeFromSystem(); - $responseType = self::getResponseTypeFromSystem(); - - // Create a new request object - $requestInstance = ObjectFactory::createObjectByName(self::convertToClassName($response) . 'Request'); - - // Remember request instance here - $this->setRequestInstance($requestInstance); - - // Do we have another response? - if ($requestInstance->isRequestElementSet('request')) { - // Then use it - $response = strtolower($requestInstance->getRequestElement('request')); - $responseType = $response; - } // END - if - - // ... and a new response object - $responseClass = sprintf('%sResponse', self::convertToClassName($response)); - $responseInstance = ObjectFactory::createObjectByName($responseClass, array($this)); - - // Remember response instance here - $this->setResponseInstance($responseInstance); + public function launchApplication () { + // Get request/response instances + $requestInstance = FrameworkBootstrap::getRequestInstance(); + $responseInstance = FrameworkBootstrap::getResponseInstance(); // Get the parameter from the request $commandName = $requestInstance->getRequestElement('command'); @@ -184,31 +125,55 @@ class ApplicationHelper extends BaseFrameworkSystem implements ManageableApplica // Get default command $commandName = $responseInstance->determineDefaultCommand(); + // Debug message + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('commandName[%s]=%s', gettype($commandName), $commandName)); + // Set it in request $requestInstance->setRequestElement('command', $commandName); - } // END - if + } - // Get a controller resolver - $resolverClass = self::convertToClassName($this->getAppShortName() . '_' . $responseType . '_controller_resolver'); - $resolverInstance = ObjectFactory::createObjectByName($resolverClass, array($commandName, $this)); + // Is the request type 'html' ? + if (FrameworkBootstrap::getRequestTypeFromSystem() == 'html') { + // The language system is needed for this + $languageInstance = ObjectFactory::createObjectByConfiguredName('language_system_class'); - // Get a controller instance as well - $this->setControllerInstance($resolverInstance->resolveController()); + // And set it here + $this->setLanguageInstance($languageInstance); + } - // Initialize language system - $languageInstance = ObjectFactory::createObjectByConfiguredName('language_system_class'); + // Configuration entry key + $configEntry = sprintf( + '%s_%s_controller_resolver_class', + $this->getAppShortName(), + FrameworkBootstrap::getRequestTypeFromSystem() + ); - // And set it here - $this->setLanguageInstance($languageInstance); + // Get a controller resolver instance + $resolverInstance = ObjectFactory::createObjectByConfiguredName($configEntry, [ + $commandName, + ]); - // Launch the main routine here + // Get a controller instance as well + $this->setControllerInstance($resolverInstance->resolveController()); + + // Launch the test suite here $this->getControllerInstance()->handleRequest($requestInstance, $responseInstance); + + // Only for console requests as this is the actual daemon + if ($requestType == 'console') { + // -------------------------- Shutdown phase -------------------------- + // Shutting down the hub by saying "good bye" to all connected peers + // and other hubs, flushing all queues and caches. + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MAIN: Shutdown in progress, main loop exited.'); + $this->getControllerInstance()->executeShutdownFilters($requestInstance, $responseInstance); + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MAIN: Shutdown completed. (This is the last line.)'); + } } /** * Assigns extra application-depending data * - * @param $templateInstance An instance of a CompileableTemplate + * @param $templateInstance An instance of a CompileableTemplate class * @return void * @todo Nothing to add? */ @@ -226,7 +191,7 @@ class ApplicationHelper extends BaseFrameworkSystem implements ManageableApplica // Walk through all messages foreach ($messageList as $message) { exit(__METHOD__ . ':MSG:' . $message); - } // END - foreach + } } /** @@ -235,9 +200,6 @@ class ApplicationHelper extends BaseFrameworkSystem implements ManageableApplica * @return $masterTemplateName Name of the master template */ public function buildMasterTemplateName () { - return 'node_main'; + return 'node_daemon'; } } - -// [EOF] -?> diff --git a/application/shipsimu/config.php b/application/shipsimu/config.php index 948efec..72cccec 100644 --- a/application/shipsimu/config.php +++ b/application/shipsimu/config.php @@ -1,4 +1,7 @@ setConfigEntry('header_charset', 'utf-8'); diff --git a/application/shipsimu/data.php b/application/shipsimu/data.php index 7b380c2..7d6dcee 100644 --- a/application/shipsimu/data.php +++ b/application/shipsimu/data.php @@ -1,51 +1,2 @@ isClass("ApplicationSelector"))) { return; } - * - * isset() is required to prevent a warning and is_object() is highly required - * when the application itself is requested in URL (hint: index.php?app=your_app) - * - * @author Roland Haeder - * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Ship-Simu Developer Team - * @license GNU GPL 3.0 or any newer version - * @link http://www.shipsimu.org - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -// Get config instance -$cfg = FrameworkConfiguration::getSelfInstance(); - -// Get an instance of the helper -$app = call_user_func_array( - array($cfg->getConfigEntry('app_helper_class'), 'getSelfInstance'), - array() -); - -// Set application name and version -$app->setAppName('Ship-Simu Shipping Simulator'); -$app->setAppVersion('0.0.0'); -$app->setAppShortName('ship_simu'); - -// [EOF] -?> +// @DEPRECATED diff --git a/application/shipsimu/exceptions.php b/application/shipsimu/exceptions.php index 8e4e14f..28c8fea 100644 --- a/application/shipsimu/exceptions.php +++ b/application/shipsimu/exceptions.php @@ -1,12 +1,17 @@ * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Ship-Simu Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Ship-Simu Developer Team * @license GNU GPL 3.0 or any newer version - * @link http://www.shipsimu.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,32 +24,91 @@ * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with this program. If not, see . + * along with this program. If not, see . */ -// Our own exception handler -function __exceptionHandler (FrameworkException $e) { - // Call the app_exit() method - ApplicationEntryPoint::app_exit(sprintf("[Main:] The application %s (%s) has terminated due to an uncaught exception: %s [%s]: %s Backtrace:
%s
", - ApplicationHelper::getSelfInstance()->getAppName(), - ApplicationHelper::getSelfInstance()->getAppShortName(), - $e->__toString(), - $e->getHexCode(), - $e->getMessage(), - $e->getPrintableBackTrace() - ), - $e->getHexCode(), - $e->getExtraData() - ); -} // END - function +// The node's own exception handler +function shipsimu_exception_handler ($exceptionInstance) { + // Is it an object and a valid instance? + if ((is_object($exceptionInstance)) && ($exceptionInstance instanceof Exception)) { + // Init variable + $backTrace = ''; -// Set the new handler -set_exception_handler('__exceptionHandler'); + // Get all call levels from backtrace + foreach ($exceptionInstance->getTrace() as $idx => $traceArray) { + // Init argument string + $argsString = ''; + + // Arguments given? + if (isset($traceArray['args'])) { + // Convert arguments type into human-readable + foreach ($traceArray['args'] as $arg) { + $argsString .= ', ' . gettype($arg); + } + $argsString = substr($argsString, 2); + } + + // Set missing file/line + if (!isset($traceArray['file'])) $traceArray['file'] = 'unknown'; + if (!isset($traceArray['line'])) $traceArray['line'] = '0'; + if (!isset($traceArray['class'])) $traceArray['class'] = 'UnknownObject'; + if (!isset($traceArray['type'])) $traceArray['type'] = '->'; + + $backTrace .= sprintf("---------- Pos %d: ---------- +Method : %s%s%s(%s) +----- Caller: ----- +File : %s +Line : %d\n", + ($idx + 1), + $traceArray['class'], + $traceArray['type'], + $traceArray['function'], + $argsString, + basename($traceArray['file']), + $traceArray['line'] + ); + } + + // Construct the message + $message = sprintf("-------------------------------------------------------------------------------- +Uncaught Exception : %s +-------------------------------------------------------------------------------- +Message : %s +Code : %s +File : %s +Line : %d +-------------------------------------------------------------------------------- +Backtrace: +-------------------------------------------------------------------------------- +%s +--------------------------------------------------------------------------------\n", + trim(html_entity_decode(strip_tags(get_class($exceptionInstance)))), + trim(html_entity_decode(strip_tags($exceptionInstance->getMessage()))), + ($exceptionInstance instanceof FrameworkException ? $exceptionInstance->getHexCode() : '0x' . bin2hex($exceptionInstance->getCode())), + $exceptionInstance->getFile(), + $exceptionInstance->getLine(), + trim($backTrace) + ); + + // Output the message + print($message); + } elseif (is_object($exceptionInstance)) { + // Output more details + printf('exceptionInstance=%s', print_r($exceptionInstance, true)); + } else { + /* + * Invalid exception instance detected! Do *only* throw exceptions that + * extends our own exception 'FrameworkException' to get such nice + * outputs like above. + */ + printf('exceptionInstance[]=%s is invalid! Please inform the core developer team.' . PHP_EOL, gettype($exceptionInstance)); + } +} // Error handler -function __errorHandler ($errno, $errstr, $errfile, $errline, array $errcontext) { +function shipsimu_error_handler (int $errno, string $errstr, string $errfile, int $errline, array $errcontext) { // Construct the message - $message = sprintf("File: %s, Line: %s, Code: %s, Message: %s", + $message = sprintf('File: %s, Line: %d, Code: %d, Message: %s', basename($errfile), $errline, $errno, @@ -53,33 +117,38 @@ function __errorHandler ($errno, $errstr, $errfile, $errline, array $errcontext) // Throw an exception here throw new FatalErrorException($message, BaseFrameworkSystem::EXCEPTION_FATAL_ERROR); -} // END - function - -// Set error handler -set_error_handler('__errorHandler'); +} // Assertion handler -function __assertHandler ($file, $line, $code) { +function shipsimu_assert_handler (string $file, int $line, int $code) { // Empty code? - if ($code === "") $code = "Unknown"; + if (empty($code)) { + $code = 'Unknown'; + } // Create message - $message = sprintf("File: %s, Line: %s, Code: %s", + $message = sprintf('File: %s, Line: %d, Code: %d', basename($file), $line, $code ); + // Log assert + syslog(LOG_WARNING, $message); + // Throw an exception here throw new AssertionException($message, BaseFrameworkSystem::EXCEPTION_ASSERTION_FAILED); -} // END - function +} + +// Set error handler +//set_error_handler('shipsimu_error_handler'); + +// Set the new handler +set_exception_handler('shipsimu_exception_handler'); // Init assert handling -assert_options(ASSERT_ACTIVE, 1); -assert_options(ASSERT_WARNING, 0); -assert_options(ASSERT_BAIL, 0); -assert_options(ASSERT_QUIET_EVAL, 0); -assert_options(ASSERT_CALLBACK, '__assertHandler'); - -// [EOF] -?> +assert_options(ASSERT_ACTIVE , true); +assert_options(ASSERT_WARNING , true); +assert_options(ASSERT_BAIL , true); +assert_options(ASSERT_QUIET_EVAL, false); +assert_options(ASSERT_CALLBACK , 'shipsimu_assert_handler'); diff --git a/application/shipsimu/init.php b/application/shipsimu/init.php index 943eb79..7d6dcee 100644 --- a/application/shipsimu/init.php +++ b/application/shipsimu/init.php @@ -1,47 +1,2 @@ isClass("ApplicationSelector"))) { return; } - * - * isset() is required to prevent a warning and is_object() is highly required - * when the application itself is requested in URL (hint: index.php?app=your_app) - * - * @author Roland Haeder - * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Ship-Simu Developer Team - * @license GNU GPL 3.0 or any newer version - * @link http://www.shipsimu.org - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -// Get config instance -$cfg = FrameworkConfiguration::getSelfInstance(); - -// Initialize output system -ApplicationHelper::createDebugInstance('ApplicationHelper'); - -// This application needs a database connection then we have to simply include -// the inc/database.php script -require($cfg->getConfigEntry('base_path') . 'inc/database.php'); - -// [EOF] -?> +// @DEPRECATED diff --git a/application/shipsimu/loader.php b/application/shipsimu/loader.php index bcb574f..7d6dcee 100644 --- a/application/shipsimu/loader.php +++ b/application/shipsimu/loader.php @@ -1,29 +1,2 @@ - * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Ship-Simu Developer Team - * @license GNU GPL 3.0 or any newer version - * @link http://www.shipsimu.org - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -// Scan for application's classes, exceptions and interfaces -ClassLoader::scanApplicationClasses(); - -// [EOF] -?> +// @DEPRECATED diff --git a/application/shipsimu/starter.php b/application/shipsimu/starter.php index b6f4c45..7d6dcee 100644 --- a/application/shipsimu/starter.php +++ b/application/shipsimu/starter.php @@ -1,53 +1,2 @@ - * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Ship-Simu Developer Team - * @license GNU GPL 3.0 or any newer version - * @link http://www.shipsimu.org - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -// Is there an application helper instance? We need the method main() for -// maining the application -$app = call_user_func_array(array(FrameworkConfiguration::getSelfInstance()->getConfigEntry('app_helper_class'), 'getSelfInstance'), array()); - -// Some sanity checks -if ((empty($app)) || (is_null($app))) { - // Something went wrong! - ApplicationEntryPoint::app_exit(sprintf("[Main:] The application %s could not be launched because the helper class %s is not loaded.", - $application, - FrameworkConfiguration::getSelfInstance()->getConfigEntry('app_helper_class') - )); -} elseif (!is_object($app)) { - // No object! - ApplicationEntryPoint::app_exit(sprintf("[Main:] The application %s could not be launched because 'app' is not an object.", - $application - )); -} elseif (!method_exists($app, FrameworkConfiguration::getSelfInstance()->getConfigEntry('entry_method'))) { - // Method not found! - ApplicationEntryPoint::app_exit(sprintf("[Main:] The application %s could not be launched because the method %s is missing.", - $application, - FrameworkConfiguration::getSelfInstance()->getConfigEntry('entry_method') - )); -} - -// Call user function -call_user_func_array(array($app, FrameworkConfiguration::getSelfInstance()->getConfigEntry('entry_method')), array()); - -// [EOF] -?> +// @DEPRECATED -- 2.39.5