Conditions | 13 |
Paths | 72 |
Total Lines | 216 |
Code Lines | 153 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
73 | public function parseString($string, array $vars = []) |
||
74 | { |
||
75 | if ($this->config[ 'allowPhpGlobals' ] === false) { |
||
76 | $string = str_replace( |
||
77 | [ |
||
78 | '{{$GLOBALS}}', |
||
79 | '{{$GLOBALS[%%]}}', |
||
80 | '{{$_SERVER}}', |
||
81 | '{{$_SERVER[%%]}}', |
||
82 | '{{$_GET}}', |
||
83 | '{{$_GET[%%]}}', |
||
84 | '{{$_POST}}', |
||
85 | '{{$_POST[%%]}}', |
||
86 | '{{$_FILES}}', |
||
87 | '{{$_FILES[%%]}}', |
||
88 | '{{$_COOKIE}}', |
||
89 | '{{$_COOKIE[%%]}}', |
||
90 | '{{$_SESSION}}', |
||
91 | '{{$_SESSION[%%]}}', |
||
92 | '{{$_REQUEST}}', |
||
93 | '{{$_REQUEST[%%]}}', |
||
94 | '{{$_ENV}}', |
||
95 | '{{$_ENV[%%]}}', |
||
96 | |||
97 | // with spaces |
||
98 | '{{ $GLOBALS }}', |
||
99 | '{{ $GLOBALS[%%] }}', |
||
100 | '{{ $_SERVER }}', |
||
101 | '{{ $_SERVER[%%] }}', |
||
102 | '{{ $_GET }}', |
||
103 | '{{ $_GET[%%] }}', |
||
104 | '{{ $_POST }}', |
||
105 | '{{ $_POST[%%] }}', |
||
106 | '{{ $_FILES }}', |
||
107 | '{{ $_FILES[%%] }}', |
||
108 | '{{ $_COOKIE }}', |
||
109 | '{{ $_COOKIE[%%] }}', |
||
110 | '{{ $_SESSION }}', |
||
111 | '{{ $_SESSION[%%] }}', |
||
112 | '{{ $_REQUEST }}', |
||
113 | '{{ $_REQUEST[%%] }}', |
||
114 | '{{ $_ENV }}', |
||
115 | '{{ $_ENV[%%] }}', |
||
116 | ], |
||
117 | '', |
||
118 | $string |
||
119 | ); |
||
120 | } |
||
121 | |||
122 | // php logical codes |
||
123 | $logicalCodes = [ |
||
124 | '{{if(%%)}}' => '<?php if(\1): ?>', |
||
125 | '{{elseif(%%)}}' => '<?php elseif(\1): ?>', |
||
126 | '{{/if}}' => '<?php endif; ?>', |
||
127 | '{{endif}}' => '<?php endif; ?>', |
||
128 | '{{else}}' => '<?php else: ?>', |
||
129 | '{{unless(%%)}}' => '<?php if(\1): ?>', |
||
130 | '{{endunless}}' => '<?php endif; ?>', |
||
131 | |||
132 | // with spaces |
||
133 | '{{ if(%%) }}' => '<?php if(\1): ?>', |
||
134 | '{{ elseif(%%) }}' => '<?php elseif(\1): ?>', |
||
135 | '{{ /if }}' => '<?php endif; ?>', |
||
136 | '{{ endif }}' => '<?php endif; ?>', |
||
137 | '{{ else }}' => '<?php else: ?>', |
||
138 | '{{ unless(%%) }}' => '<?php if(\1): ?>', |
||
139 | '{{ endunless }}' => '<?php endif; ?>', |
||
140 | ]; |
||
141 | |||
142 | // php loop codes |
||
143 | $loopCodes = [ |
||
144 | '{{for(%%)}}' => '<?php for(\1): ?>', |
||
145 | '{{/for}}' => '<?php endfor; ?>', |
||
146 | '{{endfor}}' => '<?php endfor; ?>', |
||
147 | '{{foreach(%%)}}' => '<?php foreach(\1): ?>', |
||
148 | '{{/foreach}}' => '<?php endforeach; ?>', |
||
149 | '{{endforeach}}' => '<?php endforeach; ?>', |
||
150 | '{{while(%%)}}' => '<?php while(\1): ?>', |
||
151 | '{{/while}}' => '<?php endwhile; ?>', |
||
152 | '{{endwhile}}' => '<?php endwhile; ?>', |
||
153 | '{{continue}}' => '<?php continue; ?>', |
||
154 | '{{break}}' => '<?php break; ?>', |
||
155 | |||
156 | // with spaces |
||
157 | '{{ for(%%) }}' => '<?php for(\1): ?>', |
||
158 | '{{ /for }}' => '<?php endfor; ?>', |
||
159 | '{{ endfor }}' => '<?php endfor; ?>', |
||
160 | '{{ foreach(%%) }}' => '<?php foreach(\1): ?>', |
||
161 | '{{ /foreach }}' => '<?php endforeach; ?>', |
||
162 | '{{ endforeach }}' => '<?php endforeach; ?>', |
||
163 | '{{ while(%%) }}' => '<?php while(\1): ?>', |
||
164 | '{{ /while }}' => '<?php endwhile; ?>', |
||
165 | '{{ endwhile }}' => '<?php endwhile; ?>', |
||
166 | '{{ continue }}' => '<?php continue; ?>', |
||
167 | '{{ break }}' => '<?php break; ?>', |
||
168 | ]; |
||
169 | |||
170 | // php function codes |
||
171 | $functionsCodes = []; |
||
172 | if ($this->config[ 'allowPhpFunctions' ] === false) { |
||
173 | $functionsCodes = [ |
||
174 | '{{%%(%%)}}' => '', |
||
175 | ]; |
||
176 | } elseif (is_array($this->config[ 'allowPhpFunctions' ]) AND count( |
||
177 | $this->config[ 'allowPhpFunctions' ] |
||
178 | ) > 0 |
||
179 | ) { |
||
180 | foreach ($this->config[ 'allowPhpFunctions' ] as $function_name) { |
||
181 | if (function_exists($function_name)) { |
||
182 | $functionsCodes[ '{{' . $function_name . '(%%)}}' ] = '<?php echo ' . $function_name . '(\1); ?>'; |
||
183 | } |
||
184 | } |
||
185 | } else { |
||
186 | $functionsCodes = [ |
||
187 | '{{%%()}}' => '<?php echo \1(); ?>', |
||
188 | '{{%%(%%)}}' => '<?php echo \1(\2); ?>', |
||
189 | '{{lang(%%)}}' => '<?php echo $language->getLine(\1); ?>', |
||
190 | '{{each(%%, %%, %%)}}' => '<?php echo $this->parsePartials(\1, \2, \3); ?>', |
||
191 | '{{include(%%)}}' => '<?php echo $this->parseFile(\1); ?>', |
||
192 | '{{include(%%, %%)}}' => '<?php echo $this->parseFile(\1, \2); ?>', |
||
193 | |||
194 | // with spaces |
||
195 | '{{ %%() }}' => '<?php echo \1(); ?>', |
||
196 | '{{ %%(%%) }}' => '<?php echo \1(\2); ?>', |
||
197 | '{{ lang(%%) }}' => '<?php echo $language->getLine(\1); ?>', |
||
198 | '{{ each(%%, %%, %%) }}' => '<?php echo $this->parsePartials(\1, \2, \3); ?>', |
||
199 | '{{ include(%%) }}' => '<?php echo $this->parseFile(\1); ?>', |
||
200 | '{{ include(%%, %%) }}' => '<?php echo $this->parseFile(\1, \2); ?>', |
||
201 | ]; |
||
202 | } |
||
203 | |||
204 | // php variables codes |
||
205 | $variablesCodes = [ |
||
206 | '{{%% ? %% : %%}}' => '<?php echo (\1 ? \2 : \3); ?>', |
||
207 | '{{%% or %%}}' => '<?php echo ( empty(\1) ? \2 : \1 ); ?>', |
||
208 | '{{%% || %%}}' => '<?php echo ( empty(\1) ? \2 : \1 ); ?>', |
||
209 | '{{$%%->%%(%%)}}' => '<?php echo $\1->\2(\3); ?>', |
||
210 | '{{$%%->%%}}' => '<?php echo @$\1->\2; ?>', |
||
211 | '{{$%%[%%]}}' => '<?php echo @$\1[\2]; ?>', |
||
212 | '{{$%%.%%}}' => '<?php echo @$\1[\2]; ?>', |
||
213 | '{{$%% = %%}}' => '<?php $\1 = \2; ?>', |
||
214 | '{{$%%++}}' => '<?php $\1++; ?>', |
||
215 | '{{$%%--}}' => '<?php $\1--; ?>', |
||
216 | '{{$%%}}' => '<?php echo (isset($\1) ? $\1 : ""); ?>', |
||
217 | '{{/*}}' => '<?php /*', |
||
218 | '{{*/}}' => '*/ ?>', |
||
219 | '{{!!$%%!!}}' => '<?php echo htmlentities($\1, ENT_HTML5); ?>', |
||
220 | '{{--%%--}}' => '', |
||
221 | |||
222 | // with spaces |
||
223 | '{{ %% ? %% : %% }}' => '<?php echo (\1 ? \2 : \3); ?>', |
||
224 | '{{ %% or %% }}' => '<?php echo ( empty(\1) ? \'\2\' : \1 ); ?>', |
||
225 | '{{ %% || %% }}' => '<?php echo ( empty(\1) ? \'\2\' : \1 ); ?>', |
||
226 | '{{ $%%->%%(%%) }}' => '<?php echo $\1->\2(\3); ?>', |
||
227 | '{{ $%%->%% }}' => '<?php echo $\1->\2; ?>', |
||
228 | '{{ $%%[%%] }}' => '<?php echo $\1->\2; ?>', |
||
229 | '{{ $%%.%% }}' => '<?php echo $\1->\2; ?>', |
||
230 | '{{ $%% = %% }}' => '<?php $\1 = \2; ?>', |
||
231 | '{{ $%%++ }}' => '<?php $\1++; ?>', |
||
232 | '{{ $%%-- }}' => '<?php $\1--; ?>', |
||
233 | '{{ $%% }}' => '<?php echo (isset($\1) ? $\1 : ""); ?>', |
||
234 | '{{ /* }}' => '<?php /*', |
||
235 | '{{ */ }}' => '*/ ?>', |
||
236 | '{{ !!$%%!! }}' => '<?php echo htmlentities($\1, ENT_HTML5); ?>', |
||
237 | '{{ --%%-- }}' => '', |
||
238 | ]; |
||
239 | |||
240 | if ($this->config[ 'allowPhpConstants' ] === true) { |
||
241 | $constantsVariables = get_defined_constants(true); |
||
242 | |||
243 | if ( ! empty($constantsVariables[ 'user' ])) { |
||
244 | foreach ($constantsVariables[ 'user' ] as $constant => $value) { |
||
245 | if (defined($constant)) { |
||
246 | $variablesCodes[ '{{' . $constant . '}}' ] = '<?php echo ' . $constant . '; ?>'; |
||
247 | } |
||
248 | } |
||
249 | } |
||
250 | } |
||
251 | |||
252 | $phpCodes = array_merge($logicalCodes, $loopCodes, $variablesCodes, $functionsCodes); |
||
253 | |||
254 | $patterns = $replace = []; |
||
255 | foreach ($phpCodes as $tplCode => $phpCode) { |
||
256 | $patterns[] = '#' . str_replace('%%', '(.+)', preg_quote($tplCode, '#')) . '#U'; |
||
257 | $replace[] = $phpCode; |
||
258 | } |
||
259 | |||
260 | /*replace our pseudo language in template with php code*/ |
||
261 | $string = preg_replace($patterns, $replace, $string); |
||
262 | |||
263 | extract($vars); |
||
264 | |||
265 | /* |
||
266 | * Buffer the output |
||
267 | * |
||
268 | * We buffer the output for two reasons: |
||
269 | * 1. Speed. You get a significant speed boost. |
||
270 | * 2. So that the final rendered template can be post-processed by |
||
271 | * the output class. Why do we need post processing? For one thing, |
||
272 | * in order to show the elapsed page load time. Unless we can |
||
273 | * intercept the content right before it's sent to the browser and |
||
274 | * then stop the timer it won't be accurate. |
||
275 | */ |
||
276 | ob_start(); |
||
277 | |||
278 | try { |
||
279 | echo @eval('?>' . @preg_replace('/;*\s*\?>/', '; ?>', $string)); |
||
|
|||
280 | } catch (\Exception $e) { |
||
281 | throw new \Exception($e->getMessage(), $e->getCode(), $e); |
||
282 | |||
283 | } |
||
284 | |||
285 | $output = ob_get_contents(); |
||
286 | @ob_end_clean(); |
||
287 | |||
288 | return $output; |
||
289 | } |
||
290 | } |