3 * Smarty Internal Plugin Config File Compiler
5 * This is the config file compiler class. It calls the lexer and parser to
6 * perform the compiling.
14 * Main config file compiler class
19 class Smarty_Internal_Config_File_Compiler {
45 * @var Smarty_Internal_Config object
50 * Compiled config data sections and variables
54 public $config_data = array();
59 * @param Smarty $smarty base instance
61 public function __construct($smarty)
63 $this->smarty = $smarty;
64 $this->config_data['sections'] = array();
65 $this->config_data['vars'] = array();
69 * Method to compile a Smarty template.
71 * @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->smarty);
88 $parser = new Smarty_Internal_Configfileparser($lex, $this);
89 if ($this->smarty->_parserdebug) $parser->PrintTrace();
90 // get tokens from lexer and parse them
91 while ($lex->yylex()) {
92 if ($this->smarty->_parserdebug) echo "<br>Parsing {$parser->yyTokenName[$lex->token]} Token {$lex->value} Line {$lex->line} \n";
93 $parser->doParse($lex->token, $lex->value);
95 // finish parsing process
96 $parser->doParse(0, 0);
97 $config->compiled_config = '<?php $_config_vars = ' . var_export($this->config_data, true) . '; ?>';
101 * display compiler error messages without dying
103 * If parameter $args is empty it is a parser detected syntax error.
104 * In this case the parser is called to obtain information about exspected tokens.
106 * If parameter $args contains a string this is used as error message
108 * @param string $args individual error message or null
110 public function trigger_config_file_error($args = null)
112 $this->lex = Smarty_Internal_Configfilelexer::instance();
113 $this->parser = Smarty_Internal_Configfileparser::instance();
114 // get template source line which has error
115 $line = $this->lex->line;
119 $match = preg_split("/\n/", $this->lex->data);
120 $error_text = "Syntax error in config file '{$this->config->source->filepath}' on line {$line} '{$match[$line-1]}' ";
122 // individual error message
123 $error_text .= $args;
125 // exspected token from parser
126 foreach ($this->parser->yy_get_expected_tokens($this->parser->yymajor) as $token) {
127 $exp_token = $this->parser->yyTokenName[$token];
128 if (isset($this->lex->smarty_token_names[$exp_token])) {
129 // token type from lexer
130 $expect[] = '"' . $this->lex->smarty_token_names[$exp_token] . '"';
132 // otherwise internal token name
133 $expect[] = $this->parser->yyTokenName[$token];
136 // output parser error message
137 $error_text .= ' - Unexpected "' . $this->lex->value . '", expected one of: ' . implode(' , ', $expect);
139 throw new SmartyCompilerException($error_text);