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.
13 * Main config file compiler class
18 class Smarty_Internal_Config_File_Compiler
58 * @var Smarty_Internal_Template object
63 * Compiled config data sections and variables
67 public $config_data = array();
70 * compiled config data must always be written
74 public $write_compiled_code = true;
79 * @param string $lexer_class class name
80 * @param string $parser_class class name
81 * @param Smarty $smarty global instance
83 public function __construct($lexer_class, $parser_class, Smarty $smarty)
85 $this->smarty = $smarty;
86 // get required plugins
87 $this->lexer_class = $lexer_class;
88 $this->parser_class = $parser_class;
89 $this->smarty = $smarty;
90 $this->config_data[ 'sections' ] = array();
91 $this->config_data[ 'vars' ] = array();
95 * Method to compile Smarty config source.
97 * @param Smarty_Internal_Template $template
99 * @return bool true if compiling succeeded, false if it failed
101 public function compileTemplate(Smarty_Internal_Template $template)
103 $this->template = $template;
104 $this->template->compiled->file_dependency[ $this->template->source->uid ] =
105 array($this->template->source->filepath, $this->template->source->getTimeStamp(),
106 $this->template->source->type);
107 if ($this->smarty->debugging) {
108 if (!isset( $this->smarty->_debug)) {
109 $this->smarty->_debug = new Smarty_Internal_Debug();
111 $this->smarty->_debug->start_compile($this->template);
113 // init the lexer/parser to compile the config file
114 /* @var Smarty_Internal_ConfigFileLexer $this->lex */
115 $this->lex = new $this->lexer_class(str_replace(array("\r\n", "\r"), "\n", $template->source->getContent()) . "\n",
117 /* @var Smarty_Internal_ConfigFileParser $this->parser */
118 $this->parser = new $this->parser_class($this->lex, $this);
120 if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) {
121 $mbEncoding = mb_internal_encoding();
122 mb_internal_encoding('ASCII');
127 if ($this->smarty->_parserdebug) {
128 $this->parser->PrintTrace();
130 // get tokens from lexer and parse them
131 while ($this->lex->yylex()) {
132 if ($this->smarty->_parserdebug) {
133 echo "<br>Parsing {$this->parser->yyTokenName[$this->lex->token]} Token {$this->lex->value} Line {$this->lex->line} \n";
135 $this->parser->doParse($this->lex->token, $this->lex->value);
137 // finish parsing process
138 $this->parser->doParse(0, 0);
141 mb_internal_encoding($mbEncoding);
143 if ($this->smarty->debugging) {
144 $this->smarty->_debug->end_compile($this->template);
146 // template header code
148 "<?php /* Smarty version " . Smarty::SMARTY_VERSION . ", created on " . strftime("%Y-%m-%d %H:%M:%S") .
150 $template_header .= " compiled from \"" . $this->template->source->filepath . "\" */ ?>\n";
152 $code = '<?php $_smarty_tpl->smarty->ext->configLoad->_loadConfigVars($_smarty_tpl, ' .
153 var_export($this->config_data, true) . '); ?>';
154 return $template_header . $this->template->smarty->ext->_codeFrame->create($this->template, $code);
158 * display compiler error messages without dying
159 * If parameter $args is empty it is a parser detected syntax error.
160 * In this case the parser is called to obtain information about expected tokens.
161 * If parameter $args contains a string this is used as error message
163 * @param string $args individual error message or null
165 * @throws SmartyCompilerException
167 public function trigger_config_file_error($args = null)
169 // get config source line which has error
170 $line = $this->lex->line;
174 $match = preg_split("/\n/", $this->lex->data);
176 "Syntax error in config file '{$this->template->source->filepath}' on line {$line} '{$match[$line - 1]}' ";
178 // individual error message
179 $error_text .= $args;
181 // expected token from parser
182 foreach ($this->parser->yy_get_expected_tokens($this->parser->yymajor) as $token) {
183 $exp_token = $this->parser->yyTokenName[ $token ];
184 if (isset($this->lex->smarty_token_names[ $exp_token ])) {
185 // token type from lexer
186 $expect[] = '"' . $this->lex->smarty_token_names[ $exp_token ] . '"';
188 // otherwise internal token name
189 $expect[] = $this->parser->yyTokenName[ $token ];
192 // output parser error message
193 $error_text .= ' - Unexpected "' . $this->lex->value . '", expected one of: ' . implode(' , ', $expect);
195 throw new SmartyCompilerException($error_text);