]> git.mxchange.org Git - core.git/blobdiff - framework/bootstrap/class_FrameworkBootstrap.php
Continued:
[core.git] / framework / bootstrap / class_FrameworkBootstrap.php
index ad1bf8a2a3168c816564bf81187affa9286a5a6f..4790acda2c17bbda6d8d08e19085e8adc6d3e67d 100644 (file)
@@ -8,7 +8,7 @@ use Org\Mxchange\CoreFramework\Connection\Database\DatabaseConnection;
 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\ObjectFactory;
+use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
 use Org\Mxchange\CoreFramework\Helper\Application\ApplicationHelper;
 use Org\Mxchange\CoreFramework\Localization\ManageableLanguage;
@@ -84,7 +84,7 @@ final class FrameworkBootstrap {
         * Includes applications may have. They will be tried in the given order,
         * some will become soon deprecated.
         */
-       private static $configAppIncludes = array(
+       private static $configAppIncludes = [
                // The ApplicationHelper class (required)
                'class_ApplicationHelper' => 'required',
                // Some debugging stuff (optional but can be committed)
@@ -103,7 +103,17 @@ final class FrameworkBootstrap {
                'init'                    => 'deprecated',
                // Application starter (deprecated)
                'starter'                 => 'deprecated',
-       );
+       ];
+
+       /**
+        * Detected application's name
+        */
+       private static $detectedApplicationName;
+
+       /**
+        * Detected application's full path
+        */
+       private static $detectedApplicationPath;
 
        /**
         * Private constructor, no instance is needed from this class as only
@@ -123,12 +133,30 @@ final class FrameworkBootstrap {
                if (is_null(self::$configurationInstance)) {
                        // Init new instance
                        self::$configurationInstance = new FrameworkConfiguration();
-               } // END - if
+               }
 
                // Return it
                return self::$configurationInstance;
        }
 
+       /**
+        * Getter for detected application name
+        *
+        * @return      $detectedApplicationName        Detected name of application
+        */
+       public static function getDetectedApplicationName () {
+               return self::$detectedApplicationName;
+       }
+
+       /**
+        * Getter for detected application's full path
+        *
+        * @return      $detectedApplicationPath        Detected full path of application
+        */
+       public static function getDetectedApplicationPath () {
+               return self::$detectedApplicationPath;
+       }
+
        /**
         * "Getter" to get response/request type from analysis of the system.
         *
@@ -142,7 +170,7 @@ final class FrameworkBootstrap {
                if (isset($_SERVER['HTTP_HOST'])) {
                        // Then it is a HTML response/request.
                        $requestType = 'html';
-               } // END - if
+               }
 
                // Return it
                return $requestType;
@@ -174,8 +202,8 @@ final class FrameworkBootstrap {
 
                                        // Abort lookup as it has been found in open_basedir
                                        break;
-                               } // END - if
-                       } // END - foreach
+                               }
+                       }
                } else {
                        // If open_basedir is not set, all is allowed
                        $isReachable = true;
@@ -226,7 +254,7 @@ final class FrameworkBootstrap {
                if (!self::isReadableFile($fileInstance)) {
                        // Abort here
                        throw new InvalidArgumentException(sprintf('Cannot find fileInstance.pathname=%s.', $fileInstance->getPathname()));
-               } // END - if
+               }
 
                // Load it
                require_once $fileInstance->getPathname();
@@ -270,7 +298,7 @@ final class FrameworkBootstrap {
 
                /*
                 * 2) Determine the request type, console or web and store request and
-                *    response here. This also initializes the request instance will
+                *    response here. This also initializes the request instance with
                 *    all given parameters (see doc-tag for possible sources of
                 *    parameters).
                 */
@@ -292,25 +320,21 @@ final class FrameworkBootstrap {
         * @return      void
         */
        public static function prepareApplication () {
-               // Configuration entry 'detected_app_name' must be set, get it here, including full path
-               $application = self::getConfigurationInstance()->getConfigEntry('detected_app_name');
-               $fullPath    = self::getConfigurationInstance()->getConfigEntry('detected_full_app_path');
-
                /*
                 * Now check and load all files, found deprecated files will throw a
                 * warning at the user.
                 */
                foreach (self::$configAppIncludes as $fileName => $status) {
                        // Construct file instance
-                       $fileInstance = new SplFileInfo(sprintf('%s%s.php', $fullPath, $fileName));
+                       $fileInstance = new SplFileInfo(sprintf('%s%s.php', self::getDetectedApplicationPath(), $fileName));
 
                        // Determine if this file is wanted/readable/deprecated
                        if (($status == 'required') && (!self::isReadableFile($fileInstance))) {
                                // Nope, required file cannot be found/read from
-                               ApplicationEntryPoint::exitApplication(sprintf('Application "%s" does not have required file "%s.php". Please add it.', $application, $fileInstance->getBasename()));
+                               ApplicationEntryPoint::exitApplication(sprintf('Application "%s" does not have required file "%s.php". Please add it.', self::getDetectedApplicationName(), $fileInstance->getBasename()));
                        } elseif (($fileInstance->isFile()) && (!$fileInstance->isReadable())) {
                                // Found, not readable file
-                               ApplicationEntryPoint::exitApplication(sprintf('File "%s.php" from application "%s" cannot be read. Please fix CHMOD.', $fileInstance->getBasename(), $application));
+                               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
                                continue;
@@ -319,15 +343,15 @@ final class FrameworkBootstrap {
                        // Is the file deprecated?
                        if ($status == 'deprecated') {
                                // Issue warning
-                               trigger_error(sprintf('Deprecated file "%s.php" found, will not load it to avoid problems. Please remove it from your application "%s" to avoid this warning.', $fileName, $application), E_USER_WARNING);
+                               trigger_error(sprintf('Deprecated file "%s.php" found, will not load it to avoid problems. Please remove it from your application "%s" to avoid this warning.', $fileName, self::getDetectedApplicationName()), E_USER_WARNING);
 
                                // Skip loading deprecated file
                                continue;
-                       } // END - if
+                       }
 
                        // Load it
                        self::loadInclude($fileInstance);
-               } // END - foreach
+               }
 
                // Scan for application's classes, exceptions and interfaces
                ClassLoader::scanApplicationClasses();
@@ -340,33 +364,30 @@ final class FrameworkBootstrap {
         * @return      void
         */
        public static function startApplication () {
-               // Configuration entry 'detected_app_name' must be set, get it here
-               $application = self::getConfigurationInstance()->getConfigEntry('detected_app_name');
-
                // Is there an application helper instance?
                $applicationInstance = call_user_func_array(
                        array(
                                'Org\Mxchange\CoreFramework\Helper\Application\ApplicationHelper', 'getSelfInstance'
-                       ), array()
+                       ), []
                );
 
                // Some sanity checks
                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.',
-                               $application,
+                               self::getDetectedApplicationName(),
                                'Org\Mxchange\CoreFramework\Helper\Application\ApplicationHelper'
                        ));
                } elseif (!is_object($applicationInstance)) {
                        // No object!
                        ApplicationEntryPoint::exitApplication(sprintf('[Main:] The application <span class="app_name">%s</span> could not be launched because &#39;app&#39; is not an object (%s).',
-                               $application,
+                               self::getDetectedApplicationName(),
                                gettype($applicationInstance)
                        ));
                } elseif (!($applicationInstance instanceof ManageableApplication)) {
                        // Missing interface
                        ApplicationEntryPoint::exitApplication(sprintf('[Main:] The application <span class="app_name">%s</span> could not be launched because &#39;app&#39; is lacking required interface ManageableApplication.',
-                               $application
+                               self::getDetectedApplicationName()
                        ));
                }
 
@@ -377,7 +398,7 @@ final class FrameworkBootstrap {
 
                        // Call method
                        call_user_func(array($applicationInstance, $methodName));
-               } // END - foreach
+               }
        }
 
        /**
@@ -393,7 +414,7 @@ final class FrameworkBootstrap {
                if (self::getDatabaseInstance() instanceof DatabaseConnector) {
                        // Yes, then abort here
                        throw new BadMethodCallException('Method called twice.');
-               } // END - if
+               }
 
                // Initialize database layer
                $databaseInstance = ObjectFactory::createObjectByConfiguredName(self::getConfigurationInstance()->getConfigEntry('database_type') . '_class');
@@ -430,7 +451,7 @@ final class FrameworkBootstrap {
                                         * false as many other PHP functions are doing? ;-(
                                         */
                                        throw new UnknownHostnameException(sprintf('Cannot resolve "%s" to an IP address. Please fix your setup.', $_SERVER['SERVER_NAME']));
