]> git.mxchange.org Git - friendica.git/blob - vendor/smarty/smarty/libs/sysplugins/smarty_internal_config_file_compiler.php
Add Smarty to Composer
[friendica.git] / vendor / smarty / 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 class name
22      *
23      * @var string
24      */
25     public $lexer_class;
26
27     /**
28      * Parser class name
29      *
30      * @var string
31      */
32     public $parser_class;
33
34     /**
35      * Lexer object
36      *
37      * @var object
38      */
39     public $lex;
40
41     /**
42      * Parser object
43      *
44      * @var object
45      */
46     public $parser;
47
48     /**
49      * Smarty object
50      *
51      * @var Smarty object
52      */
53     public $smarty;
54
55     /**
56      * Smarty object
57      *
58      * @var Smarty_Internal_Template object
59      */
60     public $template;
61
62     /**
63      * Compiled config data sections and variables
64      *
65      * @var array
66      */
67     public $config_data = array();
68
69     /**
70      * compiled config data must always be written
71      *
72      * @var bool
73      */
74     public $write_compiled_code = true;
75
76     /**
77      * Initialize compiler
78      *
79      * @param string $lexer_class  class name
80      * @param string $parser_class class name
81      * @param Smarty $smarty       global instance
82      */
83     public function __construct($lexer_class, $parser_class, Smarty $smarty)
84     {
85         $this->smarty = $smarty;
86         // get required plugins
87         $this->lexer_class = $lexer_class;
88         $this->parser_class = $parser_class;
89         $this->smarty = $smarty;
90         $this->config_data[ 'sections' ] = array();
91         $this->config_data[ 'vars' ] = array();
92     }
93
94     /**
95      * Method to compile Smarty config source.
96      *
97      * @param Smarty_Internal_Template $template
98      *
99      * @return bool true if compiling succeeded, false if it failed
100      */
101     public function compileTemplate(Smarty_Internal_Template $template)
102     {
103         $this->template = $template;
104         $this->template->compiled->file_dependency[ $this->template->source->uid ] =
105             array($this->template->source->filepath, $this->template->source->getTimeStamp(),
106                   $this->template->source->type);
107         if ($this->smarty->debugging) {
108             if (!isset( $this->smarty->_debug)) {
109                 $this->smarty->_debug  = new Smarty_Internal_Debug();
110             }
111             $this->smarty->_debug->start_compile($this->template);
112         }
113         // init the lexer/parser to compile the config file
114         /* @var Smarty_Internal_ConfigFileLexer $this->lex */
115         $this->lex = new $this->lexer_class(str_replace(array("\r\n", "\r"), "\n", $template->source->getContent()) . "\n",
116                                       $this);
117         /* @var Smarty_Internal_ConfigFileParser $this->parser */
118         $this->parser = new $this->parser_class($this->lex, $this);
119
120         if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) {
121             $mbEncoding = mb_internal_encoding();
122             mb_internal_encoding('ASCII');
123         } else {
124             $mbEncoding = null;
125         }
126
127         if ($this->smarty->_parserdebug) {
128             $this->parser->PrintTrace();
129         }
130         // get tokens from lexer and parse them
131         while ($this->lex->yylex()) {
132             if ($this->smarty->_parserdebug) {
133                 echo "<br>Parsing  {$this->parser->yyTokenName[$this->lex->token]} Token {$this->lex->value} Line {$this->lex->line} \n";
134             }
135             $this->parser->doParse($this->lex->token, $this->lex->value);
136         }
137         // finish parsing process
138         $this->parser->doParse(0, 0);
139
140         if ($mbEncoding) {
141             mb_internal_encoding($mbEncoding);
142         }
143         if ($this->smarty->debugging) {
144             $this->smarty->_debug->end_compile($this->template);
145         }
146         // template header code
147         $template_header =
148             "<?php /* Smarty version " . Smarty::SMARTY_VERSION . ", created on " . strftime("%Y-%m-%d %H:%M:%S") .
149             "\n";
150         $template_header .= "         compiled from \"" . $this->template->source->filepath . "\" */ ?>\n";
151
152         $code = '<?php $_smarty_tpl->smarty->ext->configLoad->_loadConfigVars($_smarty_tpl, ' .
153                 var_export($this->config_data, true) . '); ?>';
154         return $template_header . $this->template->smarty->ext->_codeFrame->create($this->template, $code);
155     }
156
157     /**
158      * display compiler error messages without dying
159      * If parameter $args is empty it is a parser detected syntax error.
160      * In this case the parser is called to obtain information about expected tokens.
161      * If parameter $args contains a string this is used as error message
162      *
163      * @param string $args individual error message or null
164      *
165      * @throws SmartyCompilerException
166      */
167     public function trigger_config_file_error($args = null)
168     {
169         // get config source line which has error
170         $line = $this->lex->line;
171         if (isset($args)) {
172             // $line--;
173         }
174         $match = preg_split("/\n/", $this->lex->data);
175         $error_text =
176             "Syntax error in config file '{$this->template->source->filepath}' on line {$line} '{$match[$line - 1]}' ";
177         if (isset($args)) {
178             // individual error message
179             $error_text .= $args;
180         } else {
181             // expected token from parser
182             foreach ($this->parser->yy_get_expected_tokens($this->parser->yymajor) as $token) {
183                 $exp_token = $this->parser->yyTokenName[ $token ];
184                 if (isset($this->lex->smarty_token_names[ $exp_token ])) {
185                     // token type from lexer
186                     $expect[] = '"' . $this->lex->smarty_token_names[ $exp_token ] . '"';
187                 } else {
188                     // otherwise internal token name
189                     $expect[] = $this->parser->yyTokenName[ $token ];
190                 }
191             }
192             // output parser error message
193             $error_text .= ' - Unexpected "' . $this->lex->value . '", expected one of: ' . implode(' , ', $expect);
194         }
195         throw new SmartyCompilerException($error_text);
196     }
197 }