]> git.mxchange.org Git - core.git/blobdiff - framework/bootstrap/class_FrameworkBootstrap.php
Continued:
[core.git] / framework / bootstrap / class_FrameworkBootstrap.php
index 6894866ee72af9626fce1e5f96660aa85eaaa6b1..004b4c6fa87b1aefad2e04a915d72a44f9569dfb 100644 (file)
@@ -9,6 +9,7 @@ use Org\Mxchange\CoreFramework\Connector\Database\DatabaseConnector;
 use Org\Mxchange\CoreFramework\Console\Tools\ConsoleTools;
 use Org\Mxchange\CoreFramework\EntryPoint\ApplicationEntryPoint;
 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
+use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
 use Org\Mxchange\CoreFramework\Helper\Application\ApplicationHelper;
 use Org\Mxchange\CoreFramework\Localization\ManageableLanguage;
@@ -16,7 +17,6 @@ use Org\Mxchange\CoreFramework\Loader\ClassLoader;
 use Org\Mxchange\CoreFramework\Manager\ManageableApplication;
 use Org\Mxchange\CoreFramework\Middleware\Debug\DebugMiddleware;
 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
-use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
 use Org\Mxchange\CoreFramework\Request\Requestable;
 use Org\Mxchange\CoreFramework\Response\Responseable;
 use Org\Mxchange\CoreFramework\Utils\Strings\StringUtils;
