Code

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