library/smarty/libs/sysplugins/smarty_internal_smartytemplatecompiler.php
changeset 0 4869aea77e21
equal deleted inserted replaced
-1:000000000000 0:4869aea77e21
       
     1 <?php
       
     2 /**
       
     3  * Smarty Internal Plugin Smarty Template Compiler Base
       
     4  * This file contains the basic classes and methods for compiling Smarty templates with lexer/parser
       
     5  *
       
     6  * @package    Smarty
       
     7  * @subpackage Compiler
       
     8  * @author     Uwe Tews
       
     9  */
       
    10 
       
    11 /**
       
    12  * @ignore
       
    13  */
       
    14 //include 'smarty_internal_parsetree.php';
       
    15 
       
    16 /**
       
    17  * Class SmartyTemplateCompiler
       
    18  *
       
    19  * @package    Smarty
       
    20  * @subpackage Compiler
       
    21  */
       
    22 class Smarty_Internal_SmartyTemplateCompiler extends Smarty_Internal_TemplateCompilerBase {
       
    23     /**
       
    24      * Lexer class name
       
    25      *
       
    26      * @var string
       
    27      */
       
    28     public $lexer_class;
       
    29 
       
    30     /**
       
    31      * Parser class name
       
    32      *
       
    33      * @var string
       
    34      */
       
    35     public $parser_class;
       
    36 
       
    37     /**
       
    38      * Lexer object
       
    39      *
       
    40      * @var object
       
    41      */
       
    42     public $lex;
       
    43 
       
    44     /**
       
    45      * Parser object
       
    46      *
       
    47      * @var object
       
    48      */
       
    49     public $parser;
       
    50 
       
    51     /**
       
    52      * array of vars which can be compiled in local scope
       
    53      *
       
    54      * @var array
       
    55      */
       
    56     public $local_var = array();
       
    57 
       
    58     /**
       
    59      * Initialize compiler
       
    60      *
       
    61      * @param string $lexer_class class name
       
    62      * @param string $parser_class class name
       
    63      * @param Smarty $smarty global instance
       
    64      */
       
    65     public function __construct($lexer_class, $parser_class, $smarty) {
       
    66         $this->smarty = $smarty;
       
    67         parent::__construct();
       
    68         // get required plugins
       
    69         $this->lexer_class = $lexer_class;
       
    70         $this->parser_class = $parser_class;
       
    71     }
       
    72 
       
    73     /**
       
    74      * method to compile a Smarty template
       
    75      *
       
    76      * @param  mixed $_content template source
       
    77      *
       
    78      * @return bool  true if compiling succeeded, false if it failed
       
    79      */
       
    80     protected function doCompile($_content, $isTemplateSource = false) {
       
    81         /* here is where the compiling takes place. Smarty
       
    82           tags in the templates are replaces with PHP code,
       
    83           then written to compiled files. */
       
    84         // init the lexer/parser to compile the template
       
    85         $this->lex = new $this->lexer_class(str_replace(array("\r\n", "\r"), "\n", $_content), $this);
       
    86         $this->parser = new $this->parser_class($this->lex, $this);
       
    87         if ($isTemplateSource) {
       
    88             $this->parser->insertPhpCode("<?php\n\$_smarty_tpl->properties['nocache_hash'] = '{$this->nocache_hash}';\n?>\n");
       
    89         }
       
    90         if ($this->inheritance_child) {
       
    91             // start state on child templates
       
    92             $this->lex->yypushstate(Smarty_Internal_Templatelexer::CHILDBODY);
       
    93         }
       
    94         if (function_exists('mb_internal_encoding') && ((int)ini_get('mbstring.func_overload')) & 2) {
       
    95             $mbEncoding = mb_internal_encoding();
       
    96             mb_internal_encoding('ASCII');
       
    97         } else {
       
    98             $mbEncoding = null;
       
    99         }
       
   100 
       
   101         if ($this->smarty->_parserdebug) {
       
   102             $this->parser->PrintTrace();
       
   103             $this->lex->PrintTrace();
       
   104         }
       
   105         // get tokens from lexer and parse them
       
   106         while ($this->lex->yylex() && !$this->abort_and_recompile) {
       
   107             if ($this->smarty->_parserdebug) {
       
   108                 echo "<pre>Line {$this->lex->line} Parsing  {$this->parser->yyTokenName[$this->lex->token]} Token " .
       
   109                     htmlentities($this->lex->value) . "</pre>";
       
   110             }
       
   111             $this->parser->doParse($this->lex->token, $this->lex->value);
       
   112         }
       
   113 
       
   114         if ($this->abort_and_recompile) {
       
   115             // exit here on abort
       
   116             return false;
       
   117         }
       
   118         // finish parsing process
       
   119         $this->parser->doParse(0, 0);
       
   120         if ($mbEncoding) {
       
   121             mb_internal_encoding($mbEncoding);
       
   122         }
       
   123         // check for unclosed tags
       
   124         if (count($this->_tag_stack) > 0) {
       
   125             // get stacked info
       
   126             list($openTag, $_data) = array_pop($this->_tag_stack);
       
   127             $this->trigger_template_error("unclosed {$this->smarty->left_delimiter}" . $openTag . "{$this->smarty->right_delimiter} tag");
       
   128         }
       
   129         // return compiled code
       
   130         // return str_replace(array("? >\n<?php","? ><?php"), array('',''), $this->parser->retvalue);
       
   131         return $this->parser->retvalue;
       
   132     }
       
   133 }