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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. */
  91. public function compileTemplate(Smarty_Internal_Template $template)
  92. {
  93. $this->template = $template;
  94. $this->template->compiled->file_dependency[$this->template->source->uid] = array($this->template->source->filepath,
  95. $this->template->source->getTimeStamp(),
  96. $this->template->source->type);
  97. if ($this->smarty->debugging) {
  98. $this->smarty->_debug->start_compile($this->template);
  99. }
  100. // init the lexer/parser to compile the config file
  101. /* @var Smarty_Internal_ConfigFileLexer $lex */
  102. $lex = new $this->lexer_class(str_replace(array("\r\n", "\r"), "\n", $template->source->getContent()) .
  103. "\n", $this);
  104. /* @var Smarty_Internal_ConfigFileParser $parser */
  105. $parser = new $this->parser_class($lex, $this);
  106. if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) {
  107. $mbEncoding = mb_internal_encoding();
  108. mb_internal_encoding('ASCII');
  109. } else {
  110. $mbEncoding = null;
  111. }
  112. if ($this->smarty->_parserdebug) {
  113. $parser->PrintTrace();
  114. }
  115. // get tokens from lexer and parse them
  116. while ($lex->yylex()) {
  117. if ($this->smarty->_parserdebug) {
  118. echo "<br>Parsing {$parser->yyTokenName[$lex->token]} Token {$lex->value} Line {$lex->line} \n";
  119. }
  120. $parser->doParse($lex->token, $lex->value);
  121. }
  122. // finish parsing process
  123. $parser->doParse(0, 0);
  124. if ($mbEncoding) {
  125. mb_internal_encoding($mbEncoding);
  126. }
  127. if ($this->smarty->debugging) {
  128. $this->smarty->_debug->end_compile($this->template);
  129. }
  130. // template header code
  131. $template_header = "<?php /* Smarty version " . Smarty::SMARTY_VERSION . ", created on " .
  132. strftime("%Y-%m-%d %H:%M:%S") . "\n";
  133. $template_header .= " compiled from \"" . $this->template->source->filepath . "\" */ ?>\n";
  134. $code = '<?php $_smarty_tpl->smarty->ext->configLoad->_loadConfigVars($_smarty_tpl, ' .
  135. var_export($this->config_data, true) . '); ?>';
  136. return $template_header . $this->template->smarty->ext->_codeFrame->create($this->template, $code);
  137. }
  138. /**
  139. * display compiler error messages without dying
  140. * If parameter $args is empty it is a parser detected syntax error.
  141. * In this case the parser is called to obtain information about expected tokens.
  142. * If parameter $args contains a string this is used as error message
  143. *
  144. * @param string $args individual error message or null
  145. *
  146. * @throws SmartyCompilerException
  147. */
  148. public function trigger_config_file_error($args = null)
  149. {
  150. $this->lex = Smarty_Internal_Configfilelexer::instance();
  151. $this->parser = Smarty_Internal_Configfileparser::instance();
  152. // get config source line which has error
  153. $line = $this->lex->line;
  154. if (isset($args)) {
  155. // $line--;
  156. }
  157. $match = preg_split("/\n/", $this->lex->data);
  158. $error_text = "Syntax error in config file '{$this->template->source->filepath}' on line {$line} '{$match[$line - 1]}' ";
  159. if (isset($args)) {
  160. // individual error message
  161. $error_text .= $args;
  162. } else {
  163. // expected token from parser
  164. foreach ($this->parser->yy_get_expected_tokens($this->parser->yymajor) as $token) {
  165. $exp_token = $this->parser->yyTokenName[$token];
  166. if (isset($this->lex->smarty_token_names[$exp_token])) {
  167. // token type from lexer
  168. $expect[] = '"' . $this->lex->smarty_token_names[$exp_token] . '"';
  169. } else {
  170. // otherwise internal token name
  171. $expect[] = $this->parser->yyTokenName[$token];
  172. }
  173. }
  174. // output parser error message
  175. $error_text .= ' - Unexpected "' . $this->lex->value . '", expected one of: ' . implode(' , ', $expect);
  176. }
  177. throw new SmartyCompilerException($error_text);
  178. }
  179. }