<?php
+// Must be this namespace, else the launcher cannot find the class.
+namespace Org\Mxchange\CoreFramework\Helper\Application;
+
+// Import framework stuff
+use Org\Mxchange\CoreFramework\Application\BaseApplication;
+use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
+use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
+use Org\Mxchange\CoreFramework\Loader\ClassLoader;
+use Org\Mxchange\CoreFramework\Manager\ManageableApplication;
+use Org\Mxchange\CoreFramework\Registry\Registerable;
+use Org\Mxchange\CoreFramework\Template\CompileableTemplate;
+
/**
* A class holding general data about the application and some methods for
* the management including the entry point.
*
* @author Roland Haeder <webmaster@shipsimu.org>
* @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
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-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__);
}
*/
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');
// 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?
*/
// Walk through all messages
foreach ($messageList as $message) {
exit(__METHOD__ . ':MSG:' . $message);
- } // END - foreach
+ }
}
/**
* @return $masterTemplateName Name of the master template
*/
public function buildMasterTemplateName () {
- return 'node_main';
+ return 'node_daemon';
}
}
-
-// [EOF]
-?>
<?php
+// Import framework stuff
+use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
+
/**
* Additional/overwritten configuration parts
*
*/
// Get a configuration instance for shorter lines
-$cfg = FrameworkConfiguration::getSelfInstance();
+$cfg = FrameworkBootstrap::getConfigurationInstance();
// CFG: HEADER-CHARSET
$cfg->setConfigEntry('header_charset', 'utf-8');
<?php
-/**
- * Application data
- *
- * Please remember that this include file is being loaded *before* the class
- * loader is loading classes from "exceptions", "interfaces" and "main"!
- *
- * You can prevent adding this application to the selector by uncommenting the
- * following line:
- *
- * if ((isset($this)) && (is_object($this)) && ($this->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 <webmaster@shipsimu.org>
- * @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 <http://www.gnu.org/licenses/>.
- */
-
-// 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
<?php
+// Import framework stuff
+use Org\Mxchange\CoreFramework\Assertion\AssertionException;
+use Org\Mxchange\CoreFramework\Error\FatalErrorException;
+use Org\Mxchange\CoreFramework\Generic\FrameworkException;
+use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
+
/**
- * The exception handler for this application
+ * An include file for setting up the exception handler of test suite
*
* @author Roland Haeder <webmaster@shipsimu.org>
* @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
* 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 <http://www.gnu.org/licenses/>.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-// Our own exception handler
-function __exceptionHandler (FrameworkException $e) {
- // Call the app_exit() method
- ApplicationEntryPoint::app_exit(sprintf("[Main:] The application <span class=\"app_name\">%s</span> (<span class=\"app_short_name\">%s</span>) has terminated due to an uncaught exception: <span class=\"exception_name\">%s</span> <span class=\"exception_number\">[%s]</span>: <span class=\"debug_exception\">%s</span> Backtrace: <div class=\"debug_backtrace\">%s</div>",
- 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: <span class=\"debug_file\">%s</span>, Line: <span class=\"debug_line\">%s</span>, Code: <span class=\"debug_code\">%s</span>, Message: <span class=\"debug_message\">%s</span>",
+ $message = sprintf('File: %s, Line: %d, Code: %d, Message: %s',
basename($errfile),
$errline,
$errno,
// 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 = "<em>Unknown</em>";
+ if (empty($code)) {
+ $code = '<em>Unknown</em>';
+ }
// Create message
- $message = sprintf("File: <span class=\"debug_file\">%s</span>, Line: <span class=\"debug_line\">%s</span>, Code: <span class=\"debug_code\">%s</span>",
+ $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');
<?php
-/**
- * Application initializer
- *
- * Please remember that this include file is being loaded *before* the class
- * loader is loading classes from "exceptions", "interfaces" and "main"!
- *
- * You can prevent adding this application to the selector by uncommenting the
- * following line:
- *
- * if ((isset($this)) && (is_object($this)) && ($this->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 <webmaster@shipsimu.org>
- * @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 <http://www.gnu.org/licenses/>.
- */
-
-// 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
<?php
-/**
- * A specialized class loader for this class
- *
- * @author Roland Haeder <webmaster@shipsimu.org>
- * @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 <http://www.gnu.org/licenses/>.
- */
-
-// Scan for application's classes, exceptions and interfaces
-ClassLoader::scanApplicationClasses();
-
-// [EOF]
-?>
+// @DEPRECATED
<?php
-/**
- * The application launcher
- *
- * @author Roland Haeder <webmaster@shipsimu.org>
- * @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 <http://www.gnu.org/licenses/>.
- */
-
-// 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 <span class=\"app_name\">%s</span> could not be launched because the helper class <span class=\"class_name\">%s</span> is not loaded.",
- $application,
- FrameworkConfiguration::getSelfInstance()->getConfigEntry('app_helper_class')
- ));
-} elseif (!is_object($app)) {
- // No object!
- ApplicationEntryPoint::app_exit(sprintf("[Main:] The application <span class=\"app_name\">%s</span> 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 <span class=\"app_name\">%s</span> could not be launched because the method <span class=\"method_name\">%s</span> 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