Fixed type-hint (still no interface).
[core.git] / inc / classes / main / class_BaseFrameworkSystem.php
index 34b0a22e6a716924f51b6e622b18345175f1e499..b13875fa7a71d1df7aca2103db2ada88d889d346 100644 (file)
@@ -3,11 +3,11 @@
  * The simulator system class is the super class of all other classes. This
  * class handles saving of games etc.
  *
- * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2013 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
- * @link               http://www.ship-simu.org
+ * @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
@@ -173,6 +173,11 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
         */
        private $wrapperInstance = NULL;
 
+       /**
+        * An instance of a file I/O pointer class (not handler)
+        */
+       private $pointerInstance = NULL;
+
        /**
         * Thousands separator
         */
@@ -249,6 +254,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
        const EXCEPTION_ARRAY_ELEMENTS_MISSING       = 0x02c;
        const EXCEPTION_TEMPLATE_ENGINE_UNSUPPORTED  = 0x02d;
        const EXCEPTION_UNSPPORTED_OPERATION         = 0x02e;
+       const EXCEPTION_FACTORY_REQUIRE_PARAMETER    = 0x02f;
        const EXCEPTION_MISSING_ELEMENT              = 0x030;
        const EXCEPTION_HEADERS_ALREADY_SENT         = 0x031;
        const EXCEPTION_DEFAULT_CONTROLLER_GONE      = 0x032;
@@ -259,7 +265,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
        const EXCEPTION_ASSERTION_FAILED             = 0x037;
        const EXCEPTION_FILE_CANNOT_BE_READ          = 0x038;
        const EXCEPTION_DATABASE_UPDATED_NOT_ALLOWED = 0x039;
