Code

Updated smarty to 1.0.9
[gosa.git] / gosa-core / include / smarty / sysplugins / smarty_internal_config_file_compiler.php
1 <?php
3 /**
4  * Smarty Internal Plugin Config File Compiler
5  * 
6  * This is the config file compiler class. It calls the lexer and parser to
7  * perform the compiling.
8  * 
9  * @package Smarty
10  * @subpackage Config
11  * @author Uwe Tews 
12  */
13  
14 /**
15  * Main config file compiler class
16  */
17 class Smarty_Internal_Config_File_Compiler {
18     /**
19      * Initialize compiler
20      */
21     public function __construct($smarty)
22     {
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();
29     } 
31     /**
32      * Methode to compile a Smarty template
33      * 
34      * @param  $template template object to compile
35      * @return bool true if compiling succeeded, false if it failed
36      */
37     public function compileSource($config)
38     {
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 == '') {
47             return true;
48         } 
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);
57         } 
58         // finish parsing process
59         $parser->doParse(0, 0);
60         $config->compiled_config = '<?php $_config_vars = ' . var_export($this->config_data, true) . '; ?>';
61     } 
62     /**
63      * display compiler error messages without dying
64      * 
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.
67      * 
68      * If parameter $args contains a string this is used as error message
69      * 
70      * @todo output exact position of parse error in source line
71      * @param  $args string individual error message or null
72      */
73     public function trigger_config_file_error($args = null)
74     {
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;
79         if (isset($args)) {
80             // $line--;
81         } 
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]}' ";
84         if (isset($args)) {
85             // individual error message
86             $error_text .= $args;
87         } else {
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] . '"';
94                 } else {
95                     // otherwise internal token name
96                     $expect[] = $this->parser->yyTokenName[$token];
97                 } 
98             } 
99             // output parser error message
100             $error_text .= ' - Unexpected "' . $this->lex->value . '", expected one of: ' . implode(' , ', $expect);
101         } 
102         throw new SmartyCompilerException($error_text);
103     } 
104
106 ?>