]> git.mxchange.org Git - shipsimu.git/blobdiff - inc/classes/main/class_BaseFrameworkSystem.php
More patterns added, part description changed to object description
[shipsimu.git] / inc / classes / main / class_BaseFrameworkSystem.php
index 3721f6399b07bfb27dfcae42b3f7842298c1d73f..dbf69b14f3a7f50d3657fee3f306596409f8db5a 100644 (file)
@@ -4,9 +4,10 @@
  * class handles saving of games etc.
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
- * @version            0.0
+ * @version            0.3.0
  * @copyright  Copyright(c) 2007, 2008 Roland Haeder, this is free software
  * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.mxchange.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
@@ -60,7 +61,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
        /**
         * A human-readable description for this simulator part
         */
-       private $partDescr      = "Namenlose Framework-Einheit";
+       private $objectDescription      = "Namenlose Framework-Einheit";
 
        /**
         * The unique ID string for identifying all type of classes
@@ -77,6 +78,16 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
         */
        private $decimals  = ","; // German
 
+       /**
+        * The language instance for the template loader
+        */
+       private $langInstance = null;
+
+       /**
+        * The file I/O instance for the template loader
+        */
+       private $fileIOInstance = null;
+
        /***********************
         * Exception codes.... *
         ***********************/
@@ -125,6 +136,10 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
        const EXCEPTION_VARIABLE_NOT_SET             = 0x02a;
        const EXCEPTION_ATTRIBUTES_ARE_MISSING       = 0x02b;
        const EXCEPTION_ARRAY_ELEMENTS_MISSING       = 0x02c;
+       const EXCEPTION_TEMPLATE_ENGINE_UNSUPPORTED  = 0x02d;
+       const EXCEPTION_MISSING_LANGUAGE_HANDLER     = 0x02e;
+       const EXCEPTION_MISSING_FILE_IO_HANDLER      = 0x02f;
+       const EXCEPTION_MISSING_ELEMENT              = 0x030;
 
        /**
         * In the super constructor these system classes shall be ignored or else
@@ -173,7 +188,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                        }
 
                        // Destroy all informations about this class but keep some text about it alive
-                       $this->setPartDescr(sprintf("Entferntes Objekt <em>%s</em>", $this->__toString()));
+                       $this->setObjectDescription(sprintf("Entferntes Objekt <em>%s</em>", $this->__toString()));
                        $this->setRealClass("DestructedObject");
                        $this->resetUniqueID();
                } elseif ((defined('DEBUG_DESTRUCTOR')) && (is_object($this->getDebugInstance()))) {
@@ -475,7 +490,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                        $this->__toString(),
                        md5(sprintf("%s:%s:%s:%s:%s:%s",
                                $this->__toString(),
-                               $this->getPartDescr(),
+                               $this->getObjectDescription(),
                                time(),
                                getenv('REMOTE_ADDR'),
                                getenv('SERVER_ADDR'),
@@ -534,14 +549,14 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
        /**
         * Getter for simulator description
         *
-        * @return      $partDescr      The description of this simulation part
+        * @return      $objectDescription      The description of this simulation part
         */
