[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/wact/src/compiler/ -> WactCodeWriter.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 WactCodeWriter.

  12   *

  13   * @package wact

  14   * @version $Id: WactCodeWriter.class.php 5945 2007-06-06 08:31:43Z pachanga $

  15   */
  16  class WactCodeWriter
  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  
  82      return $text;
  83    }
  84  
  85    function writeHTML($text)
  86    {
  87      $this->switchToHTML(substr($text,0,1));
  88      $this->code .= $text;
  89    }
  90  
  91    function renderCode()
  92    {
  93      $this->switchToHTML();
  94  
  95      $this->_prependIncludeListToCode();
  96  
  97      return $this->code;
  98    }
  99  
 100    function getCode()
 101    {
 102      return $this->code;
 103    }
 104  
 105    function setCode($code)
 106    {
 107      $this->code = $code;
 108    }
 109  
 110    protected function _prependIncludeListToCode()
 111    {
 112      $include_code = '';
 113      foreach($this->include_list as $include_file)
 114        $include_code .= "require_once '$include_file';\n";
 115  
 116      if (!empty($include_code))
 117        $this->code = '<?php ' . $include_code . '?>' . $this->code;
 118    }
 119  
 120    function getMode()
 121    {
 122      return $this->current_mode;
 123    }
 124  
 125    function registerInclude($include_file)
 126    {
 127      $this->_replaceConstantsInString($include_file);
 128  
 129      if (!in_array($include_file, $this->include_list)) {
 130          $this->include_list[] = $include_file;
 131      }
 132    }
 133  
 134    function getIncludeList()
 135    {
 136      return $this->include_list;
 137    }
 138  
 139    function _replaceConstantsInString(&$string)
 140    {
 141      $constPos = strpos($string, '%');
 142      while (is_integer($constPos))
 143      {
 144        $constant = substr($string, $constPos+1, strpos($string, '%', $constPos+1) - $constPos - 1);
 145        if (defined($constant))
 146          $string = str_replace("%$constant%", constant($constant), $string);
 147        else
 148          throw new WactException('Constant is not defined', array('constant' => $constant));
 149  
 150        $constPos = strpos($string, '%');
 151      }
 152      return true;
 153    }
 154  
 155    /**

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

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

 158    * by one.

 159    */
 160    function beginFunction($param_list)
 161    {
 162        $func_name = 'tpl' . $this->function_prefix . $this->function_suffix++;
 163        $this->writePHP('function ' . $func_name . $param_list ." {\n");
 164        return $func_name;
 165    }
 166  
 167    function endFunction()
 168    {
 169      $this->writePHP(" }\n");
 170    }
 171  
 172    function setFunctionPrefix($prefix)
 173    {
 174      $this->function_prefix = $prefix;
 175    }
 176  
 177    /**

 178    * Utility method, which generates a unique variable name

 179    */
 180    function getTempVariable()
 181    {
 182      $var = $this->tempVarName++;
 183      if ($var > 675)
 184        return chr(65 + ($var/26)/26) . chr(65 + ($var/26)%26) . chr(65 + $var%26);
 185      elseif ($var > 26)
 186        return chr(65 + ($var/26)%26) . chr(65 + $var%26);
 187      else
 188        return chr(64 + $var);
 189    }
 190  
 191    /**

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

 193    */
 194    function getTempVarRef()
 195    {
 196      return '$' . $this->getTempVariable();
 197    }
 198  }
 199  ?>


Generated: Fri Aug 29 04:49:26 2008 Cross-referenced by PHPXref 0.7