More patterns added, part description changed to object description
[shipsimu.git] / inc / classes / main / template / class_TemplateEngine.php
index 74542634f7466235ec2e3b87e8a5547df81c6ce3..44f374fc9efc26fa6611c227e0f010eea7b6c36d 100644 (file)
@@ -71,11 +71,6 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
         */
        private $configVariables = array();
 
-       /**
-        * The language instance which should link to an object of LanguageSystem
-        */
-       private $langInstance = null;
-
        /**
         * Loaded templates for recursive protection and detection
         */
@@ -108,9 +103,15 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
         */
        private $regExpCodeTags = '/\{\?([a-z_]+)(:("[^"]+"|[^?}]+)+)?\?\}/';
 
+       /**
+        * Loaded helpers
+        */
+       private $helpers = array();
+
        // Exception codes for the template engine
        const EXCEPTION_TEMPLATE_TYPE_IS_UNEXPECTED   = 0xa00;
-       const TEMPLATE_CONTAINS_INVALID_VAR_EXCEPTION = 0xa01;
+       const EXCEPTION_TEMPLATE_CONTAINS_INVALID_VAR = 0xa01;
+       const EXCEPTION_INVALID_VIEW_HELPER           = 0xa02;
 
        /**
         * Private constructor
@@ -122,7 +123,7 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
                parent::constructor(__CLASS__);
 
                // Set part description
-               $this->setPartDescr("Template-Engine");
+               $this->setObjectDescription("Template-Engine");
 
                // Create unique ID number
                $this->createUniqueID();
@@ -135,18 +136,18 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
        /**
         * Creates an instance of the class TemplateEngine and prepares it for usage
         *
-        * @param               $basePath               The local base path for all templates
-        * @param               $langInstance   An instance of LanguageSystem (default)
-        * @param               $ioInstance     An instance of FileIOHandler (default, middleware!)
+        * @param       $basePath               The local base path for all templates
+        * @param       $langInstance   An instance of LanguageSystem (default)
+        * @param       $ioInstance             An instance of FileIOHandler (default, middleware!)
         * @return      $tplInstance    An instance of TemplateEngine
         * @throws      BasePathIsEmptyException                If the provided $basePath is empty
         * @throws      InvalidBasePathStringException  If $basePath is no string
         * @throws      BasePathIsNoDirectoryException  If $basePath is no
-        *                                                                              directory or not found
+        *                                                                                      directory or not found
         * @throws      BasePathReadProtectedException  If $basePath is
-        *                                                                              read-protected
+        *                                                                                      read-protected
         */
-       public final static function createTemplateEngine ($basePath, $langInstance, $ioInstance) {
+       public final static function createTemplateEngine ($basePath, ManageableLanguage  $langInstance, FileIOHandler $ioInstance) {
                // Get a new instance
                $tplInstance = new TemplateEngine();
 
@@ -176,7 +177,7 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
 
                // Set the language and IO instances
                $tplInstance->setLanguageInstance($langInstance);
-               $tplInstance->setIOInstance($ioInstance);
+               $tplInstance->setFileIOInstance($ioInstance);
 
                // Set template extensions
                $tplInstance->setRawTemplateExtension($cfgInstance->readConfig("raw_template_extension"));
@@ -265,35 +266,6 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
                $this->varStack = new FrameworkArrayObject();
        }
 
-       /**
-        * Setter for language instance which should be LanguageSystem
-        *
-        * @param               $langInstance           The language instance
-        * @return      void
-        */
-       public final function setLanguageInstance (ManageableLanguage $langInstance) {
-               $this->langInstance = $langInstance;
-       }
-
-       /**
-        * Setter for file I/O instance which should be FileIOHandler
-        *
-        * @param               $ioInstance             The file I/O instance
-        * @return      void
-        */
-       public final function setIOInstance (FileIOHandler $ioInstance) {
-               $this->ioInstance = $ioInstance;
-       }
-
-       /**
-        * Getter for file I/O instance which should be FileIOHandler
-        *
-        * @return      $ioInstance             The file I/O instance
-        */
-       public final function getIOInstance () {
-               return $this->ioInstance;
-       }
-
        /**
         * Setter for base path
         *
@@ -527,7 +499,7 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
                // Construct the FQFN for the template by honoring the current language
                $fqfn = sprintf("%s%s/%s/%s%s",
                        $this->getBasePath(),
-                       $this->langInstance->getLanguageCode(),
+                       $this->getLanguageInstance()->getLanguageCode(),
                        $this->getTemplateType(),
                        $template,
                        $ext
@@ -556,7 +528,7 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
                ));
 
                // Get a input/output instance from the middleware
-               $ioInstance = $this->getIOInstance();
+               $ioInstance = $this->getFileIOInstance();
 
                // Validate the instance
                if (is_null($ioInstance)) {
@@ -608,11 +580,11 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
        /**
         * Extract variables from a given raw data stream
         *
-        * @param               $rawData                The raw template data we shall analyze
+        * @param       $rawData        The raw template data we shall analyze
         * @return      void
         * @throws      InvalidTemplateVariableNameException    If a variable name
-        *                                                                                      in a template is
-        *                                                                                      invalid
+        *                                                                                                      in a template is
+        *                                                                                                      invalid
         */
        private function extractVariablesFromRawData ($rawData) {
                // Cast to string
@@ -628,7 +600,7 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
                                // Is the variable name valid?
                                if (($variableMatches[1][$key] != $this->getConfigInstance()->readConfig("tpl_valid_var")) && ($variableMatches[1][$key] != "config")) {
                                        // Invalid variable name
-                                       throw new InvalidTemplateVariableNameException(array($this, $this->getLastTemplate(), $variableMatches[1][$key], $this->getConfigInstance()), self::TEMPLATE_CONTAINS_INVALID_VAR_EXCEPTION);
+                                       throw new InvalidTemplateVariableNameException(array($this, $this->getLastTemplate(), $variableMatches[1][$key], $this->getConfigInstance()), self::EXCEPTION_TEMPLATE_CONTAINS_INVALID_VAR);
                                }
 
                                // Try to assign it, empty strings are being ignored
@@ -804,6 +776,7 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
                                $this->assignVariable($var, $varMatches[3][$key]);
                        } else {
                                // Non-string found so we need some deeper analysis...
+                               // @TODO Unfinished work or don't die here.
                                die("Deeper analysis not yet implemented!");
                        }
 
@@ -1034,6 +1007,43 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
                        break;
                }
        }
+
+       /**
+        * Loads a given view helper (by name)
+        *
+        * @param       $helperName     The helper's name
+        * @return      void
+        * @throws      ViewHelperNotFoundException     If the given view helper was not found
+        */
+       protected function loadViewHelper ($helperName) {
+               // Make first character upper case, rest low
+               $helperName = ucfirst($helperName);
+
+               // Is this view helper loaded?
+               if (!isset($this->helpers[$helperName])) {
+                       // Create a class name
+                       $className = "{$helperName}ViewHelper";
+
+                       // Does this class exists?
+                       if (!class_exists($className)) {
+                               // Abort here!
+                               throw new ViewHelperNotFoundException(array($this, $helperName), self::EXCEPTION_INVALID_VIEW_HELPER);
+                       }
+
+                       // Generate new instance
+                       $eval = sprintf("\$this->helpers[%s] = %s::create%s();",
+                               $helperName,
+                               $className,
+                               $className
+                       );
+
+                       // Run the code
+                       @eval($eval);
+               }
+
+               // Return the requested instance
+               return $this->helpers[$helperName];
+       }
 }
 
 // [EOF]