4  * Smarty Internal Plugin Config File Compiler
 
   6  * This is the config file compiler class. It calls the lexer and parser to
 
   7  * perform the compiling.
 
  15  * Main config file compiler class
 
  17 class Smarty_Internal_Config_File_Compiler {
 
  21     public function __construct($smarty)
 
  23         $this->smarty = $smarty; 
 
  24         // get required plugins
 
  25         $this->smarty->loadPlugin('Smarty_Internal_Configfilelexer');
 
  26                 $this->smarty->loadPlugin('Smarty_Internal_Configfileparser');
 
  27         $this->config_data['sections'] = array();
 
  28         $this->config_data['vars'] = array();
 
  32      * Methode to compile a Smarty template
 
  34      * @param  $template template object to compile
 
  35      * @return bool true if compiling succeeded, false if it failed
 
  37     public function compileSource($config)
 
  39         /* here is where the compiling takes place. Smarty
 
  40        tags in the templates are replaces with PHP code,
 
  41        then written to compiled files. */
 
  42         $this->config = $config; 
 
  43         // get config file source
 
  44         $_content = $config->getConfigSource() . "\n"; 
 
  45         // on empty template just return
 
  46         if ($_content == '') {
 
  49         // init the lexer/parser to compile the config file
 
  50         $lex = new Smarty_Internal_Configfilelexer($_content, $this->smarty);
 
  51         $parser = new Smarty_Internal_Configfileparser($lex, $this);
 
  52         if (isset($this->smarty->_parserdebug)) $parser->PrintTrace(); 
 
  53         // get tokens from lexer and parse them
 
  54         while ($lex->yylex()) {
 
  55             if (isset($this->smarty->_parserdebug)) echo "<br>Parsing  {$parser->yyTokenName[$lex->token]} Token {$lex->value} Line {$lex->line} \n";
 
  56             $parser->doParse($lex->token, $lex->value);
 
  58         // finish parsing process
 
  59         $parser->doParse(0, 0);
 
  60         $config->compiled_config = '<?php $_config_vars = ' . var_export($this->config_data, true) . '; ?>';
 
  63      * display compiler error messages without dying
 
  65      * If parameter $args is empty it is a parser detected syntax error.
 
  66      * In this case the parser is called to obtain information about exspected tokens.
 
  68      * If parameter $args contains a string this is used as error message
 
  70      * @todo output exact position of parse error in source line
 
  71      * @param  $args string individual error message or null
 
  73     public function trigger_config_file_error($args = null)
 
  75         $this->lex = Smarty_Internal_Configfilelexer::instance();
 
  76         $this->parser = Smarty_Internal_Configfileparser::instance(); 
 
  77         // get template source line which has error
 
  78         $line = $this->lex->line;
 
  82         $match = preg_split("/\n/", $this->lex->data);
 
  83         $error_text = "Syntax error in config file '{$this->config->getConfigFilepath()}' on line {$line} '{$match[$line-1]}' ";
 
  85             // individual error message
 
  88             // exspected token from parser
 
  89             foreach ($this->parser->yy_get_expected_tokens($this->parser->yymajor) as $token) {
 
  90                 $exp_token = $this->parser->yyTokenName[$token];
 
  91                 if (isset($this->lex->smarty_token_names[$exp_token])) {
 
  92                     // token type from lexer
 
  93                     $expect[] = '"' . $this->lex->smarty_token_names[$exp_token] . '"';
 
  95                     // otherwise internal token name
 
  96                     $expect[] = $this->parser->yyTokenName[$token];
 
  99             // output parser error message
 
 100             $error_text .= ' - Unexpected "' . $this->lex->value . '", expected one of: ' . implode(' , ', $expect);
 
 102         throw new SmartyCompilerException($error_text);