smarty_internal_config_file_compiler.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Config File Compiler
  4. *
  5. * This is the config file compiler class. It calls the lexer and parser to
  6. * perform the compiling.
  7. *
  8. * @package Smarty
  9. * @subpackage Config
  10. * @author Uwe Tews
  11. */
  12. /**
  13. * Main config file compiler class
  14. *
  15. * @package Smarty
  16. * @subpackage Config
  17. */
  18. class Smarty_Internal_Config_File_Compiler {
  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. * @return bool true if compiling succeeded, false if it failed
  65. */
  66. public function compileSource(Smarty_Internal_Config $config)
  67. {
  68. /* here is where the compiling takes place. Smarty
  69. tags in the templates are replaces with PHP code,
  70. then written to compiled files. */
  71. $this->config = $config;
  72. // get config file source
  73. $_content = $config->source->content . "\n";
  74. // on empty template just return
  75. if ($_content == '') {
  76. return true;
  77. }
  78. // init the lexer/parser to compile the config file
  79. $lex = new Smarty_Internal_Configfilelexer($_content, $this->smarty);
  80. $parser = new Smarty_Internal_Configfileparser($lex, $this);
  81. if ($this->smarty->_parserdebug) $parser->PrintTrace();
  82. // get tokens from lexer and parse them
  83. while ($lex->yylex()) {
  84. if ($this->smarty->_parserdebug) echo "<br>Parsing {$parser->yyTokenName[$lex->token]} Token {$lex->value} Line {$lex->line} \n";
  85. $parser->doParse($lex->token, $lex->value);
  86. }
  87. // finish parsing process
  88. $parser->doParse(0, 0);
  89. $config->compiled_config = '<?php $_config_vars = ' . var_export($this->config_data, true) . '; ?>';
  90. }
  91. /**
  92. * display compiler error messages without dying
  93. *
  94. * If parameter $args is empty it is a parser detected syntax error.
  95. * In this case the parser is called to obtain information about exspected tokens.
  96. *
  97. * If parameter $args contains a string this is used as error message
  98. *
  99. * @param string $args individual error message or null
  100. */
  101. public function trigger_config_file_error($args = null)
  102. {
  103. $this->lex = Smarty_Internal_Configfilelexer::instance();
  104. $this->parser = Smarty_Internal_Configfileparser::instance();
  105. // get template source line which has error
  106. $line = $this->lex->line;
  107. if (isset($args)) {
  108. // $line--;
  109. }
  110. $match = preg_split("/\n/", $this->lex->data);
  111. $error_text = "Syntax error in config file '{$this->config->source->filepath}' on line {$line} '{$match[$line-1]}' ";
  112. if (isset($args)) {
  113. // individual error message
  114. $error_text .= $args;
  115. } else {
  116. // exspected token from parser
  117. foreach ($this->parser->yy_get_expected_tokens($this->parser->yymajor) as $token) {
  118. $exp_token = $this->parser->yyTokenName[$token];
  119. if (isset($this->lex->smarty_token_names[$exp_token])) {
  120. // token type from lexer
  121. $expect[] = '"' . $this->lex->smarty_token_names[$exp_token] . '"';
  122. } else {
  123. // otherwise internal token name
  124. $expect[] = $this->parser->yyTokenName[$token];
  125. }
  126. }
  127. // output parser error message
  128. $error_text .= ' - Unexpected "' . $this->lex->value . '", expected one of: ' . implode(' , ', $expect);
  129. }
  130. throw new SmartyCompilerException($error_text);
  131. }
  132. }
  133. ?>