smarty_internal_config_file_compiler.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. class Smarty_Internal_Config_File_Compiler {
  16. /**
  17. * Initialize compiler
  18. */
  19. public function __construct($smarty)
  20. {
  21. $this->smarty = $smarty;
  22. // get required plugins
  23. $this->smarty->loadPlugin('Smarty_Internal_Configfilelexer');
  24. $this->smarty->loadPlugin('Smarty_Internal_Configfileparser');
  25. $this->config_data['sections'] = array();
  26. $this->config_data['vars'] = array();
  27. }
  28. /**
  29. * Methode to compile a Smarty template
  30. *
  31. * @param $template template object to compile
  32. * @return bool true if compiling succeeded, false if it failed
  33. */
  34. public function compileSource($config)
  35. {
  36. /* here is where the compiling takes place. Smarty
  37. tags in the templates are replaces with PHP code,
  38. then written to compiled files. */
  39. $this->config = $config;
  40. // get config file source
  41. $_content = $config->getConfigSource() . "\n";
  42. // on empty template just return
  43. if ($_content == '') {
  44. return true;
  45. }
  46. // init the lexer/parser to compile the config file
  47. $lex = new Smarty_Internal_Configfilelexer($_content, $this->smarty);
  48. $parser = new Smarty_Internal_Configfileparser($lex, $this);
  49. if (isset($this->smarty->_parserdebug)) $parser->PrintTrace();
  50. // get tokens from lexer and parse them
  51. while ($lex->yylex()) {
  52. if (isset($this->smarty->_parserdebug)) echo "<br>Parsing {$parser->yyTokenName[$lex->token]} Token {$lex->value} Line {$lex->line} \n";
  53. $parser->doParse($lex->token, $lex->value);
  54. }
  55. // finish parsing process
  56. $parser->doParse(0, 0);
  57. $config->compiled_config = '<?php $_config_vars = ' . var_export($this->config_data, true) . '; ?>';
  58. }
  59. /**
  60. * display compiler error messages without dying
  61. *
  62. * If parameter $args is empty it is a parser detected syntax error.
  63. * In this case the parser is called to obtain information about exspected tokens.
  64. *
  65. * If parameter $args contains a string this is used as error message
  66. *
  67. * @todo output exact position of parse error in source line
  68. * @param $args string individual error message or null
  69. */
  70. public function trigger_config_file_error($args = null)
  71. {
  72. $this->lex = Smarty_Internal_Configfilelexer::instance();
  73. $this->parser = Smarty_Internal_Configfileparser::instance();
  74. // get template source line which has error
  75. $line = $this->lex->line;
  76. if (isset($args)) {
  77. // $line--;
  78. }
  79. $match = preg_split("/\n/", $this->lex->data);
  80. $error_text = "Syntax error in config file '{$this->config->getConfigFilepath()}' on line {$line} '{$match[$line-1]}' ";
  81. if (isset($args)) {
  82. // individual error message
  83. $error_text .= $args;
  84. } else {
  85. // exspected token from parser
  86. foreach ($this->parser->yy_get_expected_tokens($this->parser->yymajor) as $token) {
  87. $exp_token = $this->parser->yyTokenName[$token];
  88. if (isset($this->lex->smarty_token_names[$exp_token])) {
  89. // token type from lexer
  90. $expect[] = '"' . $this->lex->smarty_token_names[$exp_token] . '"';
  91. } else {
  92. // otherwise internal token name
  93. $expect[] = $this->parser->yyTokenName[$token];
  94. }
  95. }
  96. // output parser error message
  97. $error_text .= ' - Unexpected "' . $this->lex->value . '", expected one of: ' . implode(' , ', $expect);
  98. }
  99. throw new Exception($error_text);
  100. }
  101. }
  102. ?>