Source for file Engine.class.php

Documentation is available at Engine.class.php

  1. <?php
  2. /**
  3.  * Gumbo Library Framework
  4.  *
  5.  * LICENSE
  6.  * This library is being released under the terms of the New BSD License.  A
  7.  * copy of the license is packaged with the software (LICENSE.txt).  If no
  8.  * copy is found, a copy of the license template can be found at:
  9.  * http://www.opensource.org/licenses/bsd-license.php
  10.  * 
  11.  * @category Gumbo
  12.  * @package Template
  13.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  14.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  15.  * @author Michael Luster <mluster79@yahoo.com>
  16.  * @link http://sourceforge.net/projects/phpgumbo
  17.  * @version 0.0.1
  18.  */
  19.  
  20. /**
  21.  * Abstract Template Engine Class
  22.  * 
  23.  * A Template Engine defines common methods involved in running template systems.
  24.  * There are many different ways to use template (PHP includes, string replacement,
  25.  * Smarty).  A Template system will basically load a file, replacing keywords
  26.  * with values defined inside the program.  Each Engine will define a specific
  27.  * implementation for parsing Template files.
  28.  * 
  29.  * The class provides a basic implementation.  This allows an Engine to only
  30.  * override certain methods for custom implementation.  The common methods allow
  31.  * access to Template variables (and assignment), setting the Template file name
  32.  * (file name or full path), setting the Template directory path (including extra
  33.  * Smarty defined paths), loading global Template settings, and setting the
  34.  * surrounding characters of keywords inside the Template file.
  35.  * 
  36.  * To create a new Template_Engine, simply extend this class, placing the new
  37.  * file into the 'gumbo/template/engine/*' directory.  The name should be
  38.  * unique 'Gumbo_Template_Engine_[Name]'.  The new Engine will be responsible
  39.  * for parsing a particular type of Template file.  Details on how to use the
  40.  * Engine should be defined inside the class.
  41.  * 
  42.  * Template File
  43.  * The Template File can either be a simple file name (combined with the Template
  44.  * Directory) or a full path.  When the Template File is parsed, the 'getFullPath'
  45.  * should be used.
  46.  * <pre>
  47.  * $this->setFile ("my_file.txt");
  48.  * $this->setDirTpl ("/path/to/file/");
  49.  * ...
  50.  * $this->getFullPath (); // outputs '/path/to/file/my_file.txt', if file exists
  51.  * </pre>
  52.  * 
  53.  * Template Variables
  54.  * Template Variables are assigned keywords referencing a value determined by the
  55.  * program code.  The assigned keys are paired with the Template file, replacing
  56.  * the 'keyword' with the value.  Template Variables can be any type, however check
  57.  * the specific Engine on any restrictions defined.
  58.  * <pre>
  59.  * $this->assign ("key", my_value);
  60.  * $this->key = my_value;
  61.  * </pre>
  62.  * 
  63.  * Delimeters
  64.  * This is simply a left and right character string surrounding keywords, helping
  65.  * to separate raw text.  Inside a basic string replacement Engine, curly braces
  66.  * could surround the keyword '{keyword}' (or any other characters).  The parser
  67.  * (if used) should find these replacements.
  68.  * 
  69.  * @category Gumbo
  70.  * @package Template
  71.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  72.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  73.  * @author Michael Luster <mluster79@yahoo.com>
  74.  * @link http://sourceforge.net/projects/phpgumbo
  75.  * @desc Abstract Template Engine Class
  76.  * @version 0.0.1
  77.  */
  78.  
  79. gumbo_load ("Interface_Output");
  80.  
  81. abstract class Gumbo_Template_Engine implements Gumbo_Interface_Output {
  82.     
  83.     /** @var array $_vars assigned tempalte variables as key=>val pair */
  84.     protected $_vars = array ();
  85.     
  86.     /** @var string $_file template file name */
  87.     private $_file;
  88.     
  89.     /** @var string $_dir_tpl template file directory */
  90.     private $_dir_tpl;
  91.     /** @var string $_dir_compile template file compile directory */
  92.     private $_dir_compile;
  93.     /** @var string $_dir_cache template file cache directory */
  94.     private $_dir_cache;
  95.     /** @var string $_dir_config template file config directory */
  96.     private $_dir_config;
  97.     
  98.     /** @var string $_left_del left delimeter */
  99.     private $_left_del;
  100.     /** @var string $_right_del right delimeter */
  101.     private $_right_del;
  102.     
  103.     /** @var bool $_formatted if the template has been formatted into a string */
  104.     private $_formatted;
  105.     /** @var string $_output formatted template string */
  106.     private $_output;
  107.     
  108.     
  109.     
  110.     /** ABSTRACT METHODS **/
  111.     /**
  112.      * Parses the template file into an HTML string
  113.      * @precondition setFormatted (false)
  114.      * @precondition setOutput (null)
  115.      * @postcondition isFormatted ()
  116.      */
  117.     abstract protected function parse ();
  118.     
  119.     
  120.     
  121.     /**
  122.      * Assigns a template variable with a value
  123.      * @param string $key template file reference
  124.      * @param mixed $val primitive type or Gumbo_Interface_Output
  125.      */
  126.      public function __set ($key$val{
  127.          $this->assign ($key$val);
  128.      }
  129.      
  130.      /**
  131.       * Returns the value of a template key reference
  132.       * @param string $key 
  133.       * @return mixed 
  134.       */
  135.      public function __get ($key{
  136.          return $this->getVar ($key);
  137.      }
  138.      
  139.      /**
  140.       * Returns if the template reference key isset
  141.       * @param string $key 
  142.       * @return bool 
  143.       */
  144.      public function __isset ($key{
  145.          return isset ($this->_vars [$key]);
  146.      }
  147.      
  148.      /**
  149.       * Unassigns a template reference key
  150.       * @param string $key 
  151.       */
  152.      public function __unset ($key{
  153.          unset ($this->_vars [$key]);
  154.      }
  155.     
  156.     
  157.     
  158.     /** ACTION METHODS **/
  159.     /**
  160.      * Displays the formatted template file
  161.      */
  162.     public function display ({
  163.         echo $this->fetch ();
  164.     }
  165.     
  166.     /**
  167.      * Returns the formatted template file string
  168.      * @precondition exists()
  169.      * @param bool $reload parses the template file again
  170.      * @return string 
  171.      * @throws Gumbo_Exception
  172.      */
  173.     public function fetch ($reload=false{
  174.         try {
  175.             // verify precondition
  176.             try {
  177.                 if (!is_bool ($reload)) {
  178.                     throw new Gumbo_Exception ("Invalid Argument 'reload:bool' => {$reload}:gettype ($reload));
  179.                 }
  180.             catch (Gumbo_Exception $e{
  181.                 $e->setFunction (__METHOD__);
  182.                 gumbo_trigger ($e);
  183.                 $reload false;
  184.             }
  185.             if (!$this->exists ()) {
  186.                 throw new Gumbo_Exception ("File Not Found: {$this->getFullPath ()}");
  187.             }
  188.             
  189.             // check if the template has been formatted
  190.             if (!$this->isFormatted (|| $reload{
  191.                 $this->parse ();
  192.             }
  193.             
  194.             return $this->getHtml ();
  195.         catch (Gumbo_Exception $e{
  196.             $e->setFunction (__METHOD__);
  197.             gumbo_trigger ($e);
  198.         }
  199.         return null;
  200.     }
  201.     
  202.     /**
  203.      * Assigns a template variable with a value
  204.      * @param string|array$key template file reference
  205.      * @param mixed $val 
  206.      * @throws Gumbo_Exception
  207.      */
  208.     public function assign ($key$val=null{
  209.         try {
  210.             // verify precondition
  211.             if (!is_string ($key&& !is_array ($key)) {
  212.                 throw new Gumbo_Exception ("Invalid Argument 'key:str|arr' => {$key}:gettype ($key));
  213.             }
  214.             
  215.             // check for an array
  216.             if (is_array ($key)) {
  217.                 foreach ($key as $ref=>$value{
  218.                     $this->assign ($ref$value);
  219.                 }
  220.                 return;
  221.             }
  222.             
  223.             // default assignment
  224.             $this->_vars [$key$val;
  225.         catch (Gumbo_Exception $e{
  226.             $e->setFunction (__METHOD__);
  227.             gumbo_trigger ($e);
  228.         }
  229.     }
  230.     
  231.     /**
  232.      * Unassigns a template variable
  233.      * @param string $key 
  234.      * @throws Gumbo_Exception
  235.      */
  236.     public function unassign ($key{
  237.         try {
  238.             // verify precondition
  239.             if (!is_string ($key)) {
  240.                 throw new Gumbo_Exception ("Invalid Argument 'key:str' => {$key}:gettype ($key));
  241.             }
  242.             if (!isset ($this->_vars [$key])) {
  243.                 throw new Gumbo_Exception ("Undefined Key: {$key}");
  244.             }
  245.             
  246.             unset ($this->_vars [$key]);
  247.         catch (Gumbo_Exception $e{
  248.             $e->setFunction (__METHOD__);
  249.             gumbo_trigger ($e);
  250.         }
  251.     }
  252.     
  253.     /**
  254.      * Resets the template variables (useful for reloading a List)
  255.      * @postcondition !getVars()
  256.      * @postcondition !isFormatted()
  257.      */
  258.     public function reset ({
  259.         $this->_vars = array ();
  260.         $this->setFormatted (false);
  261.     }
  262.     
  263.     /**
  264.      * Loads the global template variables (does not load global Template variables)
  265.      * @param bool $assign_global_vars also assigns global variables
  266.      * @throws Gumbo_Exception
  267.      * @uses Gumbo_Template
  268.      */
  269.     public function loadGlobals ($assign_global_vars=false{
  270.         gumbo_load ("Template");
  271.         
  272.         try {
  273.             // verify precondition
  274.             if (!is_bool ($assign_global_vars)) {
  275.                 throw new Gumbo_Exception ("Invalid Argument 'assign_global_vars:bool' => {$assign_global_vars}:gettype ($assign_global_vars));
  276.             }
  277.         catch (Gumbo_Exception $e{
  278.             $e->setFunction (__METHOD__);
  279.             gumbo_trigger ($e);
  280.             $assign_global_vars false;
  281.         }
  282.         
  283.         $this->setDirTpl (Gumbo_Template::getDirTpl ());
  284.         $this->setDirCompile (Gumbo_Template::getDirCompile ());
  285.         $this->setDirCache (Gumbo_Template::getDirCache ());
  286.         $this->setDirConfig (Gumbo_Template::getDirConfig ());
  287.         
  288.         $this->setLeftDelimeter (Gumbo_Template::getLeftDelimeter ());
  289.         $this->setRightDelimeter (Gumbo_Template::getRightDelimeter ());
  290.         
  291.         if ($assign_global_vars{
  292.             $this->assign (Gumbo_Template::getVars ());
  293.         }
  294.     }
  295.     
  296.     
  297.     
  298.     /** MUTATOR METHODS **/
  299.     /**
  300.      * Sets the template file name
  301.      * @param string $file 
  302.      */
  303.     public function setFile ($file{
  304.         try {
  305.             // verify precondition
  306.             if (!is_string ($file)) {
  307.                 throw new Gumbo_Exception ("Invalid Argument 'file:str' => {$file}:gettype ($file));
  308.             }
  309.             
  310.             $this->_file = $file;
  311.         catch (Gumbo_Exception $e{
  312.             $e->setFunction (__METHOD__);
  313.             trigger ($e);
  314.         }
  315.     }
  316.     
  317.     /**
  318.      * Sets the template file directory
  319.      * @precondition is_dir ($loc)
  320.      * @param string $loc 
  321.      * @throws Gumbo_Exception
  322.      */
  323.     public function setDirTpl ($loc{
  324.         try {
  325.             // verify precondition
  326.             if (!is_string ($loc)) {
  327.                 throw new Gumbo_Exception ("Invalid Argument 'loc:str' => {$loc}:gettype ($loc));
  328.             }
  329.             if (!is_dir ($loc)) {
  330.                 throw new Gumbo_Exception ("Directory Not Found: {$loc}");
  331.             }
  332.             
  333.             $this->_dir_tpl = $loc;
  334.         catch (Gumbo_Exception $e{
  335.             $e->setFunction (__METHOD__);
  336.             gumbo_trigger ($e);
  337.         }
  338.     }
  339.     
  340.     /**
  341.      * Sets the template file compile directory
  342.      * @precondition is_dir ($loc)
  343.      * @param string $loc 
  344.      * @throws Gumbo_Exception
  345.      */
  346.     public function setDirCompile ($loc{
  347.         if (is_null ($loc)) return}
  348.         try {
  349.             // verify precondition
  350.             if (!is_string ($loc)) {
  351.                 throw new Gumbo_Exception ("Invalid Argument 'loc:str' => {$loc}:gettype ($loc));
  352.             }
  353.             if (!is_dir ($loc)) {
  354.                 throw new Gumbo_Exception ("Directory Not Found: {$loc}");
  355.             }
  356.             
  357.             $this->_dir_compile = $loc;
  358.         catch (Gumbo_Exception $e{
  359.             $e->setFunction (__METHOD__);
  360.             gumbo_trigger ($e);
  361.         }
  362.     }
  363.     
  364.     /**
  365.      * Sets the template file cache directory
  366.      * @precondition is_dir ($loc)
  367.      * @param string $loc 
  368.      * @throws Gumbo_Exception
  369.      */
  370.     public function setDirCache ($loc{
  371.         if (is_null ($loc)) return}
  372.         try {
  373.             // verify precondition
  374.             if (!is_string ($loc)) {
  375.                 throw new Gumbo_Exception ("Invalid Argument 'loc:str' => {$loc}:gettype ($loc));
  376.             }
  377.             if (!is_dir ($loc)) {
  378.                 throw new Gumbo_Exception ("Directory Not Found: {$loc}");
  379.             }
  380.             
  381.             $this->_dir_cache = $loc;
  382.         catch (Gumbo_Exception $e{
  383.             $e->setFunction (__METHOD__);
  384.             gumbo_trigger ($e);
  385.         }
  386.     }
  387.     
  388.     /**
  389.      * Sets the template file config directory
  390.      * @precondition is_dir ($loc)
  391.      * @param string $loc 
  392.      * @throws Gumbo_Exception
  393.      */
  394.     public function setDirConfig ($loc{
  395.         if (is_null ($loc)) return}
  396.         try {
  397.             // verify precondition
  398.             if (!is_string ($loc)) {
  399.                 throw new Gumbo_Exception ("Invalid Argument 'loc:str' => {$loc}:gettype ($loc));
  400.             }
  401.             if (!is_dir ($loc)) {
  402.                 throw new Gumbo_Exception ("Directory Not Found: {$loc}");
  403.             }
  404.             
  405.             $this->_dir_config = $loc;
  406.         catch (Gumbo_Exception $e{
  407.             $e->setFunction (__METHOD__);
  408.             gumbo_trigger ($e);
  409.         }
  410.     }
  411.     
  412.     /**
  413.      * Sets the left delimeter value
  414.      * @param string $val 
  415.      * @throws Gumbo_Exception
  416.      */
  417.     public function setLeftDelimeter ($val{
  418.         if (is_null ($val)) return}
  419.         try {
  420.             // verify precondition
  421.             if (!is_string ($val)) {
  422.                 throw new Gumbo_Exception ("Invalid Argument 'val:str' => {$val}:gettype ($val));
  423.             }
  424.             
  425.             $this->_left_del = $val;
  426.         catch (Gumbo_Exception $e{
  427.             $e->setFunction (__METHOD__);
  428.             gumbo_trigger ($e);
  429.         }
  430.     }
  431.     
  432.     /**
  433.      * Sets the right delimeter value
  434.      * @param string $val 
  435.      * @throws Gumbo_Exception
  436.      */
  437.     public function setRightDelimeter ($val{
  438.         if (is_null ($val)) return}
  439.         try {
  440.             // verify precondition
  441.             if (!is_string ($val)) {
  442.                 throw new Gumbo_Exception ("Invalid Argument 'val:str' => {$val}:gettype ($val));
  443.             }
  444.             
  445.             $this->_right_del = $val;
  446.         catch (Gumbo_Exception $e{
  447.             $e->setFunction (__METHOD__);
  448.             gumbo_trigger ($e);
  449.         }
  450.     }
  451.     
  452.     /**
  453.      * Sets the template file as formatted
  454.      * @param bool $val 
  455.      * @throws Gumbo_Exception
  456.      */
  457.     protected function setFormatted ($val{
  458.         try {
  459.             // verify precondition
  460.             if (!is_bool ($val)) {
  461.                 throw new Gumbo_Exception ("Invalid Argument 'val:str' => {$val}:gettype ($val));
  462.             }
  463.             
  464.             $this->_formatted = $val;
  465.         catch (Gumbo_Exception $e{
  466.             $e->setFunction (__METHOD__);
  467.             gumbo_trigger ($e);
  468.         }
  469.     }
  470.     
  471.     /**
  472.      * Sets the formatted output template string
  473.      * @param string $output 
  474.      * @throws Gumbo_Exception
  475.      */
  476.     protected function setOutput ($output{
  477.         try {
  478.             // verify precondition
  479.             if (!is_string ($output&& !is_null ($output)) {
  480.                 throw new Gumbo_Exception ("Invalid Argument 'output:str|null' => {$output}:gettype ($output));
  481.             }
  482.             
  483.             $this->_output = $output;
  484.         catch (Gumbo_Exception $e{
  485.             $e->setFunction (__METHOD__);
  486.             gumbo_trigger ($e);
  487.         }
  488.     }
  489.     
  490.     
  491.     
  492.     /** ACCESSOR METHODS **/
  493.     /**
  494.      * Returns the full path to the Template file
  495.      * @return string 
  496.      */
  497.     public function getFullPath ({
  498.         if (file_exists ($this->getDirTpl ($this->getFile ())) {
  499.             return $this->getDirTpl ($this->getFile ();
  500.         }
  501.         return $this->getFile ();
  502.     }
  503.     
  504.     /**
  505.      * Returns the template file name
  506.      * @return string 
  507.      */
  508.     public function getFile ({
  509.         return $this->_file;
  510.     }
  511.     
  512.     /**
  513.      * Returns the template file directory
  514.      * @return string 
  515.      */
  516.     public function getDirTpl ({
  517.         return $this->_dir_tpl;
  518.     }
  519.     
  520.     /**
  521.      * Returns the template file compile directory
  522.      * @return string 
  523.      */
  524.     public function getDirCompile ({
  525.         return $this->_dir_compile;
  526.     }
  527.     
  528.     /**
  529.      * Returns the template file cache directory
  530.      * @return string 
  531.      */
  532.     public function getDirCache ({
  533.         return $this->_dir_cache;
  534.     }
  535.     
  536.     /**
  537.      * Returns the template file config directory
  538.      * @return string 
  539.      */
  540.     public function getDirConfig ({
  541.         return $this->_dir_config;
  542.     }
  543.     
  544.     /**
  545.      * Returns the value of the key reference
  546.      * @param string $key reference key
  547.      * @return mixed 
  548.      * @throws Gumbo_Exception
  549.      */
  550.     public function getVar ($key{
  551.         try {
  552.             // verify precondition
  553.             if (!is_string ($key)) {
  554.                 throw new Gumbo_Exception ("Invalid Argument 'key:str' => {$key}:gettype ($key));
  555.             }
  556.             if (!isset ($this->_vars [$key])) {
  557.                 throw new Gumbo_Exception ("Undefined Key: {$key}");
  558.             }
  559.             
  560.             return $this->_vars [$key];
  561.         catch (Gumbo_Exception $e{
  562.             $e->setFunction (__METHOD__);
  563.             gumbo_trigger ($e);
  564.         }
  565.     }
  566.     
  567.     /**
  568.      * Returns all the template variables
  569.      * @return array 
  570.      */
  571.     public function getVars ({
  572.         return $this->_vars;
  573.     }
  574.     
  575.     /**
  576.      * Returns the left delimeter
  577.      * @return string 
  578.      */
  579.     public function getLeftDelimeter ({
  580.         return $this->_left_del;
  581.     }
  582.     
  583.     /**
  584.      * Returns the right delimeter
  585.      * @return string 
  586.      */
  587.     public function getRightDelimeter ({
  588.         return $this->_right_del;
  589.     }
  590.     
  591.     /**
  592.      * Returns if the template has been formatted
  593.      * @return bool 
  594.      */
  595.     protected function isFormatted ({
  596.         return $this->_formatted;
  597.     }
  598.     
  599.     /**
  600.      * Returns the formatted output template string
  601.      * @return string 
  602.      */
  603.     protected function getOutput ({
  604.         return $this->_output;
  605.     }
  606.     
  607.     
  608.     
  609.     /**
  610.      * Returns if the template file exists
  611.      * @return bool 
  612.      */
  613.     public function exists ({
  614.         if (!$this->getFile ()) return false}
  615.         if (!$this->getDirTpl ()) return false}
  616.         
  617.         return file_exists ($this->getFullPath ());
  618.     }
  619.     
  620. }
  621.  
  622. ?>