-       public final function getPartDescr () {
-               if ((defined('DEBUG_SYSTEM')) && (is_object($this->getDebugInstance()))) $this->getDebugInstance()->output(sprintf("[%s:] getPartDescr erreicht.<br />\n",
+       public final function getObjectDescription () {
+               if ((defined('DEBUG_SYSTEM')) && (is_object($this->getDebugInstance()))) $this->getDebugInstance()->output(sprintf("[%s:] getObjectDescription erreicht.<br />\n",
                        $this->__toString()
                ));
-               if (isset($this->partDescr)) {
-                       return $this->partDescr;
+               if (isset($this->objectDescription)) {
+                       return $this->objectDescription;
                } else {
                        return null;
                }
@@ -550,14 +565,14 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
        /**
         * Setter for simulation part description
         *
-        * @param               $partDescr      The description as string for this simulation part
+        * @param               $objectDescription      The description as string for this simulation part
         * @return      void
         */
-       public final function setPartDescr ($partDescr) {
-               $this->partDescr = (String) $partDescr;
+       public final function setObjectDescription ($objectDescription) {
+               $this->objectDescription = (String) $objectDescription;
                if ((defined('DEBUG_SYSTEM')) && (is_object($this->getDebugInstance()))) $this->getDebugInstance()->output(sprintf("[%s:] Teilbeschreibung wird auf <strong>%s</strong> gesetzt.<br />\n",
                        $this->__toString(),
-                       $this->partDescr
+                       $this->objectDescription
                ));
        }
 
@@ -568,11 +583,6 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
         * @return      boolean The result of comparing both's unique ID
         */
        public final function equals ($object) {
-               if ((defined('DEBUG_SYSTEM')) && (is_object($this->getDebugInstance()))) $this->getDebugInstance()->output(sprintf("[%s:] Ist <strong>%s</strong>=<strong>%s</strong>?<br />\n",
-                       $this->__toString(),
-                       $this->__toString(),
-                       $object->__toString()
-               ));
                return ($this->getUniqueID() == $object->getUniqueID());
        }
 
@@ -585,8 +595,11 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
         */
        public function itemMatches ($itemInstance) {
                return (
-                          ($this->__toString()   == $itemInstance->__toString())
-                       && ($this->getPartDescr() == $itemInstance->getPartDescr())
+                       (
+                               $this->__toString()   == $itemInstance->__toString()
+                       ) && (
+                               $this->getObjectDescription() == $itemInstance->getObjectDescription()
+                       )
                );
        }
 
@@ -724,7 +737,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
         * @throws      InvalidArrayCountException      If the array contains less or
         *                                                                      more than two elements
         */
-       public function getPathFileNameFromObject () {
+       public final function getPathFileNameFromObject () {
                // Get the main object's unique ID. We use this as a path/filename combination
                $pathFile = $this->getUniqueID();
 
@@ -751,7 +764,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
        /**
         * Appends a trailing slash to a string
         *
-        * @param               $str            A string (maybe) without trailing slash
+        * @param       $str            A string (maybe) without trailing slash
         * @return      $str            A string with an auto-appended trailing slash
         */
        public final function addMissingTrailingSlash ($str) {
@@ -759,6 +772,112 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                if (substr($str, -1, 1) != "/") $str .= "/";
                return $str;
        }
+
+       /**
+        * Private getter for language instance
+        *
+        * @return      $langInstance   An instance to the language sub-system
+        */
+       protected final function getLanguageInstance () {
+               return $this->langInstance;
+       }
+
+       /**
+        * Setter for language instance
+        *
+        * @param       $langInstance   An instance to the language sub-system
+        * @return      void
+        * @see         LanguageSystem
+        */
+       public final function setLanguageInstance (ManageableLanguage $langInstance) {
+               $this->langInstance = $langInstance;
+       }
+
+       /**
+        * Private getter for file IO instance
+        *
+        * @return      $fileIOInstance An instance to the file I/O sub-system
+        */
+       protected final function getFileIOInstance () {
+               return $this->fileIOInstance;
+       }
+
+       /**
+        * Setter for file I/O instance
+        *
+        * @param       $fileIOInstance An instance to the file I/O sub-system
+        * @return      void
+        */
+       public final function setFileIOInstance (FileIOHandler $fileIOInstance) {
+               $this->fileIOInstance = $fileIOInstance;
+       }
+
+       /**
+        * Prepare the template engine (TemplateEngine by default) for a given
+        * application helper instance (ApplicationHelper by default).
+        *
+        * @param               $appInstance                    An application helper instance
+        * @return              $tplEngine                              The template engine instance
+        * @throws              NullPointerException    If the template engine could not
+        *                                                                              be initialized
+        * @throws              UnsupportedTemplateEngineException      If $tplEngine is an
+        *                                                                              unsupported template engine
+        * @throws              MissingLanguageHandlerException If the language sub-system
+        *                                                                              is not yet initialized
+        */
+       protected function prepareTemplateEngine (BaseFrameworkSystem $appInstance) {
+               // Generate FQFN for all application templates
+               $fqfn = sprintf("%s%s/%s/%s",
+                       PATH,
+                       $this->getConfigInstance()->readConfig("application_path"),
+                       strtolower($appInstance->getAppShortName()),
+                       $this->getConfigInstance()->readConfig("tpl_base_path")
+               );
+
+               // Are both instances set?
+               if ($appInstance->getLanguageInstance() === null) {
+                       // Invalid language instance
+                       throw new MissingLanguageHandlerException($appInstance, self::EXCEPTION_MISSING_LANGUAGE_HANDLER);
+               } elseif ($appInstance->getFileIOInstance() === null) {
+                       // Invalid language instance
+                       throw new MissingFileIoHandlerException($appInstance, self::EXCEPTION_MISSING_FILE_IO_HANDLER);
+               }
+
+               // Initialize the template engine
+               $tplEngine = null;
+               $eval = sprintf("\$tplEngine = %s::create%s(
+       \"%s\",
+       \$appInstance->getLanguageInstance(),
+       \$appInstance->getFileIOInstance()
+);",
+                       $this->getConfigInstance()->readConfig("tpl_engine"),
+                       $this->getConfigInstance()->readConfig("tpl_engine"),
+                       $fqfn
+               );
+
+               // Debug message
+               if ((!is_null($this->getDebugInstance())) && (defined('DEBUG_EVAL'))) {
+                       $this->getDebugInstance()->output(sprintf("[%s:] Konstruierte PHP-Anweisung: <pre><em>%s</em></pre><br />\n",
+                               $this->__toString(),
+                               htmlentities($eval)
+                       ));
+               }
+
+               // Run the command
+               eval($eval);
+
+               // Is it a valid instance?
+               if (is_null($tplEngine)) {
+                       // No class returned
+                       throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
+               } elseif (!$tplEngine instanceof CompileableTemplate) {
+                       // Not an object! ;-(
+                       throw new UnsupportedTemplateEngineException($tplEngine, self::EXCEPTION_TEMPLATE_ENGINE_UNSUPPORTED);
+               }
+
+               // Return the prepared instance
+               return $tplEngine;
+       }
 }
 
 // [EOF]