1: | <?php
|
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: |
|
11: |
|
12: | |
13: | |
14: | |
15: | |
16: | |
17: |
|
18: | class Smarty_Internal_Config_File_Compiler
|
19: | {
|
20: | |
21: | |
22: | |
23: | |
24: |
|
25: | public $lexer_class;
|
26: |
|
27: | |
28: | |
29: | |
30: | |
31: |
|
32: | public $parser_class;
|
33: |
|
34: | |
35: | |
36: | |
37: | |
38: |
|
39: | public $lex;
|
40: |
|
41: | |
42: | |
43: | |
44: | |
45: |
|
46: | public $parser;
|
47: |
|
48: | |
49: | |
50: | |
51: | |
52: |
|
53: | public $smarty;
|
54: |
|
55: | |
56: | |
57: | |
58: | |
59: |
|
60: | public $template;
|
61: |
|
62: | |
63: | |
64: | |
65: | |
66: |
|
67: | public $config_data = array();
|
68: |
|
69: | |
70: | |
71: | |
72: | |
73: |
|
74: | public $write_compiled_code = true;
|
75: |
|
76: | |
77: | |
78: | |
79: | |
80: | |
81: | |
82: |
|
83: | public function __construct($lexer_class, $parser_class, Smarty $smarty)
|
84: | {
|
85: | $this->smarty = $smarty;
|
86: |
|
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: | |
96: | |
97: | |
98: | |
99: | |
100: | |
101: |
|
102: | public function compileTemplate(Smarty_Internal_Template $template)
|
103: | {
|
104: | $this->template = $template;
|
105: | $this->template->compiled->file_dependency[ $this->template->source->uid ] =
|
106: | array(
|
107: | $this->template->source->filepath,
|
108: | $this->template->source->getTimeStamp(),
|
109: | $this->template->source->type
|
110: | );
|
111: | if ($this->smarty->debugging) {
|
112: | if (!isset($this->smarty->_debug)) {
|
113: | $this->smarty->_debug = new Smarty_Internal_Debug();
|
114: | }
|
115: | $this->smarty->_debug->start_compile($this->template);
|
116: | }
|
117: |
|
118: |
|
119: | $this->lex = new $this->lexer_class(
|
120: | str_replace(
|
121: | array(
|
122: | "\r\n",
|
123: | "\r"
|
124: | ),
|
125: | "\n",
|
126: | $template->source->getContent()
|
127: | ) . "\n",
|
128: | $this
|
129: | );
|
130: |
|
131: | $this->parser = new $this->parser_class($this->lex, $this);
|
132: | if (function_exists('mb_internal_encoding')
|
133: | && function_exists('ini_get')
|
134: | && ((int)ini_get('mbstring.func_overload')) & 2
|
135: | ) {
|
136: | $mbEncoding = mb_internal_encoding();
|
137: | mb_internal_encoding('ASCII');
|
138: | } else {
|
139: | $mbEncoding = null;
|
140: | }
|
141: | if ($this->smarty->_parserdebug) {
|
142: | $this->parser->PrintTrace();
|
143: | }
|
144: |
|
145: | while ($this->lex->yylex()) {
|
146: | if ($this->smarty->_parserdebug) {
|
147: | echo "<br>Parsing {$this->parser->yyTokenName[$this->lex->token]} Token {$this->lex->value} Line {$this->lex->line} \n";
|
148: | }
|
149: | $this->parser->doParse($this->lex->token, $this->lex->value);
|
150: | }
|
151: |
|
152: | $this->parser->doParse(0, 0);
|
153: | if ($mbEncoding) {
|
154: | mb_internal_encoding($mbEncoding);
|
155: | }
|
156: | if ($this->smarty->debugging) {
|
157: | $this->smarty->_debug->end_compile($this->template);
|
158: | }
|
159: |
|
160: | $template_header = sprintf(
|
161: | "<?php /* Smarty version %s, created on %s\n compiled from '%s' */ ?>\n",
|
162: | Smarty::SMARTY_VERSION,
|
163: | date("Y-m-d H:i:s"),
|
164: | str_replace('*/', '* /' , $this->template->source->filepath)
|
165: | );
|
166: | $code = '<?php $_smarty_tpl->smarty->ext->configLoad->_loadConfigVars($_smarty_tpl, ' .
|
167: | var_export($this->config_data, true) . '); ?>';
|
168: | return $template_header . $this->template->smarty->ext->_codeFrame->create($this->template, $code);
|
169: | }
|
170: |
|
171: | |
172: | |
173: | |
174: | |
175: | |
176: | |
177: | |
178: | |
179: | |
180: |
|
181: | public function trigger_config_file_error($args = null)
|
182: | {
|
183: |
|
184: | $line = $this->lex->line;
|
185: | if (isset($args)) {
|
186: |
|
187: | }
|
188: | $match = preg_split("/\n/", $this->lex->data);
|
189: | $error_text =
|
190: | "Syntax error in config file '{$this->template->source->filepath}' on line {$line} '{$match[$line - 1]}' ";
|
191: | if (isset($args)) {
|
192: |
|
193: | $error_text .= $args;
|
194: | } else {
|
195: |
|
196: | foreach ($this->parser->yy_get_expected_tokens($this->parser->yymajor) as $token) {
|
197: | $exp_token = $this->parser->yyTokenName[ $token ];
|
198: | if (isset($this->lex->smarty_token_names[ $exp_token ])) {
|
199: |
|
200: | $expect[] = '"' . $this->lex->smarty_token_names[ $exp_token ] . '"';
|
201: | } else {
|
202: |
|
203: | $expect[] = $this->parser->yyTokenName[ $token ];
|
204: | }
|
205: | }
|
206: |
|
207: | $error_text .= ' - Unexpected "' . $this->lex->value . '", expected one of: ' . implode(' , ', $expect);
|
208: | }
|
209: | throw new SmartyCompilerException($error_text);
|
210: | }
|
211: | }
|
212: | |