PageRenderTime 24ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/PhPVersion/application/third_party/Smarty/libs/sysplugins/smarty_internal_config_file_compiler.php

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