]> git.mxchange.org Git - friendica.git/blob - library/Smarty/libs/sysplugins/smarty_internal_config_file_compiler.php
Changes in api
[friendica.git] / library / Smarty / libs / sysplugins / smarty_internal_config_file_compiler.php
1 <?php
2 /**
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.
6  *
7  * @package    Smarty
8  * @subpackage Config
9  * @author     Uwe Tews
10  */
11
12 /**
13  * Main config file compiler class
14  *
15  * @package    Smarty
16  * @subpackage Config
17  */
18 class Smarty_Internal_Config_File_Compiler
19 {
20     /**
21      * Lexer object
22      *
23      * @var object
24      */
25     public $lex;
26
27     /**
28      * Parser object
29      *
30      * @var object
31      */
32     public $parser;
33
34     /**
35      * Smarty object
36      *
37      * @var Smarty object
38      */
39     public $smarty;
40
41     /**
42      * Smarty object
43      *
44      * @var Smarty_Internal_Config object
45      */
46     public $config;
47
48     /**
49      * Compiled config data sections and variables
50      *
51      * @var array
52      */
53     public $config_data = array();
54
55     /**
56      * Initialize compiler
57      *
58      * @param Smarty $smarty base instance
59      */
60     public function __construct($smarty)
61     {
62         $this->smarty = $smarty;
63         $this->config_data['sections'] = array();
64         $this->config_data['vars'] = array();
65     }
66
67     /**
68      * Method to compile a Smarty template.
69      *
70      * @param  Smarty_Internal_Config $config config object
71      *
72      * @return bool                   true if compiling succeeded, false if it failed
73      */
74     public function compileSource(Smarty_Internal_Config $config)
75     {
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 == '') {
84             return true;
85         }
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);
89
90         if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) {
91             $mbEncoding = mb_internal_encoding();
92             mb_internal_encoding('ASCII');
93         } else {
94             $mbEncoding = null;
95         }
96
97
98         if ($this->smarty->_parserdebug) {
99             $parser->PrintTrace();
100         }
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";
105             }
106             $parser->doParse($lex->token, $lex->value);
107         }
108         // finish parsing process
109         $parser->doParse(0, 0);
110
111         if ($mbEncoding) {
112             mb_internal_encoding($mbEncoding);
113         }
114
115         $config->compiled_config = '<?php $_config_vars = ' . var_export($this->config_data, true) . '; ?>';
116     }
117
118     /**
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
123      *
124      * @param string $args individual error message or null
125      *
126      * @throws SmartyCompilerException
127      */
128     public function trigger_config_file_error($args = null)
129     {
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;
134         if (isset($args)) {
135             // $line--;
136         }
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]}' ";
139         if (isset($args)) {
140             // individual error message
141             $error_text .= $args;
142         } else {
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] . '"';
149                 } else {
150                     // otherwise internal token name
151                     $expect[] = $this->parser->yyTokenName[$token];
152                 }
153             }
154             // output parser error message
155             $error_text .= ' - Unexpected "' . $this->lex->value . '", expected one of: ' . implode(' , ', $expect);
156         }
157         throw new SmartyCompilerException($error_text);
158     }
159 }