@@ -31,7 +31,7 @@ use \SplFileInfo;
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2021 Core Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
@@ -186,30 +186,36 @@ final class FrameworkBootstrap {
         */
        public static function isReachableFilePath (SplFileInfo $fileInstance) {
                // Is not reachable by default
+               //* NOISY-DEBUG: */ printf('[%s:%d]: fileInstance=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, get_class($fileInstance));
                $isReachable = false;
 
                // Get open_basedir parameter
                $openBaseDir = trim(ini_get('open_basedir'));
 
                // Is it set?
+               //* NOISY-DEBUG: */ printf('[%s:%d]: openBaseDir=%s' . PHP_EOL, __METHOD__, __LINE__, $openBaseDir);
                if (!empty($openBaseDir)) {
                        // Check all entries
                        foreach (explode(PATH_SEPARATOR, $openBaseDir) as $dir) {
                                // Check on existence
+                               //* NOISY-DEBUG: */ printf('[%s:%d]: dir=%s' . PHP_EOL, __METHOD__, __LINE__, $dir);
                                if (substr($fileInstance->getPathname(), 0, strlen($dir)) == $dir) {
                                        // Is reachable
                                        $isReachable = true;
 
                                        // Abort lookup as it has been found in open_basedir
+                                       //* NOISY-DEBUG: */ printf('[%s:%d]: BREAK!' . PHP_EOL, __METHOD__, __LINE__);
                                        break;
                                }
                        }
                } else {
                        // If open_basedir is not set, all is allowed
+                       //* NOISY-DEBUG: */ printf('[%s:%d]: All is allowed - BREAK!' . PHP_EOL, __METHOD__, __LINE__);
                        $isReachable = true;
                }
 
                // Return status
+               //* NOISY-DEBUG: */ printf('[%s:%d]: isReachable=%d - EXIT' . PHP_EOL, __METHOD__, __LINE__, intval($isReachable));
                return $isReachable;
        }
 
@@ -221,10 +227,8 @@ final class FrameworkBootstrap {
         * @return      $isReadable             Whether the file is readable (and therefor exists)
         */
        public static function isReadableFile (SplFileInfo $fileInstance) {
-               // Default is not readable
-               $isReadable = false;
-
                // Check if it is a file and readable
+               //* NOISY-DEBUG: */ printf('[%s:%d]: fileInstance=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, get_class($fileInstance));
                $isReadable = (
                        (
                                self::isReachableFilePath($fileInstance)
@@ -236,6 +240,7 @@ final class FrameworkBootstrap {
                );
 
                // Return status
+               //* NOISY-DEBUG: */ printf('[%s:%d]: isReadable=%d - EXIT!' . PHP_EOL, __METHOD__, __LINE__, intval($isReadable));
                return $isReadable;
        }
 
@@ -248,10 +253,16 @@ final class FrameworkBootstrap {
         */
        public static function loadInclude (SplFileInfo $fileInstance) {
                // Should be there ...
-               //* NOISY-DEBUG: */ printf('[%s:%d]: fileInstance=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $fileInstance);
+               //* NOISY-DEBUG: */ printf('[%s:%d]: fileInstance=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $fileInstance->__toString());
                if (!self::isReadableFile($fileInstance)) {
                        // Abort here
                        throw new InvalidArgumentException(sprintf('Cannot find fileInstance.pathname=%s.', $fileInstance->getPathname()));
+               } elseif (!$fileInstance->isFile()) {
+                       // Not a file
+                       throw new InvalidArgumentException(sprintf('fileInstance.pathname=%s is not a file', $fileInstance->getPathname()));
+               } elseif (substr($fileInstance->__toString(), -4, 4) != '.php') {
+                       // Not PHP script
+                       throw new InvalidArgumentException(sprintf('fileInstance.pathname=%s is not a PHP script', $fileInstance->getPathname()));
                }
 
                // Load it
@@ -300,6 +311,7 @@ final class FrameworkBootstrap {
                 * 1) Load class loader and scan framework classes, interfaces and
                 *    exceptions.
                 */
+               //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
                self::scanFrameworkClasses();
 
                /*
@@ -316,6 +328,9 @@ final class FrameworkBootstrap {
                 *    found, continue below with next step.
                 */
                self::validateApplicationParameter();
+
+               // Trace message
+               //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
        }
 
        /**
@@ -330,8 +345,10 @@ final class FrameworkBootstrap {
                 * Now check and load all files, found deprecated files will throw a
                 * warning at the user.
                 */
+               //* NOISY-DEBUG: */ printf('[%s:%d]: self::configAppIncludes()=%d - CALLED!' . PHP_EOL, __METHOD__, __LINE__, count(self::$configAppIncludes));
                foreach (self::$configAppIncludes as $fileName => $status) {
                        // Construct file instance
+                       //* NOISY-DEBUG: */ printf('[%s:%d]: fileName=%s,status=%s' . PHP_EOL, __METHOD__, __LINE__, $fileName, $status);
                        $fileInstance = new SplFileInfo(sprintf('%s%s.php', self::getDetectedApplicationPath(), $fileName));
 
                        // Determine if this file is wanted/readable/deprecated
@@ -343,6 +360,7 @@ final class FrameworkBootstrap {
                                ApplicationEntryPoint::exitApplication(sprintf('File "%s.php" from application "%s" cannot be read. Please fix CHMOD.', $fileInstance->getBasename(), self::getDetectedApplicationName()));
                        } elseif (($status != 'required') && (!self::isReadableFile($fileInstance))) {
                                // Not found but optional/deprecated file, skip it
+                               //* NOISY-DEBUG: */ printf('[%s:%d]: fileName=%s,status=%s - SKIPPED!' . PHP_EOL, __METHOD__, __LINE__, $fileName, $status);
                                continue;
                        }
 
@@ -356,11 +374,18 @@ final class FrameworkBootstrap {
                        }
 
                        // Load it
+                       //* NOISY-DEBUG: */ printf('[%s:%d]: Invoking self::loadInclude(%s) ...' . PHP_EOL, __METHOD__, __LINE__, $fileInstance->__toString());
                        self::loadInclude($fileInstance);
                }
 
+               // After this, sort the configuration array
+               self::getConfigurationInstance()->sortConfigurationArray();
+
                // Scan for application's classes, exceptions and interfaces
                ClassLoader::scanApplicationClasses();
+
+               // Trace message
+               //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
        }
 
        /**
@@ -371,13 +396,15 @@ final class FrameworkBootstrap {
         */
        public static function startApplication () {
                // Is there an application helper instance?
+               //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
                $applicationInstance = call_user_func_array(
-                       array(
+                       [
                                'Org\Mxchange\CoreFramework\Helper\Application\ApplicationHelper', 'getSelfInstance'
-                       ), []
+                       ], []
                );
 
                // Some sanity checks
+               //* NOISY-DEBUG: */ printf('[%s:%d]: applicationInstance=%s' . PHP_EOL, __METHOD__, __LINE__, $applicationInstance->__toString());
                if ((empty($applicationInstance)) || (is_null($applicationInstance))) {
                        // Something went wrong!
                        ApplicationEntryPoint::exitApplication(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.',
@@ -398,23 +425,30 @@ final class FrameworkBootstrap {
                }
 
                // Now call all methods in one go
+               //* NOISY-DEBUG: */ printf('[%s:%d]: Initializing application ...' . PHP_EOL, __METHOD__, __LINE__);
                foreach (['setupApplicationData', 'initApplication', 'launchApplication'] as $methodName) {
                        // Call method
-                       //*NOISY-DEBUG: */ printf('[%s:%d]: Calling methodName=%s ...' . PHP_EOL, __METHOD__, __LINE__, $methodName);
+                       //*NOISY-DEBUG: */ printf('[%s:%d]: Invoking methodName=%s ...' . PHP_EOL, __METHOD__, __LINE__, $methodName);
                        call_user_func([$applicationInstance, $methodName]);
                }
+
+               // Trace message
+               //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
        }
 
        /**
         * Initializes database instance, no need to double-call this method
         *
         * @return      void
+        * @throws      BadMethodCallException  If this method was invoked twice
         */
        public static function initDatabaseInstance () {
                // Get application instance
+               //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
                $applicationInstance = ApplicationHelper::getSelfInstance();
 
                // Is the database instance already set?
+               //* NOISY-DEBUG: */ printf('[%s:%d]: self::databaseInstance[]=%s' . PHP_EOL, __METHOD__, __LINE__, gettype(self::getDatabaseInstance()));
                if (self::getDatabaseInstance() instanceof DatabaseConnector) {
                        // Yes, then abort here
                        throw new BadMethodCallException('Method called twice.');
@@ -424,10 +458,14 @@ final class FrameworkBootstrap {
                $databaseInstance = ObjectFactory::createObjectByConfiguredName(self::getConfigurationInstance()->getConfigEntry('database_type') . '_class');
 
                // Prepare database instance
-               $connectionInstance = DatabaseConnection::createDatabaseConnection(DebugMiddleware::getSelfInstance(), $databaseInstance);
+               $connectionInstance = DatabaseConnection::createDatabaseConnection($databaseInstance);
 
                // Set it in application helper
+               //* NOISY-DEBUG: */ printf('[%s:%d]: Setting connectionInstance=%s ...' . PHP_EOL, __METHOD__, __LINE__, $connectionInstance->__toString());
                self::setDatabaseInstance($connectionInstance);
+
+               // Trace message
+               //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
        }
 
        /**
@@ -439,16 +477,20 @@ final class FrameworkBootstrap {
         */
        public static function detectServerAddress () {
                // Is the entry set?
+               //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
                if (!isset(self::$serverAddress)) {
                        // Is it set in $_SERVER?
                        if (!empty($_SERVER['SERVER_ADDR'])) {
                                // Set it from $_SERVER
+                               //* NOISY-DEBUG: */ printf('[%s:%d]: Setting self::serverAddress=%s from SERVER_ADDR ...' . PHP_EOL, __METHOD__, __LINE__, $_SERVER['SERVER_ADDR']);
                                self::$serverAddress = $_SERVER['SERVER_ADDR'];
                        } elseif (isset($_SERVER['SERVER_NAME'])) {
                                // Resolve IP address
+                               //* NOISY-DEBUG: */ printf('[%s:%d]: Resolving SERVER_NAME=%s ...' . PHP_EOL, __METHOD__, __LINE__, $_SERVER['SERVER_NAME']);
                                $serverIp = ConsoleTools::resolveIpAddress($_SERVER['SERVER_NAME']);
 
                                // Is it valid?
+                               //* NOISY-DEBUG: */ printf('[%s:%d]: serverId[%s]=%s' . PHP_EOL, __METHOD__, __LINE__, gettype($serverIp), $serverIp);
                                if ($serverIp === false) {
                                        /*
                                         * Why is gethostbyname() returning the host name and not
@@ -458,14 +500,17 @@ final class FrameworkBootstrap {
                                }
 
                                // Al fine, set it
+                               //* NOISY-DEBUG: */ printf('[%s:%d]: Setting self::serverAddress=%s from resolver ...' . PHP_EOL, __METHOD__, __LINE__, $serverAddress);
                                self::$serverAddress = $serverIp;
                        } else {
                                // Run auto-detecting through console tools lib
+                               //* NOISY-DEBUG: */ printf('[%s:%d]: Invoking ConsoleTools::acquireSelfIpAddress() ...' . PHP_EOL, __METHOD__, __LINE__);
                                self::$serverAddress = ConsoleTools::acquireSelfIpAddress();
                        }
                }
 
                // Return it
+               //* NOISY-DEBUG: */ printf('[%s:%d]: self::serverAddress=%s - EXIT!' . PHP_EOL, __METHOD__, __LINE__, self::$serverAddress);
                return self::$serverAddress;
        }
 
@@ -478,9 +523,10 @@ final class FrameworkBootstrap {
         */
        public static function setDefaultTimezone (string $timezone) {
                // Is it set?
+               //* NOISY-DEBUG: */ printf('[%s:%d]: timezone=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $timezone);
                if (empty($timezone)) {
                        // Entry is empty
-                       throw new InvalidArgumentException('timezone is empty');
+                       throw new InvalidArgumentException('Parameter "timezone" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
                }
 
                // Default success
@@ -493,6 +539,7 @@ final class FrameworkBootstrap {
                $success = date_default_timezone_set($timezone);
 
                // Return status
+               //* NOISY-DEBUG: */ printf('[%s:%d]: success=%d - EXIT!' . PHP_EOL, __METHOD__, __LINE__, intval($success));
                return $success;
        }
 
@@ -503,6 +550,7 @@ final class FrameworkBootstrap {
         * @todo        Test more fields
         */
        public static function isHttpSecured () {
+               //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
                return (
                        (
                                (
@@ -527,6 +575,7 @@ final class FrameworkBootstrap {
         */
        public static function detectBaseUrl () {
                // Initialize the URL
+               //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
                $protocol = 'http';
 
                // Do we have HTTPS?
@@ -539,6 +588,7 @@ final class FrameworkBootstrap {
                $baseUrl = sprintf('%s://%s%s', $protocol, self::detectDomain(), self::detectScriptPath());
 
                // Return the URL
+               //* NOISY-DEBUG: */ printf('[%s:%d]: baseUrl=%s - EXIT!' . PHP_EOL, __METHOD__, __LINE__, $baseUrl);
                return $baseUrl;
        }
 
@@ -549,6 +599,7 @@ final class FrameworkBootstrap {
         */
        public static function detectDomain () {
                // Full domain is localnet.invalid by default
+               //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
                $fullDomain = 'localnet.invalid';
 
                // Is the server name there?
@@ -558,6 +609,7 @@ final class FrameworkBootstrap {
                }
 
                // Return it
+               //* NOISY-DEBUG: */ printf('[%s:%d]: fullDomain=%s - EXIT!' . PHP_EOL, __METHOD__, __LINE__, $fullDomain);
                return $fullDomain;
        }
 
@@ -569,6 +621,7 @@ final class FrameworkBootstrap {
         */
        public static function detectScriptPath () {
                // Default is empty
+               //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
                $scriptPath = '';
 
                // Is the scriptname set?
@@ -578,6 +631,7 @@ final class FrameworkBootstrap {
                }
 
                // Return it
+               //* NOISY-DEBUG: */ printf('[%s:%d]: scriptPath=%s - EXIT!' . PHP_EOL, __METHOD__, __LINE__, $scriptPath);
                return $scriptPath;
        }
 
@@ -595,6 +649,7 @@ final class FrameworkBootstrap {
         */
        private static function scanFrameworkClasses () {
                // Include class loader
+               //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
                require self::getConfigurationInstance()->getConfigEntry('framework_base_path') . 'loader/class_ClassLoader.php';
 
                // Register auto-load function with the SPL
@@ -602,6 +657,9 @@ final class FrameworkBootstrap {
 
                // Scan for all framework classes, exceptions and interfaces
                ClassLoader::scanFrameworkClasses();
+
+               // Trace message
+               //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
        }
 
        /**
@@ -612,13 +670,16 @@ final class FrameworkBootstrap {
         */
        private static function determineRequestType () {
                // Determine request type
+               //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
                $request = self::getRequestTypeFromSystem();
                $requestType = self::getRequestTypeFromSystem();
 
                // Create a new request object
+               //* NOISY-DEBUG: */ printf('[%s:%d]: request=%s,requestType=%s' . PHP_EOL, __METHOD__, __LINE__, $request, $requestType);
                $requestInstance = ObjectFactory::createObjectByName(sprintf('Org\Mxchange\CoreFramework\Request\%sRequest', StringUtils::convertToClassName($request)));
 
                // Remember request instance here
+               //* NOISY-DEBUG: */ printf('[%s:%d]: Setting requestInstance=%s ...' . PHP_EOL, __METHOD__, __LINE__, $requestInstance->__toString());
                self::setRequestInstance($requestInstance);
 
                // Do we have another response?
@@ -629,10 +690,15 @@ final class FrameworkBootstrap {
                }
 
                // ... and a new response object
+               //* NOISY-DEBUG: */ printf('[%s:%d]: request=%s,requestType=%s - AFTER!' . PHP_EOL, __METHOD__, __LINE__, $request, $requestType);
                $responseInstance = ObjectFactory::createObjectByName(sprintf('Org\Mxchange\CoreFramework\Response\%sResponse', StringUtils::convertToClassName($request)));
 
                // Remember response instance here
+               //* NOISY-DEBUG: */ printf('[%s:%d]: Setting responseInstance=%s ...' . PHP_EOL, __METHOD__, __LINE__, $responseInstance->__toString());
                self::setResponseInstance($responseInstance);
+
+               // Trace message
+               //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
        }
 
        /**
@@ -642,6 +708,7 @@ final class FrameworkBootstrap {
         */
        private static function validateApplicationParameter () {
                // Is the parameter set?
+               //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
                if (!self::getRequestInstance()->isRequestElementSet('app')) {
                        /*
                         * Don't continue here, the application 'selector' is no longer
@@ -655,12 +722,15 @@ final class FrameworkBootstrap {
                $applicationName = self::getRequestInstance()->getRequestElement('app');
 
                // Secure it, by keeping out tags
+               //* NOISY-DEBUG: */ printf('[%s:%d]: applicationName=%s' . PHP_EOL, __METHOD__, __LINE__, $applicationName);
                $applicationName = htmlentities(strip_tags($applicationName), ENT_QUOTES);
 
                // Secure it a little more with a reg.exp.
+               //* NOISY-DEBUG: */ printf('[%s:%d]: applicationName=%s' . PHP_EOL, __METHOD__, __LINE__, $applicationName);
                $applicationName = preg_replace('/([^a-z0-9_-])+/i', '', $applicationName);
 
                // Construct FQPN (Full-Qualified Path Name) for ApplicationHelper class
+               //* NOISY-DEBUG: */ printf('[%s:%d]: applicationName=%s' . PHP_EOL, __METHOD__, __LINE__, $applicationName);
                $applicationPath = sprintf(
                        '%s%s%s',
                        self::getConfigurationInstance()->getConfigEntry('application_base_path'),
@@ -670,14 +740,19 @@ final class FrameworkBootstrap {
 
                // Full path for application
                // Is the path there? This secures a bit the parameter (from untrusted source).
+               //* NOISY-DEBUG: */ printf('[%s:%d]: applicationPath=%s' . PHP_EOL, __METHOD__, __LINE__, $applicationPath);
                if ((!is_dir($applicationPath)) || (!is_readable($applicationPath))) {
                        // Not found or not readable
                        ApplicationEntryPoint::exitApplication(sprintf('Application "%s" not found.', $applicationName));
                }
 
                // Set the detected application's name and full path for later usage
+               //* NOISY-DEBUG: */ printf('[%s:%d]: Setting applicationPath=%s,applicationName=%s' . PHP_EOL, __METHOD__, __LINE__, $applicationPath, $applicationName);
                self::$detectedApplicationPath = $applicationPath;
                self::$detectedApplicationName = $applicationName;
+
+               // Trace message
+               //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
        }
 
        /**