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
44 * @var Smarty_Internal_Config object
49 * Compiled config data sections and variables
53 public $config_data = array();
58 * @param Smarty $smarty base instance
60 public function __construct($smarty)
62 $this->smarty = $smarty;
63 $this->config_data['sections'] = array();
64 $this->config_data['vars'] = array();
68 * Method to compile a Smarty template.
70 * @param Smarty_Internal_Config $config config object
72 * @return bool true if compiling succeeded, false if it failed
74 public function compileSource(Smarty_Internal_Config $config)
76 /* here is where the compiling takes place. Smarty
77 tags in the templates are replaces with PHP code,
78 then written to compiled files. */
79 $this->config = $config;
80 // get config file source
81 $_content = $config->source->content . "\n";
82 // on empty template just return
83 if ($_content == '') {
86 // init the lexer/parser to compile the config file
87 $lex = new Smarty_Internal_Configfilelexer($_content, $this);
88 $parser = new Smarty_Internal_Configfileparser($lex, $this);
90 if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) {
91 $mbEncoding = mb_internal_encoding();
92 mb_internal_encoding('ASCII');
98 if ($this->smarty->_parserdebug) {
99 $parser->PrintTrace();
101 // get tokens from lexer and parse them
102 while ($lex->yylex()) {
103 if ($this->smarty->_parserdebug) {
104 echo "<br>Parsing {$parser->yyTokenName[$lex->token]} Token {$lex->value} Line {$lex->line} \n";
106 $parser->doParse($lex->token, $lex->value);
108 // finish parsing process
109 $parser->doParse(0, 0);
112 mb_internal_encoding($mbEncoding);
115 $config->compiled_config = '<?php $_config_vars = ' . var_export($this->config_data, true) . '; ?>';
119 * display compiler error messages without dying
120 * If parameter $args is empty it is a parser detected syntax error.
121 * In this case the parser is called to obtain information about expected tokens.
122 * If parameter $args contains a string this is used as error message
124 * @param string $args individual error message or null
126 * @throws SmartyCompilerException
128 public function trigger_config_file_error($args = null)
130 $this->lex = Smarty_Internal_Configfilelexer::instance();
131 $this->parser = Smarty_Internal_Configfileparser::instance();
132 // get template source line which has error
133 $line = $this->lex->line;
137 $match = preg_split("/\n/", $this->lex->data);
138 $error_text = "Syntax error in config file '{$this->config->source->filepath}' on line {$line} '{$match[$line - 1]}' ";
140 // individual error message
141 $error_text .= $args;
143 // expected token from parser
144 foreach ($this->parser->yy_get_expected_tokens($this->parser->yymajor) as $token) {
145 $exp_token = $this->parser->yyTokenName[$token];
146 if (isset($this->lex->smarty_token_names[$exp_token])) {
147 // token type from lexer
148 $expect[] = '"' . $this->lex->smarty_token_names[$exp_token] . '"';
150 // otherwise internal token name
151 $expect[] = $this->parser->yyTokenName[$token];
154 // output parser error message
155 $error_text .= ' - Unexpected "' . $this->lex->value . '", expected one of: ' . implode(' , ', $expect);
157 throw new SmartyCompilerException($error_text);