-                               } // END - if
+                               }
 
                                // Al fine, set it
                                self::$serverAddress = $serverIp;
@@ -438,7 +459,7 @@ final class FrameworkBootstrap {
                                // Run auto-detecting through console tools lib
                                self::$serverAddress = ConsoleTools::acquireSelfIpAddress();
                        }
-               } // END - if
+               }
 
                // Return it
                return self::$serverAddress;
@@ -449,18 +470,11 @@ final class FrameworkBootstrap {
         *
         * @param       $timezone       The timezone string (e.g. Europe/Berlin)
         * @return      $success        If timezone was accepted
-        * @throws      NullPointerException    If $timezone is NULL
         * @throws      InvalidArgumentException        If $timezone is empty
         */
-       public static function setDefaultTimezone ($timezone) {
-               // Is it null?
-               if (is_null($timezone)) {
-                       // Throw NPE
-                       throw new NullPointerException(NULL, BaseFrameworkSystem::EXCEPTION_IS_NULL_POINTER);
-               } elseif (!is_string($timezone)) {
-                       // Is not a string
-                       throw new InvalidArgumentException(sprintf('timezone[]=%s is not a string', gettype($timezone)));
-               } elseif ((is_string($timezone)) && (empty($timezone))) {
+       public static function setDefaultTimezone (string $timezone) {
+               // Is it set?
+               if (empty($timezone)) {
                        // Entry is empty
                        throw new InvalidArgumentException('timezone is empty');
                }
@@ -515,7 +529,7 @@ final class FrameworkBootstrap {
                if (self::isHttpSecured()) {
                        // Add the >s< for HTTPS
                        $protocol = 'https';
-               } // END - if
+               }
 
                // Construct the full URL and secure it against CSRF attacks
                $baseUrl = sprintf('%s://%s%s', $protocol, self::detectDomain(), self::detectScriptPath());
@@ -537,7 +551,7 @@ final class FrameworkBootstrap {
                if (isset($_SERVER['SERVER_NAME'])) {
                        // Detect the full domain
                        $fullDomain = htmlentities(strip_tags($_SERVER['SERVER_NAME']), ENT_QUOTES);
-               } // END - if
+               }
 
                // Return it
                return $fullDomain;
@@ -557,7 +571,7 @@ final class FrameworkBootstrap {
                if (isset($_SERVER['SCRIPT_NAME'])) {
                        // Get dirname from it and replace back-slashes with slashes for lame OSes...
                        $scriptPath = str_replace("\\", '/', dirname($_SERVER['SCRIPT_NAME']));
-               } // END - if
+               }
 
                // Return it
                return $scriptPath;
@@ -608,7 +622,7 @@ final class FrameworkBootstrap {
                        // Then use it
                        $request = strtolower($requestInstance->getRequestElement('request'));
                        $requestType = $request;
-               } // END - if
+               }
 
                // ... and a new response object
                $responseClass = sprintf('Org\Mxchange\CoreFramework\Response\%sResponse', StringUtils::convertToClassName($request));
@@ -632,22 +646,22 @@ final class FrameworkBootstrap {
                         * application (by user).
                         */
                        ApplicationEntryPoint::exitApplication('No application specified. Please provide a parameter "app" and retry.');
-               } // END - if
+               }
 
                // Get it for local usage
-               $application = self::getRequestInstance()->getRequestElement('app');
+               $applicationName = self::getRequestInstance()->getRequestElement('app');
 
                // Secure it, by keeping out tags
-               $application = htmlentities(strip_tags($application), ENT_QUOTES);
+               $applicationName = htmlentities(strip_tags($applicationName), ENT_QUOTES);
 
                // Secure it a little more with a reg.exp.
-               $application = preg_replace('/([^a-z0-9_-])+/i', '', $application);
+               $applicationName = preg_replace('/([^a-z0-9_-])+/i', '', $applicationName);
 
                // Construct FQPN (Full-Qualified Path Name) for ApplicationHelper class
                $applicationPath = sprintf(
                        '%s%s%s',
                        self::getConfigurationInstance()->getConfigEntry('application_base_path'),
-                       $application,
+                       $applicationName,
                        DIRECTORY_SEPARATOR
                );
 
@@ -655,12 +669,12 @@ final class FrameworkBootstrap {
                // Is the path there? This secures a bit the parameter (from untrusted source).
                if ((!is_dir($applicationPath)) || (!is_readable($applicationPath))) {
                        // Not found or not readable
-                       ApplicationEntryPoint::exitApplication(sprintf('Application "%s" not found.', $application));
-               } // END - if
+                       ApplicationEntryPoint::exitApplication(sprintf('Application "%s" not found.', $applicationName));
+               }
 
                // Set the detected application's name and full path for later usage
-               self::getConfigurationInstance()->setConfigEntry('detected_full_app_path', $applicationPath);
-               self::getConfigurationInstance()->setConfigEntry('detected_app_name'     , $application);
+               self::$detectedApplicationPath = $applicationPath;
+               self::$detectedApplicationName = $applicationName;
        }
 
        /**