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