smarty_internal_config_file_compiler.php (6069B)
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 /** 13 * Main config file compiler class 14 * 15 * @package Smarty 16 * @subpackage Config 17 */ 18 class Smarty_Internal_Config_File_Compiler 19 { 20 /** 21 * Lexer class name 22 * 23 * @var string 24 */ 25 public $lexer_class; 26 27 /** 28 * Parser class name 29 * 30 * @var string 31 */ 32 public $parser_class; 33 /** 34 * Lexer object 35 * 36 * @var object 37 */ 38 public $lex; 39 40 /** 41 * Parser object 42 * 43 * @var object 44 */ 45 public $parser; 46 47 /** 48 * Smarty object 49 * 50 * @var Smarty object 51 */ 52 public $smarty; 53 54 /** 55 * Smarty object 56 * 57 * @var Smarty_Internal_Template object 58 */ 59 public $template; 60 61 /** 62 * Compiled config data sections and variables 63 * 64 * @var array 65 */ 66 public $config_data = array(); 67 68 /** 69 * compiled config data must always be written 70 * 71 * @var bool 72 */ 73 public $write_compiled_code = true; 74 75 /** 76 * Initialize compiler 77 * 78 * @param string $lexer_class class name 79 * @param string $parser_class class name 80 * @param Smarty $smarty global instance 81 */ 82 public function __construct($lexer_class, $parser_class, Smarty $smarty) 83 { 84 $this->smarty = $smarty; 85 // get required plugins 86 $this->lexer_class = $lexer_class; 87 $this->parser_class = $parser_class; 88 $this->smarty = $smarty; 89 $this->config_data['sections'] = array(); 90 $this->config_data['vars'] = array(); 91 } 92 93 /** 94 * Method to compile Smarty config source. 95 * 96 * @param Smarty_Internal_Template $template 97 * 98 * @return bool true if compiling succeeded, false if it failed 99 */ 100 public function compileTemplate(Smarty_Internal_Template $template) 101 { 102 $this->template = $template; 103 $this->template->properties['file_dependency'][$this->template->source->uid] = array($this->template->source->name, $this->template->source->timestamp, $this->template->source->type); 104 // on empty config just return 105 if ($template->source->content == '') { 106 return true; 107 } 108 if ($this->smarty->debugging) { 109 Smarty_Internal_Debug::start_compile($this->template); 110 } 111 // init the lexer/parser to compile the config file 112 $lex = new $this->lexer_class(str_replace(array("\r\n", "\r"), "\n", $template->source->content) . "\n", $this); 113 $parser = new $this->parser_class($lex, $this); 114 115 if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) { 116 $mbEncoding = mb_internal_encoding(); 117 mb_internal_encoding('ASCII'); 118 } else { 119 $mbEncoding = null; 120 } 121 122 if ($this->smarty->_parserdebug) { 123 $parser->PrintTrace(); 124 } 125 // get tokens from lexer and parse them 126 while ($lex->yylex()) { 127 if ($this->smarty->_parserdebug) { 128 echo "<br>Parsing {$parser->yyTokenName[$lex->token]} Token {$lex->value} Line {$lex->line} \n"; 129 } 130 $parser->doParse($lex->token, $lex->value); 131 } 132 // finish parsing process 133 $parser->doParse(0, 0); 134 135 if ($mbEncoding) { 136 mb_internal_encoding($mbEncoding); 137 } 138 if ($this->smarty->debugging) { 139 Smarty_Internal_Debug::end_compile($this->template); 140 } 141 // template header code 142 $template_header = "<?php /* Smarty version " . Smarty::SMARTY_VERSION . ", created on " . strftime("%Y-%m-%d %H:%M:%S") . "\n"; 143 $template_header .= " compiled from \"" . $this->template->source->filepath . "\" */ ?>\n"; 144 145 $code = '<?php Smarty_Internal_Extension_Config::loadConfigVars($_smarty_tpl, ' . var_export($this->config_data, true) . '); ?>'; 146 return $template_header . Smarty_Internal_Extension_CodeFrame::create($this->template, $code); 147 } 148 149 /** 150 * display compiler error messages without dying 151 * If parameter $args is empty it is a parser detected syntax error. 152 * In this case the parser is called to obtain information about expected tokens. 153 * If parameter $args contains a string this is used as error message 154 * 155 * @param string $args individual error message or null 156 * 157 * @throws SmartyCompilerException 158 */ 159 public function trigger_config_file_error($args = null) 160 { 161 $this->lex = Smarty_Internal_Configfilelexer::instance(); 162 $this->parser = Smarty_Internal_Configfileparser::instance(); 163 // get config source line which has error 164 $line = $this->lex->line; 165 if (isset($args)) { 166 // $line--; 167 } 168 $match = preg_split("/\n/", $this->lex->data); 169 $error_text = "Syntax error in config file '{$this->template->source->filepath}' on line {$line} '{$match[$line - 1]}' "; 170 if (isset($args)) { 171 // individual error message 172 $error_text .= $args; 173 } else { 174 // expected token from parser 175 foreach ($this->parser->yy_get_expected_tokens($this->parser->yymajor) as $token) { 176 $exp_token = $this->parser->yyTokenName[$token]; 177 if (isset($this->lex->smarty_token_names[$exp_token])) { 178 // token type from lexer 179 $expect[] = '"' . $this->lex->smarty_token_names[$exp_token] . '"'; 180 } else { 181 // otherwise internal token name 182 $expect[] = $this->parser->yyTokenName[$token]; 183 } 184 } 185 // output parser error message 186 $error_text .= ' - Unexpected "' . $this->lex->value . '", expected one of: ' . implode(' , ', $expect); 187 } 188 throw new SmartyCompilerException($error_text); 189 } 190 }