smarty_internal_smartytemplatecompiler.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. * @ignore
  12. */
  13. include 'smarty_internal_parsetree.php';
  14. /**
  15. * Class SmartyTemplateCompiler
  16. *
  17. * @package Smarty
  18. * @subpackage Compiler
  19. */
  20. class Smarty_Internal_SmartyTemplateCompiler extends Smarty_Internal_TemplateCompilerBase
  21. {
  22. /**
  23. * Lexer class name
  24. *
  25. * @var string
  26. */
  27. public $lexer_class;
  28. /**
  29. * Parser class name
  30. *
  31. * @var string
  32. */
  33. public $parser_class;
  34. /**
  35. * Lexer object
  36. *
  37. * @var object
  38. */
  39. public $lex;
  40. /**
  41. * Parser object
  42. *
  43. * @var object
  44. */
  45. public $parser;
  46. /**
  47. * Smarty object
  48. *
  49. * @var object
  50. */
  51. public $smarty;
  52. /**
  53. * array of vars which can be compiled in local scope
  54. *
  55. * @var array
  56. */
  57. public $local_var = array();
  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. {
  67. $this->smarty = $smarty;
  68. parent::__construct();
  69. // get required plugins
  70. $this->lexer_class = $lexer_class;
  71. $this->parser_class = $parser_class;
  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)
  81. {
  82. /* here is where the compiling takes place. Smarty
  83. tags in the templates are replaces with PHP code,
  84. then written to compiled files. */
  85. // init the lexer/parser to compile the template
  86. $this->lex = new $this->lexer_class($_content, $this);
  87. $this->parser = new $this->parser_class($this->lex, $this);
  88. if ($this->inheritance_child) {
  89. // start state on child templates
  90. $this->lex->yypushstate(Smarty_Internal_Templatelexer::CHILDBODY);
  91. }
  92. if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) {
  93. $mbEncoding = mb_internal_encoding();
  94. mb_internal_encoding('ASCII');
  95. } else {
  96. $mbEncoding = null;
  97. }
  98. if ($this->smarty->_parserdebug) {
  99. $this->parser->PrintTrace();
  100. $this->lex->PrintTrace();
  101. }
  102. // get tokens from lexer and parse them
  103. while ($this->lex->yylex() && !$this->abort_and_recompile) {
  104. if ($this->smarty->_parserdebug) {
  105. echo "<pre>Line {$this->lex->line} Parsing {$this->parser->yyTokenName[$this->lex->token]} Token " .
  106. htmlentities($this->lex->value) . "</pre>";
  107. }
  108. $this->parser->doParse($this->lex->token, $this->lex->value);
  109. }
  110. if ($this->abort_and_recompile) {
  111. // exit here on abort
  112. return false;
  113. }
  114. // finish parsing process
  115. $this->parser->doParse(0, 0);
  116. if ($mbEncoding) {
  117. mb_internal_encoding($mbEncoding);
  118. }
  119. // check for unclosed tags
  120. if (count($this->_tag_stack) > 0) {
  121. // get stacked info
  122. list($openTag, $_data) = array_pop($this->_tag_stack);
  123. $this->trigger_template_error("unclosed {$this->smarty->left_delimiter}" . $openTag . "{$this->smarty->right_delimiter} tag");
  124. }
  125. // return compiled code
  126. // return str_replace(array("? >\n<?php","? ><?php"), array('',''), $this->parser->retvalue);
  127. return $this->parser->retvalue;
  128. }
  129. }