smarty_internal_config_file_compiler.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 object
  21. *
  22. * @var object
  23. */
  24. public $lex;
  25. /**
  26. * Parser object
  27. *
  28. * @var object
  29. */
  30. public $parser;
  31. /**
  32. * Smarty object
  33. *
  34. * @var Smarty object
  35. */
  36. public $smarty;
  37. /**
  38. * Smarty object
  39. *
  40. * @var Smarty_Internal_Config object
  41. */
  42. public $config;
  43. /**
  44. * Compiled config data sections and variables
  45. *
  46. * @var array
  47. */
  48. public $config_data = array();
  49. /**
  50. * Initialize compiler
  51. *
  52. * @param Smarty $smarty base instance
  53. */
  54. public function __construct($smarty)
  55. {
  56. $this->smarty = $smarty;
  57. $this->config_data['sections'] = array();
  58. $this->config_data['vars'] = array();
  59. }
  60. /**
  61. * Method to compile a Smarty template.
  62. *
  63. * @param Smarty_Internal_Config $config config object
  64. *
  65. * @return bool true if compiling succeeded, false if it failed
  66. */
  67. public function compileSource(Smarty_Internal_Config $config)
  68. {
  69. /* here is where the compiling takes place. Smarty
  70. tags in the templates are replaces with PHP code,
  71. then written to compiled files. */
  72. $this->config = $config;
  73. // get config file source
  74. $_content = $config->source->content . "\n";
  75. // on empty template just return
  76. if ($_content == '') {
  77. return true;
  78. }
  79. // init the lexer/parser to compile the config file
  80. $lex = new Smarty_Internal_Configfilelexer($_content, $this);
  81. $parser = new Smarty_Internal_Configfileparser($lex, $this);
  82. if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) {
  83. $mbEncoding = mb_internal_encoding();
  84. mb_internal_encoding('ASCII');
  85. } else {
  86. $mbEncoding = null;
  87. }
  88. if ($this->smarty->_parserdebug) {
  89. $parser->PrintTrace();
  90. }
  91. // get tokens from lexer and parse them
  92. while ($lex->yylex()) {
  93. if ($this->smarty->_parserdebug) {
  94. echo "<br>Parsing {$parser->yyTokenName[$lex->token]} Token {$lex->value} Line {$lex->line} \n";
  95. }
  96. $parser->doParse($lex->token, $lex->value);
  97. }
  98. // finish parsing process
  99. $parser->doParse(0, 0);
  100. if ($mbEncoding) {
  101. mb_internal_encoding($mbEncoding);
  102. }
  103. $config->compiled_config = '<?php $_config_vars = ' . var_export($this->config_data, true) . '; ?>';
  104. }
  105. /**
  106. * display compiler error messages without dying
  107. * If parameter $args is empty it is a parser detected syntax error.
  108. * In this case the parser is called to obtain information about expected tokens.
  109. * If parameter $args contains a string this is used as error message
  110. *
  111. * @param string $args individual error message or null
  112. *
  113. * @throws SmartyCompilerException
  114. */
  115. public function trigger_config_file_error($args = null)
  116. {
  117. $this->lex = Smarty_Internal_Configfilelexer::instance();
  118. $this->parser = Smarty_Internal_Configfileparser::instance();
  119. // get template source line which has error
  120. $line = $this->lex->line;
  121. if (isset($args)) {
  122. // $line--;
  123. }
  124. $match = preg_split("/\n/", $this->lex->data);
  125. $error_text = "Syntax error in config file '{$this->config->source->filepath}' on line {$line} '{$match[$line - 1]}' ";
  126. if (isset($args)) {
  127. // individual error message
  128. $error_text .= $args;
  129. } else {
  130. // expected token from parser
  131. foreach ($this->parser->yy_get_expected_tokens($this->parser->yymajor) as $token) {
  132. $exp_token = $this->parser->yyTokenName[$token];
  133. if (isset($this->lex->smarty_token_names[$exp_token])) {
  134. // token type from lexer
  135. $expect[] = '"' . $this->lex->smarty_token_names[$exp_token] . '"';
  136. } else {
  137. // otherwise internal token name
  138. $expect[] = $this->parser->yyTokenName[$token];
  139. }
  140. }
  141. // output parser error message
  142. $error_text .= ' - Unexpected "' . $this->lex->value . '", expected one of: ' . implode(' , ', $expect);
  143. }
  144. throw new SmartyCompilerException($error_text);
  145. }
  146. }