You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_internal_config_file_compiler.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Config File Compiler
  4. * This is the config file compiler class. It calls the lexer and parser to
  5. * perform the compiling.
  6. *
  7. * @package Smarty
  8. * @subpackage Config
  9. * @author Uwe Tews
  10. */
  11. /**
  12. * Main config file compiler class
  13. *
  14. * @package Smarty
  15. * @subpackage Config
  16. */
  17. class Smarty_Internal_Config_File_Compiler
  18. {
  19. /**
  20. * Lexer class name
  21. *
  22. * @var string
  23. */
  24. public $lexer_class;
  25. /**
  26. * Parser class name
  27. *
  28. * @var string
  29. */
  30. public $parser_class;
  31. /**
  32. * Lexer object
  33. *
  34. * @var object
  35. */
  36. public $lex;
  37. /**
  38. * Parser object
  39. *
  40. * @var object
  41. */
  42. public $parser;
  43. /**
  44. * Smarty object
  45. *
  46. * @var Smarty object
  47. */
  48. public $smarty;
  49. /**
  50. * Smarty object
  51. *
  52. * @var Smarty_Internal_Template object
  53. */
  54. public $template;
  55. /**
  56. * Compiled config data sections and variables
  57. *
  58. * @var array
  59. */
  60. public $config_data = array();
  61. /**
  62. * compiled config data must always be written
  63. *
  64. * @var bool
  65. */
  66. public $write_compiled_code = true;
  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 $smarty)
  75. {
  76. $this->smarty = $smarty;
  77. // get required plugins
  78. $this->lexer_class = $lexer_class;
  79. $this->parser_class = $parser_class;
  80. $this->smarty = $smarty;
  81. $this->config_data[ 'sections' ] = array();
  82. $this->config_data[ 'vars' ] = array();
  83. }
  84. /**
  85. * Method to compile Smarty config source.
  86. *
  87. * @param Smarty_Internal_Template $template
  88. *
  89. * @return bool true if compiling succeeded, false if it failed
  90. * @throws \SmartyException
  91. */
  92. public function compileTemplate(Smarty_Internal_Template $template)
  93. {
  94. $this->template = $template;
  95. $this->template->compiled->file_dependency[ $this->template->source->uid ] =
  96. array($this->template->source->filepath,
  97. $this->template->source->getTimeStamp(),
  98. $this->template->source->type);
  99. if ($this->smarty->debugging) {
  100. if (!isset($this->smarty->_debug)) {
  101. $this->smarty->_debug = new Smarty_Internal_Debug();
  102. }
  103. $this->smarty->_debug->start_compile($this->template);
  104. }
  105. // init the lexer/parser to compile the config file
  106. /* @var Smarty_Internal_ConfigFileLexer $this ->lex */
  107. $this->lex = new $this->lexer_class(str_replace(array("\r\n",
  108. "\r"), "\n", $template->source->getContent()) . "\n",
  109. $this);
  110. /* @var Smarty_Internal_ConfigFileParser $this ->parser */
  111. $this->parser = new $this->parser_class($this->lex, $this);
  112. if (function_exists('mb_internal_encoding')
  113. && function_exists('ini_get')
  114. && ((int) ini_get('mbstring.func_overload')) & 2
  115. ) {
  116. $mbEncoding = mb_internal_encoding();
  117. mb_internal_encoding('ASCII');
  118. } else {
  119. $mbEncoding = null;
  120. }
  121. if ($this->smarty->_parserdebug) {
  122. $this->parser->PrintTrace();
  123. }
  124. // get tokens from lexer and parse them
  125. while ($this->lex->yylex()) {
  126. if ($this->smarty->_parserdebug) {
  127. echo "<br>Parsing {$this->parser->yyTokenName[$this->lex->token]} Token {$this->lex->value} Line {$this->lex->line} \n";
  128. }
  129. $this->parser->doParse($this->lex->token, $this->lex->value);
  130. }
  131. // finish parsing process
  132. $this->parser->doParse(0, 0);
  133. if ($mbEncoding) {
  134. mb_internal_encoding($mbEncoding);
  135. }
  136. if ($this->smarty->debugging) {
  137. $this->smarty->_debug->end_compile($this->template);
  138. }
  139. // template header code
  140. $template_header =
  141. "<?php /* Smarty version " . Smarty::SMARTY_VERSION . ", created on " . strftime("%Y-%m-%d %H:%M:%S") .
  142. "\n";
  143. $template_header .= " compiled from '{$this->template->source->filepath}' */ ?>\n";
  144. $code = '<?php $_smarty_tpl->smarty->ext->configLoad->_loadConfigVars($_smarty_tpl, ' .
  145. var_export($this->config_data, true) . '); ?>';
  146. return $template_header . $this->template->smarty->ext->_codeFrame->create($this->template, $code);
  147. }
  148. /**
  149. * display compiler error messages without dying
  150. * If parameter $args is empty it is a parser detected syntax error.
  151. * In this case the parser is called to obtain information about expected tokens.
  152. * If parameter $args contains a string this is used as error message
  153. *
  154. * @param string $args individual error message or null
  155. *
  156. * @throws SmartyCompilerException
  157. */
  158. public function trigger_config_file_error($args = null)
  159. {
  160. // get config source line which has error
  161. $line = $this->lex->line;
  162. if (isset($args)) {
  163. // $line--;
  164. }
  165. $match = preg_split("/\n/", $this->lex->data);
  166. $error_text =
  167. "Syntax error in config file '{$this->template->source->filepath}' on line {$line} '{$match[$line - 1]}' ";
  168. if (isset($args)) {
  169. // individual error message
  170. $error_text .= $args;
  171. } else {
  172. // expected token from parser
  173. foreach ($this->parser->yy_get_expected_tokens($this->parser->yymajor) as $token) {
  174. $exp_token = $this->parser->yyTokenName[ $token ];
  175. if (isset($this->lex->smarty_token_names[ $exp_token ])) {
  176. // token type from lexer
  177. $expect[] = '"' . $this->lex->smarty_token_names[ $exp_token ] . '"';
  178. } else {
  179. // otherwise internal token name
  180. $expect[] = $this->parser->yyTokenName[ $token ];
  181. }
  182. }
  183. // output parser error message
  184. $error_text .= ' - Unexpected "' . $this->lex->value . '", expected one of: ' . implode(' , ', $expect);
  185. }
  186. throw new SmartyCompilerException($error_text);
  187. }
  188. }