[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/macro/src/ -> lmbMacroCodeWriter.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  
  10  /**

  11   * class lmbMacroCodeWriter.

  12   *

  13   * @package macro

  14   * @version $Id$

  15   */
  16  class lmbMacroCodeWriter
  17  {
  18    const MODE_PHP = 1;
  19    const MODE_HTML = 2;
  20  
  21    protected $current_mode = self :: MODE_HTML;
  22  
  23    protected $code = '';
  24  
  25    protected $function_prefix = '';
  26    protected $function_suffix = 1;
  27  
  28    protected $include_list = array();
  29  
  30    protected $tempVarName = 1;
  31  
  32    function reset()
  33    {
  34      $this->code = '';
  35      $this->current_mode = self :: MODE_HTML;
  36      $this->include_list = array();
  37    }
  38  
  39    protected function switchToPHP()
  40    {
  41      if($this->current_mode == self :: MODE_HTML)
  42      {
  43        $this->current_mode = self :: MODE_PHP;
  44        $this->code .= '<?php ';
  45      }
  46    }
  47  
  48    protected function switchToHTML($context = null)
  49    {
  50      if($this->current_mode == self :: MODE_PHP)
  51      {
  52        $this->current_mode = self :: MODE_HTML;
  53        if($context === "\n")
  54          $this->code .= " ?>\n";
  55        else
  56          $this->code .= ' ?>';
  57      }
  58    }
  59  
  60    function writePHP($text)
  61    {
  62      $this->switchToPHP();
  63      $this->code .= $text;
  64    }
  65  
  66    function writePHPLiteral($text, $escape_text = true)
  67    {
  68      $this->switchToPHP();
  69  
  70      if($escape_text)
  71        $this->code .= "'" . $this->escapeLiteral($text) . "'";
  72      else
  73        $this->code .= "'" . $text . "'";
  74    }
  75  
  76    function escapeLiteral($text)
  77    {
  78      $text = str_replace('\'', "\\'", $text);
  79      if(substr($text, -1) == '\\')
  80        $text .= '\\';
  81      return $text;
  82    }
  83  
  84    function writeHTML($text)
  85    {
  86      $this->switchToHTML(substr($text,0,1));
  87      $this->code .= $text;
  88    }
  89    
  90    function writeRaw($text)
  91    {
  92      $this->code .= $text;
  93    }
  94  
  95    function renderCode()
  96    {
  97      $this->switchToHTML();
  98  
  99      $this->_prependIncludeListToCode();
 100  
 101      return $this->code;
 102    }
 103  
 104    function getCode()
 105    {
 106      return $this->code;
 107    }
 108  
 109    function setCode($code)
 110    {
 111      $this->code = $code;
 112    }
 113  
 114    protected function _prependIncludeListToCode()
 115    {
 116      $include_code = '';
 117      foreach($this->include_list as $include_file)
 118        $include_code .= "require_once('$include_file');\n";
 119  
 120      if(!empty($include_code))
 121        $this->code = '<?php ' . $include_code . '?>' . $this->code;
 122    }
 123  
 124    function getMode()
 125    {
 126      return $this->current_mode;
 127    }
 128  
 129    function registerInclude($include_file)
 130    {
 131      if(!in_array($include_file, $this->include_list))
 132        $this->include_list[] = $include_file;    
 133    }
 134  
 135    function getIncludeList()
 136    {
 137      return $this->include_list;
 138    }
 139  
 140    /**

 141    * Begins writing a PHP function to the compiled template, using the

 142    * function_prefix and the function_suffix, the latter being post incremented

 143    * by one.

 144    */
 145    function beginFunction($param_list)
 146    {
 147        $func_name = 'tpl' . $this->function_prefix . $this->function_suffix++;
 148        $this->writePHP('function ' . $func_name . $param_list ." {\n");
 149        return $func_name;
 150    }
 151  
 152    function endFunction()
 153    {
 154      $this->writePHP(" }\n");
 155    }
 156  
 157    function setFunctionPrefix($prefix)
 158    {
 159      $this->function_prefix = $prefix;
 160    }
 161  
 162    /**

 163    * Utility method, which generates a unique variable name

 164    */
 165    function getTempVariable()
 166    {
 167      $var = $this->tempVarName++;
 168      if($var > 675)
 169        return chr(65 + ($var/26)/26) . chr(65 + ($var/26)%26) . chr(65 + $var%26);
 170      elseif($var > 26)
 171        return chr(65 + ($var/26)%26) . chr(65 + $var%26);
 172      else
 173        return chr(64 + $var);
 174    }
 175  
 176    /**

 177    * Utility method, which generates a unique variable name, prefixed with a $

 178    */
 179    function getTempVarRef()
 180    {
 181      return '$' . $this->getTempVariable();
 182    }
 183  }
 184  ?>


Generated: Sat Nov 22 03:48:54 2008 Cross-referenced by PHPXref 0.7