-       const EXCEPTION_FILTER_CHAIN_INTERCEPTED     = 0x040;
+       const EXCEPTION_FILTER_CHAIN_INTERCEPTED     = 0x03a;
 
        /**
         * Hexadecimal->Decimal translation array
@@ -326,10 +332,10 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                        $this->setConfigInstance(FrameworkConfiguration::getSelfInstance());
                } // END - if
 
-               // Is the startup time set? (0 cannot be true anymore)
+               // Is the startup time set? (0 cannot be TRUE anymore)
                if (self::$startupTime == 0) {
                        // Then set it
-                       self::$startupTime = microtime(true);
+                       self::$startupTime = microtime(TRUE);
                } // END - if
        }
 
@@ -355,6 +361,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                } else {
                        // Do not call this twice
                        trigger_error(__METHOD__ . ': Called twice.');
+                       exit;
                }
        }
 
@@ -404,11 +411,11 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                                        // .. or size if array
                                        $argsString .= ', ' . count($arg);
                                } elseif ($arg === TRUE) {
-                                       // ... is boolean 'true'
-                                       $argsString .= ', true';
+                                       // ... is boolean 'TRUE'
+                                       $argsString .= ', TRUE';
                                } elseif ($arg === FALSE) {
-                                       // ... is boolean 'true'
-                                       $argsString .= ', false';
+                                       // ... is boolean 'FALSE'
+                                       $argsString .= ', FALSE';
                                }
 
                                // Closing bracket
@@ -1216,6 +1223,25 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                return $this->iteratorInstance;
        }
 
+       /**
+        * Setter for FrameworkFileInputOutputPointer instance
+        *
+        * @param       $pointerInstance        An instance of an FrameworkFileInputOutputPointer
+        * @return      void
+        */
+       protected final function setPointerInstance (FrameworkFileInputOutputPointer $pointerInstance) {
+               $this->pointerInstance = $pointerInstance;
+       }
+
+       /**
+        * Getter for FrameworkFileInputOutputPointer instance
+        *
+        * @return      $pointerInstance        An instance of an FrameworkFileInputOutputPointer
+        */
+       public final function getPointerInstance () {
+               return $this->pointerInstance;
+       }
+
        /**
         * Checks whether an object equals this object. You should overwrite this
         * method to implement own equality checks
@@ -1344,7 +1370,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                $content .= sprintf('<pre>%s</pre>',
                        trim(
                                htmlentities(
-                                       print_r($this, true)
+                                       print_r($this, TRUE)
                                )
                        )
                );
@@ -1410,6 +1436,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                } else {
                        // Trigger an error
                        trigger_error($stubMessage);
+                       exit;
                }
        }
 
@@ -1417,7 +1444,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
         * Outputs a debug backtrace and stops further script execution
         *
         * @param       $message        An optional message to output
-        * @param       $doExit         Whether exit the program (true is default)
+        * @param       $doExit         Whether exit the program (TRUE is default)
         * @return      void
         */
        public function debugBackTrace ($message = '', $doExit = TRUE) {
@@ -1444,20 +1471,29 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
         * @return      $debugInstance  An instance of a debugger class
         */
        public final static function createDebugInstance ($className) {
-               // Init debug instance
-               $debugInstance = NULL;
+               // Is the instance set?
+               if (!Registry::getRegistry()->instanceExists('debug')) {
+                       // Init debug instance
+                       $debugInstance = NULL;
+
+                       // Try it
+                       try {
+                               // Get a debugger instance
+                               $debugInstance = DebugMiddleware::createDebugMiddleware(FrameworkConfiguration::getSelfInstance()->getConfigEntry('debug_class'));
+                       } catch (NullPointerException $e) {
+                               // Didn't work, no instance there
+                               exit('Cannot create debugInstance! Exception=' . $e->__toString() . ', message=' . $e->getMessage());
+                       }
 
-               // Try it
-               try {
-                       // Get a debugger instance
-                       $debugInstance = DebugMiddleware::createDebugMiddleware(FrameworkConfiguration::getSelfInstance()->getConfigEntry('debug_class'));
-               } catch (NullPointerException $e) {
-                       // Didn't work, no instance there
-                       exit('Cannot create debugInstance! Exception=' . $e->__toString() . ', message=' . $e->getMessage());
-               }
+                       // Empty string should be ignored and used for testing the middleware
+                       DebugMiddleware::getSelfInstance()->output('');
 
-               // Empty string should be ignored and used for testing the middleware
-               DebugMiddleware::getSelfInstance()->output('');
+                       // Set it in its own class. This will set it in the registry
+                       $debugInstance->setDebugInstance($debugInstance);
+               } else {
+                       // Get instance from registry
+                       $debugInstance = Registry::getRegistry()->getDebugInstance();
+               }
 
                // Return it
                return $debugInstance;
@@ -1481,7 +1517,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
         *
         * @param       $message        Message we shall send out...
         * @param       $doPrint        Whether print or die here (default: print)
-        * @paran       $stripTags      Whether to strip tags (default: false)
+        * @paran       $stripTags      Whether to strip tags (default: FALSE)
         * @return      void
         */
        public function debugOutput ($message, $doPrint = TRUE, $stripTags = FALSE) {
@@ -1718,7 +1754,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
 
                // Get current array
                $fieldArray = $resultInstance->current();
-               //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($fieldName.':<pre>'.print_r($fieldArray, true).'</pre>');
+               //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($fieldName.':<pre>'.print_r($fieldArray, TRUE).'</pre>');
 
                // Convert dashes to underscore
                $fieldName = $this->convertDashesToUnderscores($fieldName);
@@ -1775,6 +1811,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                        } else {
                                // Trigger an error
                                trigger_error($message . "<br />\n");
+                               exit;
                        }
                } else {
                        // @TODO Finish this part!
@@ -1804,7 +1841,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
         */
        public function getMilliTime () {
                // Get the time of day as float
-               $milliTime = gettimeofday(true);
+               $milliTime = gettimeofday(TRUE);
 
                // Return it
                return $milliTime;
@@ -1983,7 +2020,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
         */
        protected function isBase64Encoded ($encodedData) {
                // Determine it
-               $isBase64 = (@base64_decode($encodedData, true) !== FALSE);
+               $isBase64 = (@base64_decode($encodedData, TRUE) !== FALSE);
 
                // Return it
                return $isBase64;
@@ -2017,7 +2054,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
         */
        protected function getCacheKeyByCriteria (Criteria $criteriaInstance, array $onlyKeys = array()) {
                // Generate it
-               $cacheKey = sprintf("%s@%s",
+               $cacheKey = sprintf('%s@%s',
                        $this->__toString(),
                        $criteriaInstance->getCacheKey($onlyKeys)
                );
@@ -2043,7 +2080,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
         */
        protected function getPrintableExecutionTime () {
                // Caculate the execution time
-               $executionTime = microtime(true) - $this->getStartupTime();
+               $executionTime = microtime(TRUE) - $this->getStartupTime();
 
                // Pack it in nice braces
                $executionTime = sprintf('[ %01.5f ] ', $executionTime);
@@ -2198,6 +2235,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                if (!$this->isGenericArrayGroupSet($keyGroup, $subGroup)) {
                        // No, then abort here
                        trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ' not found.');
+                       exit;
                } // END - if
 
                // Debug message
@@ -2304,6 +2342,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                if (($forceInit === FALSE) && ($this->isGenericArrayGroupSet($keyGroup, $subGroup))) {
                        // Already initialized
                        trigger_error(__METHOD__ . ':keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ' already initialized.');
+                       exit;
                } // END - if
 
                // Initialize it
@@ -2327,6 +2366,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                if (($forceInit === FALSE) && ($this->isGenericArrayKeySet($keyGroup, $subGroup, $key))) {
                        // Already initialized
                        trigger_error(__METHOD__ . ':keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ' already initialized.');
+                       exit;
                } // END - if
 
                // Initialize it
@@ -2351,6 +2391,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                if (($forceInit === FALSE) && ($this->isGenericArrayElementSet($keyGroup, $subGroup, $key, $element))) {
                        // Already initialized
                        trigger_error(__METHOD__ . ':keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',element=' . $element . ' already initialized.');
+                       exit;
                } // END - if
 
                // Initialize it
@@ -2430,6 +2471,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                if (!$this->isGenericArrayKeySet($keyGroup, $subGroup, $key)) {
                        // Not found
                        trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ' not found.');
+                       exit;
                } // END - if
 
                // Then "pop" it
@@ -2457,6 +2499,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                if (!$this->isGenericArrayKeySet($keyGroup, $subGroup, $key)) {
                        // Not found
                        trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ' not found.');
+                       exit;
                } // END - if
 
                // Then "shift" it
@@ -2482,6 +2525,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                if (!isset($this->genericArray[$keyGroup])) {
                        // Abort here
                        trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ' not found.');
+                       exit;
                } // END - if
 
                // Then count it
@@ -2509,6 +2553,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                if (!$this->isGenericArrayGroupSet($keyGroup, $subGroup)) {
                        // Abort here
                        trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ' not found.');
+                       exit;
                } // END - if
 
                // Then count it
@@ -2537,9 +2582,11 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                if (!$this->isGenericArrayKeySet($keyGroup, $subGroup, $key)) {
                        // Abort here
                        trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ' not found.');
+                       exit;
                } elseif (!$this->isValidGenericArrayGroup($keyGroup, $subGroup)) {
                        // Not valid
                        trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ' is not an array.');
+                       exit;
                }
 
                // Then count it
@@ -2566,6 +2613,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                if (!isset($this->genericArray[$keyGroup])) {
                        // Then abort here
                        trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ' does not exist.');
+                       exit;
                } // END - if
 
                // Return it
@@ -2605,6 +2653,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                if (!$this->isGenericArrayKeySet($keyGroup, $subGroup, $key)) {
                        // Then abort here
                        trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ' does not exist.');
+                       exit;
                } // END - if
 
                // Return it
@@ -2646,6 +2695,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                if (!$this->isGenericArrayElementSet($keyGroup, $subGroup, $key, $element)) {
                        // Then abort here
                        trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',element=' . $element . ' does not exist.');
+                       exit;
                } // END - if
 
                // Return it
@@ -2688,6 +2738,23 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                // Return it
                return $isValid;
        }
+
+       /**
+        * Translates boolean TRUE to 'Y' and FALSE to 'N'
+        *
+        * @param       $boolean                Boolean value
+        * @return      $translated             Translated boolean value
+        */
+       public static final function translateBooleanToYesNo ($boolean) {
+               // Make sure it is really boolean
+               assert(is_bool($boolean));
+
+               // "Translate" it
+               $translated = ($boolean === TRUE) ? 'Y' : 'N';
+
+               // ... and return it
+               return $translated;
+       }
 }
 
 // [EOF]