]> git.mxchange.org Git - hub.git/commitdiff
support for view helpers added
authorRoland Häder <roland@mxchange.org>
Thu, 13 Mar 2008 18:55:47 +0000 (18:55 +0000)
committerRoland Häder <roland@mxchange.org>
Thu, 13 Mar 2008 18:55:47 +0000 (18:55 +0000)
.gitattributes
inc/classes/exceptions/template/class_InvalidBasePathStringException.php
inc/classes/exceptions/template/class_ViewHelperNotFoundException.php [new file with mode: 0644]
inc/classes/interfaces/template/view/class_ViewHelper.php [new file with mode: 0644]
inc/classes/main/template/class_TemplateEngine.php

index 5f5e3c6253c65eb26f03a381b6425e565cb270ad..eea91999ac71a93f8c44014950dce12a341c3219 100644 (file)
@@ -108,6 +108,7 @@ inc/classes/exceptions/template/class_InvalidBasePathStringException.php -text
 inc/classes/exceptions/template/class_InvalidTemplateVariableNameException.php -text
 inc/classes/exceptions/template/class_UnexpectedTemplateTypeException.php -text
 inc/classes/exceptions/template/class_UnsupportedTemplateEngineException.php -text
+inc/classes/exceptions/template/class_ViewHelperNotFoundException.php -text
 inc/classes/interfaces/.htaccess -text
 inc/classes/interfaces/application/.htaccess -text
 inc/classes/interfaces/application/class_ManageableApplication.php -text
@@ -135,6 +136,7 @@ inc/classes/interfaces/language/.htaccess -text
 inc/classes/interfaces/language/class_ManageableLanguage.php -text
 inc/classes/interfaces/template/.htaccess -text
 inc/classes/interfaces/template/class_CompileableTemplate.php -text
+inc/classes/interfaces/template/view/class_ViewHelper.php -text
 inc/classes/main/.htaccess -text
 inc/classes/main/class_BaseFrameworkSystem.php -text
 inc/classes/main/class_FrameworkArrayObject.php -text
index af7ff4d78dd3827dcc9fc06aa26e25c0e9776da3..729aca9a4cbdfb01b5a85930cda0b6f60b464134 100644 (file)
@@ -25,8 +25,8 @@ class InvalidBasePathStringException extends FrameworkException {
        /**
         * The constructor
         *
-        * @param               $class          An array holding our informations
-        * @param               $code           Code number for the exception
+        * @param       $class  An array holding our informations
+        * @param       $code   Code number for the exception
         * @return      void
         */
        public function __construct (BaseFrameworkSystem $class, $code) {
diff --git a/inc/classes/exceptions/template/class_ViewHelperNotFoundException.php b/inc/classes/exceptions/template/class_ViewHelperNotFoundException.php
new file mode 100644 (file)
index 0000000..78cb673
--- /dev/null
@@ -0,0 +1,45 @@
+<?php
+/**
+ * An exception thrown when a given view helper was not found
+ *
+ * @author             Roland Haeder <webmaster@mxchange.org>
+ * @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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+class ViewHelperNotFoundException extends FrameworkException {
+       /**
+        * The constructor
+        *
+        * @param               $msgArray       An array holding our informations
+        * @param               $code           Code number for the exception
+        * @return      void
+        */
+       public function __construct (array $msgArray, $code) {
+               // Add a message around the missing class
+               $message = sprintf("[%s:] View-Helper <u>%s</u> ist ung&uuml;ltig.",
+                       $msgArray[0]->__toString(),
+                       $msgArray[1]
+               );
+
+               // Call parent constructor
+               parent::__construct($message, $code);
+       }
+}
+
+// [EOF]
+?>
diff --git a/inc/classes/interfaces/template/view/class_ViewHelper.php b/inc/classes/interfaces/template/view/class_ViewHelper.php
new file mode 100644 (file)
index 0000000..1de45ff
--- /dev/null
@@ -0,0 +1,35 @@
+<?php
+/**
+ * An interface for view helpers
+ *
+ * @author             Roland Haeder <webmaster@mxchange.org>
+ * @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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+interface ViewHelper extends FrameworkInterface {
+       /**
+        * The execute method for executing the view helper
+        *
+        * @param       $args   Arguments to send to the view helper
+        * @return      mixed   Unknown return arguments, or void
+        */
+       function execute (array $args = null);
+}
+
+// [EOF]
+?>
index 74542634f7466235ec2e3b87e8a5547df81c6ce3..2fff655b7c4b7a98362a024775e769953925f320 100644 (file)
@@ -108,9 +108,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
@@ -628,7 +634,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
@@ -1034,6 +1040,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]