/framework/vendor/smarty3/lib/libs/sysplugins/smarty_internal_config_file_compiler.php

http://zoop.googlecode.com/ · PHP · 116 lines · 58 code · 5 blank · 53 comment · 10 complexity · 815cc60ffd44c7c93a5fb9d08c8e44df MD5 · raw file

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