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