Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Compiler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Compiler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class Compiler implements ICompiler |
||
33 | { |
||
34 | /** |
||
35 | * Constant that represents a php opening tag. |
||
36 | * use it in case it needs to be adjusted |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | const PHP_OPEN = '<?php '; |
||
41 | |||
42 | /** |
||
43 | * Constant that represents a php closing tag. |
||
44 | * use it in case it needs to be adjusted |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | const PHP_CLOSE = '?>'; |
||
49 | |||
50 | /** |
||
51 | * Boolean flag to enable or disable debugging output. |
||
52 | * |
||
53 | * @var bool |
||
54 | */ |
||
55 | public $debug = false; |
||
56 | |||
57 | /** |
||
58 | * Left script delimiter. |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | protected $ld = '{'; |
||
63 | |||
64 | /** |
||
65 | * Left script delimiter with escaped regex meta characters. |
||
66 | * |
||
67 | * @var string |
||
68 | */ |
||
69 | protected $ldr = '\\{'; |
||
70 | |||
71 | /** |
||
72 | * Right script delimiter. |
||
73 | * |
||
74 | * @var string |
||
75 | */ |
||
76 | protected $rd = '}'; |
||
77 | |||
78 | /** |
||
79 | * Right script delimiter with escaped regex meta characters. |
||
80 | * |
||
81 | * @var string |
||
82 | */ |
||
83 | protected $rdr = '\\}'; |
||
84 | |||
85 | /** |
||
86 | * Defines whether the nested comments should be parsed as nested or not. |
||
87 | * defaults to false (classic block comment parsing as in all languages) |
||
88 | * |
||
89 | * @var bool |
||
90 | */ |
||
91 | protected $allowNestedComments = false; |
||
92 | |||
93 | /** |
||
94 | * Defines whether opening and closing tags can contain spaces before valid data or not. |
||
95 | * turn to true if you want to be sloppy with the syntax, but when set to false it allows |
||
96 | * to skip javascript and css tags as long as they are in the form "{ something", which is |
||
97 | * nice. default is false. |
||
98 | * |
||
99 | * @var bool |
||
100 | */ |
||
101 | protected $allowLooseOpenings = false; |
||
102 | |||
103 | /** |
||
104 | * Defines whether the compiler will automatically html-escape variables or not. |
||
105 | * default is false |
||
106 | * |
||
107 | * @var bool |
||
108 | */ |
||
109 | protected $autoEscape = false; |
||
110 | |||
111 | /** |
||
112 | * Security policy object. |
||
113 | * |
||
114 | * @var SecurityPolicy |
||
115 | */ |
||
116 | protected $securityPolicy; |
||
117 | |||
118 | /** |
||
119 | * Stores the custom plugins registered with this compiler. |
||
120 | * |
||
121 | * @var array |
||
122 | */ |
||
123 | protected $customPlugins = array(); |
||
124 | |||
125 | /** |
||
126 | * Stores the template plugins registered with this compiler. |
||
127 | * |
||
128 | * @var array |
||
129 | */ |
||
130 | protected $templatePlugins = array(); |
||
131 | |||
132 | /** |
||
133 | * Stores the pre- and post-processors callbacks. |
||
134 | * |
||
135 | * @var array |
||
136 | */ |
||
137 | protected $processors = array('pre' => array(), 'post' => array()); |
||
138 | |||
139 | /** |
||
140 | * Stores a list of plugins that are used in the currently compiled |
||
141 | * template, and that are not compilable. these plugins will be loaded |
||
142 | * during the template's runtime if required. |
||
143 | * it is a 1D array formatted as key:pluginName value:pluginType |
||
144 | * |
||
145 | * @var array |
||
146 | */ |
||
147 | protected $usedPlugins; |
||
148 | |||
149 | /** |
||
150 | * Stores the template undergoing compilation. |
||
151 | * |
||
152 | * @var string |
||
153 | */ |
||
154 | protected $template; |
||
155 | |||
156 | /** |
||
157 | * Stores the current pointer position inside the template. |
||
158 | * |
||
159 | * @var int |
||
160 | */ |
||
161 | protected $pointer; |
||
162 | |||
163 | /** |
||
164 | * Stores the current line count inside the template for debugging purposes. |
||
165 | * |
||
166 | * @var int |
||
167 | */ |
||
168 | protected $line; |
||
169 | |||
170 | /** |
||
171 | * Stores the current template source while compiling it. |
||
172 | * |
||
173 | * @var string |
||
174 | */ |
||
175 | protected $templateSource; |
||
176 | |||
177 | /** |
||
178 | * Stores the data within which the scope moves. |
||
179 | * |
||
180 | * @var array |
||
181 | */ |
||
182 | protected $data; |
||
183 | |||
184 | /** |
||
185 | * Variable scope of the compiler, set to null if |
||
186 | * it can not be resolved to a static string (i.e. if some |
||
187 | * plugin defines a new scope based on a variable array key). |
||
188 | * |
||
189 | * @var mixed |
||
190 | */ |
||
191 | protected $scope; |
||
192 | |||
193 | /** |
||
194 | * Variable scope tree, that allows to rebuild the current |
||
195 | * scope if required, i.e. when going to a parent level. |
||
196 | * |
||
197 | * @var array |
||
198 | */ |
||
199 | protected $scopeTree; |
||
200 | |||
201 | /** |
||
202 | * Block plugins stack, accessible through some methods. |
||
203 | * |
||
204 | * @see findBlock |
||
205 | * @see getCurrentBlock |
||
206 | * @see addBlock |
||
207 | * @see addCustomBlock |
||
208 | * @see injectBlock |
||
209 | * @see removeBlock |
||
210 | * @see removeTopBlock |
||
211 | * @var array |
||
212 | */ |
||
213 | protected $stack = array(); |
||
214 | |||
215 | /** |
||
216 | * Current block at the top of the block plugins stack, |
||
217 | * accessible through getCurrentBlock. |
||
218 | * |
||
219 | * @see getCurrentBlock |
||
220 | * @var array |
||
221 | */ |
||
222 | protected $curBlock; |
||
223 | |||
224 | /** |
||
225 | * Current dwoo object that uses this compiler, or null. |
||
226 | * |
||
227 | * @var Core |
||
228 | */ |
||
229 | public $core; |
||
230 | |||
231 | /** |
||
232 | * Holds an instance of this class, used by getInstance when you don't |
||
233 | * provide a custom compiler in order to save resources. |
||
234 | * |
||
235 | * @var Compiler |
||
236 | */ |
||
237 | protected static $instance; |
||
238 | |||
239 | /** |
||
240 | * Token types. |
||
241 | * |
||
242 | * @var int |
||
243 | */ |
||
244 | const T_UNQUOTED_STRING = 1; |
||
245 | const T_NUMERIC = 2; |
||
246 | const T_NULL = 4; |
||
247 | const T_BOOL = 8; |
||
248 | const T_MATH = 16; |
||
249 | const T_BREAKCHAR = 32; |
||
250 | |||
251 | /** |
||
252 | * Compiler constructor. |
||
253 | * saves the created instance so that child templates get the same one |
||
254 | */ |
||
255 | public function __construct() |
||
259 | |||
260 | /** |
||
261 | * Sets the delimiters to use in the templates. |
||
262 | * delimiters can be multi-character strings but should not be one of those as they will |
||
263 | * make it very hard to work with templates or might even break the compiler entirely : "\", "$", "|", ":" and |
||
264 | * finally "#" only if you intend to use config-vars with the #var# syntax. |
||
265 | * |
||
266 | * @param string $left left delimiter |
||
267 | * @param string $right right delimiter |
||
268 | */ |
||
269 | public function setDelimiters($left, $right) |
||
276 | |||
277 | /** |
||
278 | * Returns the left and right template delimiters. |
||
279 | * |
||
280 | * @return array containing the left and the right delimiters |
||
281 | */ |
||
282 | public function getDelimiters() |
||
286 | |||
287 | /** |
||
288 | * Sets the way to handle nested comments, if set to true |
||
289 | * {* foo {* some other *} comment *} will be stripped correctly. |
||
290 | * if false it will remove {* foo {* some other *} and leave "comment *}" alone, |
||
291 | * this is the default behavior |
||
292 | * |
||
293 | * @param bool $allow allow nested comments or not, defaults to true (but the default internal value is false) |
||
294 | */ |
||
295 | public function setNestedCommentsHandling($allow = true) |
||
299 | |||
300 | /** |
||
301 | * Returns the nested comments handling setting. |
||
302 | * |
||
303 | * @see setNestedCommentsHandling |
||
304 | * @return bool true if nested comments are allowed |
||
305 | */ |
||
306 | public function getNestedCommentsHandling() |
||
310 | |||
311 | /** |
||
312 | * Sets the tag openings handling strictness, if set to true, template tags can |
||
313 | * contain spaces before the first function/string/variable such as { $foo} is valid. |
||
314 | * if set to false (default setting), { $foo} is invalid but that is however a good thing |
||
315 | * as it allows css (i.e. #foo { color:red; }) to be parsed silently without triggering |
||
316 | * an error, same goes for javascript. |
||
317 | * |
||
318 | * @param bool $allow true to allow loose handling, false to restore default setting |
||
319 | */ |
||
320 | public function setLooseOpeningHandling($allow = false) |
||
324 | |||
325 | /** |
||
326 | * Returns the tag openings handling strictness setting. |
||
327 | * |
||
328 | * @see setLooseOpeningHandling |
||
329 | * @return bool true if loose tags are allowed |
||
330 | */ |
||
331 | public function getLooseOpeningHandling() |
||
335 | |||
336 | /** |
||
337 | * Changes the auto escape setting. |
||
338 | * if enabled, the compiler will automatically html-escape variables, |
||
339 | * unless they are passed through the safe function such as {$var|safe} |
||
340 | * or {safe $var} |
||
341 | * default setting is disabled/false |
||
342 | * |
||
343 | * @param bool $enabled set to true to enable, false to disable |
||
344 | */ |
||
345 | public function setAutoEscape($enabled) |
||
349 | |||
350 | /** |
||
351 | * Returns the auto escape setting. |
||
352 | * default setting is disabled/false |
||
353 | * |
||
354 | * @return bool |
||
355 | */ |
||
356 | public function getAutoEscape() |
||
360 | |||
361 | /** |
||
362 | * Adds a preprocessor to the compiler, it will be called |
||
363 | * before the template is compiled. |
||
364 | * |
||
365 | * @param mixed $callback either a valid callback to the preprocessor or a simple name if the autoload is set to |
||
366 | * true |
||
367 | * @param bool $autoload if set to true, the preprocessor is auto-loaded from one of the plugin directories, else |
||
368 | * you must provide a valid callback |
||
369 | */ |
||
370 | View Code Duplication | public function addPreProcessor($callback, $autoload = false) |
|
389 | |||
390 | /** |
||
391 | * Removes a preprocessor from the compiler. |
||
392 | * |
||
393 | * @param mixed $callback either a valid callback to the preprocessor or a simple name if it was autoloaded |
||
394 | */ |
||
395 | View Code Duplication | public function removePreProcessor($callback) |
|
413 | |||
414 | /** |
||
415 | * Adds a postprocessor to the compiler, it will be called |
||
416 | * before the template is compiled. |
||
417 | * |
||
418 | * @param mixed $callback either a valid callback to the postprocessor or a simple name if the autoload is set to |
||
419 | * true |
||
420 | * @param bool $autoload if set to true, the postprocessor is auto-loaded from one of the plugin directories, else |
||
421 | * you must provide a valid callback |
||
422 | */ |
||
423 | View Code Duplication | public function addPostProcessor($callback, $autoload = false) |
|
442 | |||
443 | /** |
||
444 | * Removes a postprocessor from the compiler. |
||
445 | * |
||
446 | * @param mixed $callback either a valid callback to the postprocessor or a simple name if it was autoloaded |
||
447 | */ |
||
448 | View Code Duplication | public function removePostProcessor($callback) |
|
466 | |||
467 | /** |
||
468 | * Internal function to autoload processors at runtime if required. |
||
469 | * |
||
470 | * @param string $class the class/function name |
||
471 | * @param string $name the plugin name (without Dwoo_Plugin_ prefix) |
||
472 | * |
||
473 | * @return array|string |
||
474 | * @throws Exception |
||
475 | */ |
||
476 | protected function loadProcessor($class, $name) |
||
497 | |||
498 | /** |
||
499 | * Adds an used plugin, this is reserved for use by the {template} plugin. |
||
500 | * this is required so that plugin loading bubbles up from loaded |
||
501 | * template files to the current one |
||
502 | * |
||
503 | * @private |
||
504 | * |
||
505 | * @param string $name function name |
||
506 | * @param int $type plugin type (Core::*_PLUGIN) |
||
507 | */ |
||
508 | public function addUsedPlugin($name, $type) |
||
512 | |||
513 | /** |
||
514 | * Returns all the plugins this template uses. |
||
515 | * |
||
516 | * @private |
||
517 | * @return array the list of used plugins in the parsed template |
||
518 | */ |
||
519 | public function getUsedPlugins() |
||
523 | |||
524 | /** |
||
525 | * Adds a template plugin, this is reserved for use by the {template} plugin. |
||
526 | * this is required because the template functions are not declared yet |
||
527 | * during compilation, so we must have a way of validating their argument |
||
528 | * signature without using the reflection api |
||
529 | * |
||
530 | * @private |
||
531 | * |
||
532 | * @param string $name function name |
||
533 | * @param array $params parameter array to help validate the function call |
||
534 | * @param string $uuid unique id of the function |
||
535 | * @param string $body function php code |
||
536 | */ |
||
537 | public function addTemplatePlugin($name, array $params, $uuid, $body = null) |
||
541 | |||
542 | /** |
||
543 | * Returns all the parsed sub-templates. |
||
544 | * |
||
545 | * @private |
||
546 | * @return array the parsed sub-templates |
||
547 | */ |
||
548 | public function getTemplatePlugins() |
||
552 | |||
553 | /** |
||
554 | * Marks a template plugin as being called, which means its source must be included in the compiled template. |
||
555 | * |
||
556 | * @param string $name function name |
||
557 | */ |
||
558 | public function useTemplatePlugin($name) |
||
562 | |||
563 | /** |
||
564 | * Adds the custom plugins loaded into Dwoo to the compiler so it can load them. |
||
565 | * |
||
566 | * @see Core::addPlugin |
||
567 | * |
||
568 | * @param array $customPlugins an array of custom plugins |
||
569 | */ |
||
570 | public function setCustomPlugins(array $customPlugins) |
||
574 | |||
575 | /** |
||
576 | * Sets the security policy object to enforce some php security settings. |
||
577 | * use this if untrusted persons can modify templates, |
||
578 | * set it on the Dwoo object as it will be passed onto the compiler automatically |
||
579 | * |
||
580 | * @param SecurityPolicy $policy the security policy object |
||
581 | */ |
||
582 | public function setSecurityPolicy(SecurityPolicy $policy = null) |
||
586 | |||
587 | /** |
||
588 | * Returns the current security policy object or null by default. |
||
589 | * |
||
590 | * @return SecurityPolicy|null the security policy object if any |
||
591 | */ |
||
592 | public function getSecurityPolicy() |
||
596 | |||
597 | /** |
||
598 | * Sets the pointer position. |
||
599 | * |
||
600 | * @param int $position the new pointer position |
||
601 | * @param bool $isOffset if set to true, the position acts as an offset and not an absolute position |
||
602 | */ |
||
603 | public function setPointer($position, $isOffset = false) |
||
611 | |||
612 | /** |
||
613 | * Returns the current pointer position, only available during compilation of a template. |
||
614 | * |
||
615 | * @return int |
||
616 | */ |
||
617 | public function getPointer() |
||
621 | |||
622 | /** |
||
623 | * Sets the line number. |
||
624 | * |
||
625 | * @param int $number the new line number |
||
626 | * @param bool $isOffset if set to true, the position acts as an offset and not an absolute position |
||
627 | */ |
||
628 | public function setLine($number, $isOffset = false) |
||
636 | |||
637 | /** |
||
638 | * Returns the current line number, only available during compilation of a template. |
||
639 | * |
||
640 | * @return int |
||
641 | */ |
||
642 | public function getLine() |
||
646 | |||
647 | /** |
||
648 | * Returns the dwoo object that initiated this template compilation, only available during compilation of a |
||
649 | * template. |
||
650 | * |
||
651 | * @return Core |
||
652 | */ |
||
653 | public function getCore() |
||
654 | { |
||
655 | return $this->core; |
||
656 | } |
||
657 | |||
658 | /** |
||
659 | * Overwrites the template that is being compiled. |
||
660 | * |
||
661 | * @param string $newSource the template source that must replace the current one |
||
662 | * @param bool $fromPointer if set to true, only the source from the current pointer position is replaced |
||
663 | * |
||
664 | * @return void |
||
665 | */ |
||
666 | public function setTemplateSource($newSource, $fromPointer = false) |
||
674 | |||
675 | /** |
||
676 | * Returns the template that is being compiled. |
||
677 | * |
||
678 | * @param mixed $fromPointer if set to true, only the source from the current pointer |
||
679 | * position is returned, if a number is given it overrides the current pointer |
||
680 | * |
||
681 | * @return string the template or partial template |
||
682 | */ |
||
683 | public function getTemplateSource($fromPointer = false) |
||
693 | |||
694 | /** |
||
695 | * Resets the compilation pointer, effectively restarting the compilation process. |
||
696 | * this is useful if a plugin modifies the template source since it might need to be recompiled |
||
697 | */ |
||
698 | public function recompile() |
||
702 | |||
703 | /** |
||
704 | * Compiles the provided string down to php code. |
||
705 | * |
||
706 | * @param Core $core |
||
707 | * @param ITemplate $template the template to compile |
||
708 | * |
||
709 | * @return string a compiled php string |
||
710 | * @throws CompilationException |
||
711 | */ |
||
712 | public function compile(Core $core, ITemplate $template) |
||
964 | |||
965 | /** |
||
966 | * Checks what sub-templates are used in every sub-template so that we're sure they are all compiled. |
||
967 | * |
||
968 | * @param string $function the sub-template name |
||
969 | */ |
||
970 | protected function resolveSubTemplateDependencies($function) |
||
986 | |||
987 | /** |
||
988 | * Adds compiled content to the current block. |
||
989 | * |
||
990 | * @param string $content the content to push |
||
991 | * @param int $lineCount newlines count in content, optional |
||
992 | * |
||
993 | * @throws CompilationException |
||
994 | */ |
||
995 | public function push($content, $lineCount = null) |
||
1014 | |||
1015 | /** |
||
1016 | * Sets the scope. |
||
1017 | * set to null if the scope becomes "unstable" (i.e. too variable or unknown) so that |
||
1018 | * variables are compiled in a more evaluative way than just $this->scope['key'] |
||
1019 | * |
||
1020 | * @param mixed $scope a string i.e. "level1.level2" or an array i.e. array("level1", "level2") |
||
1021 | * @param bool $absolute if true, the scope is set from the top level scope and not from the current scope |
||
1022 | * |
||
1023 | * @return array the current scope tree |
||
1024 | */ |
||
1025 | public function setScope($scope, $absolute = false) |
||
1067 | |||
1068 | /** |
||
1069 | * Adds a block to the top of the block stack. |
||
1070 | * |
||
1071 | * @param string $type block type (name) |
||
1072 | * @param array $params the parameters array |
||
1073 | * @param int $paramtype the parameters type (see mapParams), 0, 1 or 2 |
||
1074 | * |
||
1075 | * @return string the preProcessing() method's output |
||
1076 | */ |
||
1077 | public function addBlock($type, array $params, $paramtype) |
||
1103 | |||
1104 | /** |
||
1105 | * Adds a custom block to the top of the block stack. |
||
1106 | * |
||
1107 | * @param string $type block type (name) |
||
1108 | * @param array $params the parameters array |
||
1109 | * @param int $paramtype the parameters type (see mapParams), 0, 1 or 2 |
||
1110 | * |
||
1111 | * @return string the preProcessing() method's output |
||
1112 | */ |
||
1113 | public function addCustomBlock($type, array $params, $paramtype) |
||
1135 | |||
1136 | /** |
||
1137 | * Injects a block at the top of the plugin stack without calling its preProcessing method. |
||
1138 | * used by {else} blocks to re-add themselves after having closed everything up to their parent |
||
1139 | * |
||
1140 | * @param string $type block type (name) |
||
1141 | * @param array $params parameters array |
||
1142 | */ |
||
1143 | public function injectBlock($type, array $params) |
||
1162 | |||
1163 | /** |
||
1164 | * Removes the closest-to-top block of the given type and all other |
||
1165 | * blocks encountered while going down the block stack. |
||
1166 | * |
||
1167 | * @param string $type block type (name) |
||
1168 | * |
||
1169 | * @return string the output of all postProcessing() method's return values of the closed blocks |
||
1170 | * @throws CompilationException |
||
1171 | */ |
||
1172 | public function removeBlock($type) |
||
1173 | { |
||
1174 | if ($this->debug) { |
||
1175 | echo 'Compiler::' . __FUNCTION__ . "\n"; |
||
1176 | } |
||
1177 | |||
1178 | $output = ''; |
||
1179 | |||
1180 | $pluginType = $this->getPluginType($type); |
||
1181 | if ($pluginType & Core::SMARTY_BLOCK) { |
||
1182 | $type = 'Smartyinterface'; |
||
1183 | } |
||
1184 | while (true) { |
||
1185 | while ($top = array_pop($this->stack)) { |
||
1186 | if ($top['custom']) { |
||
1187 | $class = $top['class']; |
||
1188 | View Code Duplication | } else { |
|
1189 | $class = current(array_filter([ |
||
1190 | 'Plugin' . Core::toCamelCase($top['type']), |
||
1191 | Core::NAMESPACE_PLUGINS_BLOCKS . 'Plugin' . Core::toCamelCase($top['type']) |
||
1192 | ], 'class_exists')); |
||
1193 | } |
||
1194 | if (count($this->stack)) { |
||
1195 | $this->curBlock = &$this->stack[count($this->stack) - 1]; |
||
1196 | $this->push(call_user_func(array( |
||
1197 | $class, |
||
1198 | 'postProcessing' |
||
1199 | ), $this, $top['params'], '', '', $top['buffer']), 0); |
||
1200 | } else { |
||
1201 | $null = null; |
||
1202 | $this->curBlock = &$null; |
||
1203 | $output = call_user_func( |
||
1204 | array( |
||
1205 | $class, |
||
1206 | 'postProcessing' |
||
1207 | ), $this, $top['params'], '', '', $top['buffer'] |
||
1208 | ); |
||
1209 | } |
||
1210 | |||
1211 | if ($top['type'] === $type) { |
||
1212 | break 2; |
||
1213 | } |
||
1214 | } |
||
1215 | |||
1216 | throw new CompilationException($this, 'Syntax malformation, a block of type "' . $type . '" was closed but was not opened'); |
||
1217 | } |
||
1218 | |||
1219 | return $output; |
||
1220 | } |
||
1221 | |||
1222 | /** |
||
1223 | * Returns a reference to the first block of the given type encountered and |
||
1224 | * optionally closes all blocks until it finds it |
||
1225 | * this is mainly used by {else} plugins to close everything that was opened |
||
1226 | * between their parent and themselves. |
||
1227 | * |
||
1228 | * @param string $type the block type (name) |
||
1229 | * @param bool $closeAlong whether to close all blocks encountered while going down the block stack or not |
||
1230 | * |
||
1231 | * @return mixed &array the array is as such: array('type'=>pluginName, 'params'=>parameter array, |
||
1232 | * 'custom'=>bool defining whether it's a custom plugin or not, for internal use) |
||
1233 | * @throws CompilationException |
||
1234 | */ |
||
1235 | public function &findBlock($type, $closeAlong = false) |
||
1256 | |||
1257 | /** |
||
1258 | * Returns a reference to the current block array. |
||
1259 | * |
||
1260 | * @return array the array is as such: array('type'=>pluginName, 'params'=>parameter array, |
||
1261 | * 'custom'=>bool defining whether it's a custom plugin or not, for internal use) |
||
1262 | */ |
||
1263 | public function &getCurrentBlock() |
||
1267 | |||
1268 | /** |
||
1269 | * Removes the block at the top of the stack and calls its postProcessing() method. |
||
1270 | * |
||
1271 | * @return string the postProcessing() method's output |
||
1272 | * @throws CompilationException |
||
1273 | */ |
||
1274 | public function removeTopBlock() |
||
1294 | |||
1295 | /** |
||
1296 | * Returns the compiled parameters (for example a variable's compiled parameter will be "$this->scope['key']") out |
||
1297 | * of the given parameter array. |
||
1298 | * |
||
1299 | * @param array $params parameter array |
||
1300 | * |
||
1301 | * @return array filtered parameters |
||
1302 | */ |
||
1303 | View Code Duplication | public function getCompiledParams(array $params) |
|
1313 | |||
1314 | /** |
||
1315 | * Returns the real parameters (for example a variable's real parameter will be its key, etc) out of the given |
||
1316 | * parameter array. |
||
1317 | * |
||
1318 | * @param array $params parameter array |
||
1319 | * |
||
1320 | * @return array filtered parameters |
||
1321 | */ |
||
1322 | View Code Duplication | public function getRealParams(array $params) |
|
1332 | |||
1333 | /** |
||
1334 | * Returns the token of each parameter out of the given parameter array. |
||
1335 | * |
||
1336 | * @param array $params parameter array |
||
1337 | * |
||
1338 | * @return array tokens |
||
1339 | */ |
||
1340 | View Code Duplication | public function getParamTokens(array $params) |
|
1350 | |||
1351 | /** |
||
1352 | * Entry point of the parser, it redirects calls to other parse* functions. |
||
1353 | * |
||
1354 | * @param string $in the string within which we must parse something |
||
1355 | * @param int $from the starting offset of the parsed area |
||
1356 | * @param int $to the ending offset of the parsed area |
||
1357 | * @param mixed $parsingParams must be an array if we are parsing a function or modifier's parameters, or false by |
||
1358 | * default |
||
1359 | * @param string $curBlock the current parser-block being processed |
||
1360 | * @param mixed $pointer a reference to a pointer that will be increased by the amount of characters parsed, |
||
1361 | * or null by default |
||
1362 | * |
||
1363 | * @return string parsed values |
||
1364 | * @throws CompilationException |
||
1365 | */ |
||
1366 | protected function parse($in, $from, $to, $parsingParams = false, $curBlock = '', &$pointer = null) |
||
1720 | |||
1721 | /** |
||
1722 | * Parses a function call. |
||
1723 | * |
||
1724 | * @param string $in the string within which we must parse something |
||
1725 | * @param int $from the starting offset of the parsed area |
||
1726 | * @param int $to the ending offset of the parsed area |
||
1727 | * @param mixed $parsingParams must be an array if we are parsing a function or modifier's parameters, or false by |
||
1728 | * default |
||
1729 | * @param string $curBlock the current parser-block being processed |
||
1730 | * @param mixed $pointer a reference to a pointer that will be increased by the amount of characters parsed, |
||
1731 | * or null by default |
||
1732 | * |
||
1733 | * @return string parsed values |
||
1734 | * @throws CompilationException |
||
1735 | * @throws Exception |
||
1736 | * @throws SecurityException |
||
1737 | */ |
||
1738 | protected function parseFunction($in, $from, $to, $parsingParams = false, $curBlock = '', &$pointer = null) |
||
1739 | { |
||
1740 | $output = ''; |
||
1741 | $cmdstr = substr($in, $from, $to - $from); |
||
1742 | preg_match('/^(\\\\?[a-z_](?:\\\\?[a-z0-9_]+)*(?:::[a-z_][a-z0-9_]*)?)(\s*' . $this->rdr . '|\s*;)?/i', $cmdstr, $match); |
||
1743 | |||
1744 | View Code Duplication | if (empty($match[1])) { |
|
1745 | throw new CompilationException($this, 'Parse error, invalid function name : ' . substr($cmdstr, 0, 15)); |
||
1746 | } |
||
1747 | |||
1748 | $func = $match[1]; |
||
1749 | |||
1750 | if (!empty($match[2])) { |
||
1751 | $cmdstr = $match[1]; |
||
1752 | } |
||
1753 | |||
1754 | if ($this->debug) { |
||
1755 | echo 'FUNC FOUND (' . $func . ')' . "\n"; |
||
1756 | } |
||
1757 | |||
1758 | $paramsep = ''; |
||
1759 | |||
1760 | if (is_array($parsingParams) || $curBlock != 'root') { |
||
1761 | $paramspos = strpos($cmdstr, '('); |
||
1762 | $paramsep = ')'; |
||
1763 | } elseif (preg_match_all('#^\s*[\\\\:a-z0-9_]+(\s*\(|\s+[^(])#i', $cmdstr, $match, PREG_OFFSET_CAPTURE)) { |
||
1764 | $paramspos = $match[1][0][1]; |
||
1765 | $paramsep = substr($match[1][0][0], - 1) === '(' ? ')' : ''; |
||
1766 | if ($paramsep === ')') { |
||
1767 | $paramspos += strlen($match[1][0][0]) - 1; |
||
1768 | if (substr($cmdstr, 0, 2) === 'if' || substr($cmdstr, 0, 6) === 'elseif') { |
||
1769 | $paramsep = ''; |
||
1770 | if (strlen($match[1][0][0]) > 1) { |
||
1771 | -- $paramspos; |
||
1772 | } |
||
1773 | } |
||
1774 | } |
||
1775 | } else { |
||
1776 | $paramspos = false; |
||
1777 | } |
||
1778 | |||
1779 | $state = 0; |
||
1780 | |||
1781 | if ($paramspos === false) { |
||
1782 | $params = array(); |
||
1783 | |||
1784 | if ($curBlock !== 'root') { |
||
1785 | return $this->parseOthers($in, $from, $to, $parsingParams, $curBlock, $pointer); |
||
1786 | } |
||
1787 | } else { |
||
1788 | if ($curBlock === 'condition') { |
||
1789 | // load if plugin |
||
1790 | $this->getPluginType('if'); |
||
1791 | |||
1792 | if (PluginIf::replaceKeywords(array($func), array(self::T_UNQUOTED_STRING), $this) !== array($func)) { |
||
1793 | return $this->parseOthers($in, $from, $to, $parsingParams, $curBlock, $pointer); |
||
1794 | } |
||
1795 | } |
||
1796 | $whitespace = strlen(substr($cmdstr, strlen($func), $paramspos - strlen($func))); |
||
1797 | $paramstr = substr($cmdstr, $paramspos + 1); |
||
1798 | View Code Duplication | if (substr($paramstr, - 1, 1) === $paramsep) { |
|
1799 | $paramstr = substr($paramstr, 0, - 1); |
||
1800 | } |
||
1801 | |||
1802 | if (strlen($paramstr) === 0) { |
||
1803 | $params = array(); |
||
1804 | $paramstr = ''; |
||
1805 | } else { |
||
1806 | $ptr = 0; |
||
1807 | $params = array(); |
||
1808 | if ($func === 'empty') { |
||
1809 | $params = $this->parseVar($paramstr, $ptr, strlen($paramstr), $params, 'root', $ptr); |
||
1810 | } else { |
||
1811 | while ($ptr < strlen($paramstr)) { |
||
1812 | while (true) { |
||
1813 | if ($ptr >= strlen($paramstr)) { |
||
1814 | break 2; |
||
1815 | } |
||
1816 | |||
1817 | if ($func !== 'if' && $func !== 'elseif' && $paramstr[$ptr] === ')') { |
||
1818 | if ($this->debug) { |
||
1819 | echo 'PARAM PARSING ENDED, ")" FOUND, POINTER AT ' . $ptr . "\n"; |
||
1820 | } |
||
1821 | break 2; |
||
1822 | } elseif ($paramstr[$ptr] === ';') { |
||
1823 | ++ $ptr; |
||
1824 | if ($this->debug) { |
||
1825 | echo 'PARAM PARSING ENDED, ";" FOUND, POINTER AT ' . $ptr . "\n"; |
||
1826 | } |
||
1827 | break 2; |
||
1828 | } elseif ($func !== 'if' && $func !== 'elseif' && $paramstr[$ptr] === '/') { |
||
1829 | if ($this->debug) { |
||
1830 | echo 'PARAM PARSING ENDED, "/" FOUND, POINTER AT ' . $ptr . "\n"; |
||
1831 | } |
||
1832 | break 2; |
||
1833 | } elseif (substr($paramstr, $ptr, strlen($this->rd)) === $this->rd) { |
||
1834 | if ($this->debug) { |
||
1835 | echo 'PARAM PARSING ENDED, RIGHT DELIMITER FOUND, POINTER AT ' . $ptr . "\n"; |
||
1836 | } |
||
1837 | break 2; |
||
1838 | } |
||
1839 | |||
1840 | if ($paramstr[$ptr] === ' ' || $paramstr[$ptr] === ',' || $paramstr[$ptr] === "\r" || $paramstr[$ptr] === "\n" || $paramstr[$ptr] === "\t") { |
||
1841 | ++ $ptr; |
||
1842 | } else { |
||
1843 | break; |
||
1844 | } |
||
1845 | } |
||
1846 | |||
1847 | if ($this->debug) { |
||
1848 | echo 'FUNC START PARAM PARSING WITH POINTER AT ' . $ptr . "\n"; |
||
1849 | } |
||
1850 | |||
1851 | if ($func === 'if' || $func === 'elseif' || $func === 'tif') { |
||
1852 | $params = $this->parse($paramstr, $ptr, strlen($paramstr), $params, 'condition', $ptr); |
||
1853 | } elseif ($func === 'array') { |
||
1854 | $params = $this->parse($paramstr, $ptr, strlen($paramstr), $params, 'array', $ptr); |
||
1855 | } else { |
||
1856 | $params = $this->parse($paramstr, $ptr, strlen($paramstr), $params, 'function', $ptr); |
||
1857 | } |
||
1858 | |||
1859 | View Code Duplication | if ($this->debug) { |
|
1860 | echo 'PARAM PARSED, POINTER AT ' . $ptr . ' (' . substr($paramstr, $ptr - 1, 3) . ')' . "\n"; |
||
1861 | } |
||
1862 | } |
||
1863 | } |
||
1864 | $paramstr = substr($paramstr, 0, $ptr); |
||
1865 | $state = 0; |
||
1866 | foreach ($params as $k => $p) { |
||
1867 | if (is_array($p) && is_array($p[1])) { |
||
1868 | $state |= 2; |
||
1869 | } else { |
||
1870 | if (($state & 2) && preg_match('#^(["\'])(.+?)\1$#', $p[0], $m) && $func !== 'array') { |
||
1871 | $params[$k] = array($m[2], array('true', 'true')); |
||
1872 | } else { |
||
1873 | if ($state & 2 && $func !== 'array') { |
||
1874 | throw new CompilationException($this, 'You can not use an unnamed parameter after a named one'); |
||
1875 | } |
||
1876 | $state |= 1; |
||
1877 | } |
||
1878 | } |
||
1879 | } |
||
1880 | } |
||
1881 | } |
||
1882 | |||
1883 | if ($pointer !== null) { |
||
1884 | $pointer += (isset($paramstr) ? strlen($paramstr) : 0) + (')' === $paramsep ? 2 : ($paramspos === false ? 0 : 1)) + strlen($func) + (isset($whitespace) ? $whitespace : 0); |
||
1885 | if ($this->debug) { |
||
1886 | echo 'FUNC ADDS ' . ((isset($paramstr) ? strlen($paramstr) : 0) + (')' === $paramsep ? 2 : ($paramspos === false ? 0 : 1)) + strlen($func)) . ' TO POINTER' . "\n"; |
||
1887 | } |
||
1888 | } |
||
1889 | |||
1890 | if ($curBlock === 'method' || $func === 'do' || strstr($func, '::') !== false) { |
||
1891 | // handle static method calls with security policy |
||
1892 | if (strstr($func, '::') !== false && $this->securityPolicy !== null && $this->securityPolicy->isMethodAllowed(explode('::', strtolower($func))) !== true) { |
||
1893 | throw new SecurityException('Call to a disallowed php function : ' . $func); |
||
1894 | } |
||
1895 | $pluginType = Core::NATIVE_PLUGIN; |
||
1896 | } else { |
||
1897 | $pluginType = $this->getPluginType($func); |
||
1898 | } |
||
1899 | |||
1900 | // Blocks plugin |
||
1901 | if ($pluginType & Core::BLOCK_PLUGIN) { |
||
1902 | if ($curBlock !== 'root' || is_array($parsingParams)) { |
||
1903 | throw new CompilationException($this, 'Block plugins can not be used as other plugin\'s arguments'); |
||
1904 | } |
||
1905 | if ($pluginType & Core::CUSTOM_PLUGIN) { |
||
1906 | return $this->addCustomBlock($func, $params, $state); |
||
1907 | } else { |
||
1908 | return $this->addBlock($func, $params, $state); |
||
1909 | } |
||
1910 | } elseif ($pluginType & Core::SMARTY_BLOCK) { |
||
1911 | if ($curBlock !== 'root' || is_array($parsingParams)) { |
||
1912 | throw new CompilationException($this, 'Block plugins can not be used as other plugin\'s arguments'); |
||
1913 | } |
||
1914 | |||
1915 | if ($state & 2) { |
||
1916 | array_unshift($params, array('__functype', array($pluginType, $pluginType))); |
||
1917 | array_unshift($params, array('__funcname', array($func, $func))); |
||
1918 | } else { |
||
1919 | array_unshift($params, array($pluginType, $pluginType)); |
||
1920 | array_unshift($params, array($func, $func)); |
||
1921 | } |
||
1922 | |||
1923 | return $this->addBlock('smartyinterface', $params, $state); |
||
1924 | } |
||
1925 | |||
1926 | // Native & Smarty plugins |
||
1927 | if ($pluginType & Core::NATIVE_PLUGIN || $pluginType & Core::SMARTY_FUNCTION || $pluginType & Core::SMARTY_BLOCK) { |
||
1928 | $params = $this->mapParams($params, null, $state); |
||
1929 | } // PHP class plugin |
||
1930 | elseif ($pluginType & Core::CLASS_PLUGIN) { |
||
1931 | if ($pluginType & Core::CUSTOM_PLUGIN) { |
||
1932 | $params = $this->mapParams( |
||
1933 | $params, array( |
||
1934 | $this->customPlugins[$func]['class'], |
||
1935 | $this->customPlugins[$func]['function'] |
||
1936 | ), $state); |
||
1937 | } else { |
||
1938 | if (class_exists('Plugin' . Core::toCamelCase($func)) !== false) { |
||
1939 | $params = $this->mapParams($params, array( |
||
1940 | 'Plugin' . Core::toCamelCase($func), |
||
1941 | ($pluginType & Core::COMPILABLE_PLUGIN) ? 'compile' : 'process' |
||
1942 | ), $state); |
||
1943 | } elseif (class_exists(Core::NAMESPACE_PLUGINS_HELPERS . 'Plugin' . Core::toCamelCase($func)) !== false) { |
||
1944 | $params = $this->mapParams($params, array( |
||
1945 | Core::NAMESPACE_PLUGINS_HELPERS . 'Plugin' . Core::toCamelCase($func), |
||
1946 | ($pluginType & Core::COMPILABLE_PLUGIN) ? 'compile' : 'process' |
||
1947 | ), $state); |
||
1948 | View Code Duplication | } else { |
|
1949 | $params = $this->mapParams($params, array( |
||
1950 | Core::NAMESPACE_PLUGINS_FUNCTIONS . 'Plugin' . Core::toCamelCase($func), |
||
1951 | ($pluginType & Core::COMPILABLE_PLUGIN) ? 'compile' : 'process' |
||
1952 | ), $state); |
||
1953 | } |
||
1954 | } |
||
1955 | } // PHP function plugin |
||
1956 | elseif ($pluginType & Core::FUNC_PLUGIN) { |
||
1957 | if ($pluginType & Core::CUSTOM_PLUGIN) { |
||
1958 | $params = $this->mapParams($params, $this->customPlugins[$func]['callback'], $state); |
||
1959 | } else { |
||
1960 | // Custom plugin |
||
1961 | if (function_exists('Plugin' . Core::toCamelCase($func) . (($pluginType & Core::COMPILABLE_PLUGIN) ? |
||
1962 | 'Compile' : '')) !== false) { |
||
1963 | $params = $this->mapParams($params, 'Plugin' . Core::toCamelCase($func) . (($pluginType & |
||
1964 | Core::COMPILABLE_PLUGIN) ? 'Compile' : ''), $state); |
||
1965 | } // Builtin helper plugin |
||
1966 | elseif (function_exists(Core::NAMESPACE_PLUGINS_HELPERS . 'Plugin' . Core::toCamelCase($func) . ( |
||
1967 | ($pluginType & Core::COMPILABLE_PLUGIN) ? 'Compile' : '')) !== false) { |
||
1968 | $params = $this->mapParams($params, Core::NAMESPACE_PLUGINS_HELPERS . 'Plugin' . Core::toCamelCase |
||
1969 | ($func) . (($pluginType & Core::COMPILABLE_PLUGIN) ? 'Compile' : ''), $state); |
||
1970 | } // Builtin function plugin |
||
1971 | View Code Duplication | else { |
|
1972 | $params = $this->mapParams($params, Core::NAMESPACE_PLUGINS_FUNCTIONS . 'Plugin' . Core::toCamelCase |
||
1973 | ($func) . (($pluginType & Core::COMPILABLE_PLUGIN) ? 'Compile' : ''), $state); |
||
1974 | } |
||
1975 | } |
||
1976 | } // Smarty modifier |
||
1977 | elseif ($pluginType & Core::SMARTY_MODIFIER) { |
||
1978 | $output = 'smarty_modifier_' . $func . '(' . implode(', ', $params) . ')'; |
||
1979 | } // Proxy plugin |
||
1980 | elseif ($pluginType & Core::PROXY_PLUGIN) { |
||
1981 | $params = $this->mapParams($params, $this->getCore()->getPluginProxy()->getCallback($func), $state); |
||
1982 | } // Template plugin |
||
1983 | elseif ($pluginType & Core::TEMPLATE_PLUGIN) { |
||
1984 | // transforms the parameter array from (x=>array('paramname'=>array(values))) to (paramname=>array(values)) |
||
1985 | $map = array(); |
||
1986 | foreach ($this->templatePlugins[$func]['params'] as $param => $defValue) { |
||
1987 | if ($param == 'rest') { |
||
1988 | $param = '*'; |
||
1989 | } |
||
1990 | $hasDefault = $defValue !== null; |
||
1991 | if ($defValue === 'null') { |
||
1992 | $defValue = null; |
||
1993 | } elseif ($defValue === 'false') { |
||
1994 | $defValue = false; |
||
1995 | } elseif ($defValue === 'true') { |
||
1996 | $defValue = true; |
||
1997 | } elseif (preg_match('#^([\'"]).*?\1$#', $defValue)) { |
||
1998 | $defValue = substr($defValue, 1, - 1); |
||
1999 | } |
||
2000 | $map[] = array($param, $hasDefault, $defValue); |
||
2001 | } |
||
2002 | |||
2003 | $params = $this->mapParams($params, null, $state, $map); |
||
2004 | } |
||
2005 | |||
2006 | // only keep php-syntax-safe values for non-block plugins |
||
2007 | $tokens = array(); |
||
2008 | foreach ($params as $k => $p) { |
||
2009 | $tokens[$k] = isset($p[2]) ? $p[2] : 0; |
||
2010 | $params[$k] = $p[0]; |
||
2011 | } |
||
2012 | |||
2013 | // Native plugin |
||
2014 | if ($pluginType & Core::NATIVE_PLUGIN) { |
||
2015 | if ($func === 'do') { |
||
2016 | $output = ''; |
||
2017 | if (isset($params['*'])) { |
||
2018 | $output = implode(';', $params['*']) . ';'; |
||
2019 | } |
||
2020 | |||
2021 | if (is_array($parsingParams) || $curBlock !== 'root') { |
||
2022 | throw new CompilationException($this, 'Do can not be used inside another function or block'); |
||
2023 | } |
||
2024 | |||
2025 | return self::PHP_OPEN . $output . self::PHP_CLOSE; |
||
2026 | } else { |
||
2027 | if (isset($params['*'])) { |
||
2028 | $output = $func . '(' . implode(', ', $params['*']) . ')'; |
||
2029 | } else { |
||
2030 | $output = $func . '()'; |
||
2031 | } |
||
2032 | } |
||
2033 | } // Block class OR Function class |
||
2034 | elseif ($pluginType & Core::CLASS_PLUGIN || ($pluginType & Core::FUNC_PLUGIN && $pluginType & Core::CLASS_PLUGIN)) { |
||
2035 | if ($pluginType & Core::COMPILABLE_PLUGIN) { |
||
2036 | if ($pluginType & Core::CUSTOM_PLUGIN) { |
||
2037 | $callback = $this->customPlugins[$func]['callback']; |
||
2038 | View Code Duplication | if (!is_array($callback)) { |
|
2039 | if (!method_exists($callback, 'compile')) { |
||
2040 | throw new Exception('Custom plugin ' . $func . ' must implement the "compile" method to be compilable, or you should provide a full callback to the method to use'); |
||
2041 | } |
||
2042 | if (($ref = new ReflectionMethod($callback, 'compile')) && $ref->isStatic()) { |
||
2043 | $funcCompiler = array($callback, 'compile'); |
||
2044 | } else { |
||
2045 | $funcCompiler = array(new $callback(), 'compile'); |
||
2046 | } |
||
2047 | } else { |
||
2048 | $funcCompiler = $callback; |
||
2049 | } |
||
2050 | } else { |
||
2051 | if (class_exists('Plugin' . Core::toCamelCase($func)) !== false) { |
||
2052 | $funcCompiler = array('Plugin' . Core::toCamelCase($func), 'compile'); |
||
2053 | } elseif (class_exists(Core::NAMESPACE_PLUGINS_HELPERS . 'Plugin' . Core::toCamelCase($func)) !== false) { |
||
2054 | $funcCompiler = array(Core::NAMESPACE_PLUGINS_HELPERS . 'Plugin' . Core::toCamelCase($func), 'compile'); |
||
2055 | } else { |
||
2056 | $funcCompiler = array( |
||
2057 | Core::NAMESPACE_PLUGINS_FUNCTIONS . 'Plugin' . Core::toCamelCase($func), |
||
2058 | 'compile' |
||
2059 | ); |
||
2060 | } |
||
2061 | array_unshift($params, $this); |
||
2062 | } |
||
2063 | // @TODO: Is it a real fix ? |
||
2064 | if ($func === 'tif') { |
||
2065 | $params[] = $tokens; |
||
2066 | } |
||
2067 | $output = call_user_func_array($funcCompiler, $params); |
||
2068 | } else { |
||
2069 | $params = self::implode_r($params); |
||
2070 | if ($pluginType & Core::CUSTOM_PLUGIN) { |
||
2071 | $callback = $this->customPlugins[$func]['callback']; |
||
2072 | if (!is_array($callback)) { |
||
2073 | if (!method_exists($callback, 'process')) { |
||
2074 | throw new Exception('Custom plugin ' . $func . ' must implement the "process" method to be usable, or you should provide a full callback to the method to use'); |
||
2075 | } |
||
2076 | if (is_object($callback)) { |
||
2077 | $callback = get_class($callback); |
||
2078 | } |
||
2079 | if (($ref = new ReflectionMethod($callback, 'process')) && $ref->isStatic()) { |
||
2080 | $output = 'call_user_func(array(\'' . $callback . '\', \'process\'), ' . $params . ')'; |
||
2081 | } else { |
||
2082 | $output = 'call_user_func(array($this->getObjectPlugin(\'' . $callback . '\'), \'process\'), ' . $params . ')'; |
||
2083 | } |
||
2084 | View Code Duplication | } elseif (is_object($callback[0])) { |
|
2085 | $output = 'call_user_func(array($this->plugins[\'' . $func . '\'][\'callback\'][0], \'' . $callback[1] . '\'), ' . $params . ')'; |
||
2086 | } elseif (($ref = new ReflectionMethod($callback[0], $callback[1])) && $ref->isStatic()) { |
||
2087 | $output = 'call_user_func(array(\'' . $callback[0] . '\', \'' . $callback[1] . '\'), ' . $params . ')'; |
||
2088 | View Code Duplication | } else { |
|
2089 | $output = 'call_user_func(array($this->getObjectPlugin(\'' . $callback[0] . '\'), \'' . $callback[1] . '\'), ' . $params . ')'; |
||
2090 | } |
||
2091 | if (empty($params)) { |
||
2092 | $output = substr($output, 0, - 3) . ')'; |
||
2093 | } |
||
2094 | } else { |
||
2095 | if (class_exists('Plugin' . Core::toCamelCase($func)) !== false) { |
||
2096 | $output = '$this->classCall(\'Plugin' . $func . '\', array(' . $params . '))'; |
||
2097 | View Code Duplication | } elseif (class_exists(Core::NAMESPACE_PLUGINS_FUNCTIONS . 'Plugin' . Core::toCamelCase($func)) !== false) { |
|
2098 | $output = '$this->classCall(\'' . Core::NAMESPACE_PLUGINS_FUNCTIONS . 'Plugin' . $func . '\', |
||
2099 | array(' . $params . '))'; |
||
2100 | } else { |
||
2101 | $output = '$this->classCall(\'' . $func . '\', array(' . $params . '))'; |
||
2102 | } |
||
2103 | } |
||
2104 | } |
||
2105 | } // Function plugin only (cannot be a class) |
||
2106 | elseif ($pluginType & Core::FUNC_PLUGIN) { |
||
2107 | if ($pluginType & Core::COMPILABLE_PLUGIN) { |
||
2108 | if ($pluginType & Core::CUSTOM_PLUGIN) { |
||
2109 | $funcCompiler = $this->customPlugins[$func]['callback']; |
||
2110 | } else { |
||
2111 | // Custom plugin |
||
2112 | if (function_exists('Plugin' . Core::toCamelCase($func) . 'Compile') !== false) { |
||
2113 | $funcCompiler = 'Plugin' . Core::toCamelCase($func) . 'Compile'; |
||
2114 | } // Builtin helper plugin |
||
2115 | View Code Duplication | elseif (function_exists(Core::NAMESPACE_PLUGINS_HELPERS . 'Plugin' . Core::toCamelCase($func) . 'Compile') !== false) { |
|
2116 | $funcCompiler = Core::NAMESPACE_PLUGINS_HELPERS . 'Plugin' . Core::toCamelCase($func) . |
||
2117 | 'Compile'; |
||
2118 | } // Builtin function plugin |
||
2119 | else { |
||
2120 | $funcCompiler = Core::NAMESPACE_PLUGINS_FUNCTIONS . 'Plugin' . Core::toCamelCase($func) . |
||
2121 | 'Compile'; |
||
2122 | } |
||
2123 | } |
||
2124 | array_unshift($params, $this); |
||
2125 | // @TODO: Is it a real fix ? |
||
2126 | if ($func === 'tif') { |
||
2127 | $params[] = $tokens; |
||
2128 | } |
||
2129 | $output = call_user_func_array($funcCompiler, $params); |
||
2130 | } else { |
||
2131 | array_unshift($params, '$this'); |
||
2132 | $params = self::implode_r($params); |
||
2133 | if ($pluginType & Core::CUSTOM_PLUGIN) { |
||
2134 | $callback = $this->customPlugins[$func]['callback']; |
||
2135 | if ($callback instanceof Closure) { |
||
2136 | $output = 'call_user_func($this->getCustomPlugin(\'' . $func . '\'), ' . $params . ')'; |
||
2137 | } else { |
||
2138 | $output = 'call_user_func(\'' . $callback . '\', ' . $params . ')'; |
||
2139 | } |
||
2140 | } else { |
||
2141 | // Custom plugin |
||
2142 | if (function_exists('Plugin' . Core::toCamelCase($func)) !== false) { |
||
2143 | $output = 'Plugin' . Core::toCamelCase($func) . '(' . $params . |
||
2144 | ')'; |
||
2145 | } // Builtin helper plugin |
||
2146 | View Code Duplication | elseif(function_exists(Core::NAMESPACE_PLUGINS_HELPERS . 'Plugin' . Core::toCamelCase($func)) !== |
|
2147 | false) { |
||
2148 | $output = Core::NAMESPACE_PLUGINS_HELPERS . 'Plugin' . Core::toCamelCase($func) . '(' . |
||
2149 | $params . ')'; |
||
2150 | } // Builtin function plugin |
||
2151 | View Code Duplication | else { |
|
2152 | $output = Core::NAMESPACE_PLUGINS_FUNCTIONS . 'Plugin' . Core::toCamelCase($func) . '(' . |
||
2153 | $params . ')'; |
||
2154 | } |
||
2155 | } |
||
2156 | } |
||
2157 | } // Proxy plugin |
||
2158 | elseif ($pluginType & Core::PROXY_PLUGIN) { |
||
2159 | $output = call_user_func(array($this->getCore()->getPluginProxy(), 'getCode'), $func, $params); |
||
2160 | } // Smarty function (@deprecated) |
||
2161 | elseif ($pluginType & Core::SMARTY_FUNCTION) { |
||
2162 | $params = ''; |
||
2163 | if (isset($params['*'])) { |
||
2164 | $params = self::implode_r($params['*'], true); |
||
2165 | } |
||
2166 | |||
2167 | if ($pluginType & Core::CUSTOM_PLUGIN) { |
||
2168 | $callback = $this->customPlugins[$func]['callback']; |
||
2169 | if (is_array($callback)) { |
||
2170 | if (is_object($callback[0])) { |
||
2171 | $output = 'call_user_func_array(array($this->getCustomPlugin(\'' . $func . '\'), \'' . $callback[1] . '\'), array(array(' . $params . '), $this))'; |
||
2172 | View Code Duplication | } else { |
|
2173 | $output = 'call_user_func_array(array(\'' . $callback[0] . '\', \'' . $callback[1] . '\'), array(array(' . $params . '), $this))'; |
||
2174 | } |
||
2175 | } else { |
||
2176 | $output = $callback . '(array(' . $params . '), $this)'; |
||
2177 | } |
||
2178 | } else { |
||
2179 | $output = 'smarty_function_' . $func . '(array(' . $params . '), $this)'; |
||
2180 | } |
||
2181 | } // Template plugin |
||
2182 | elseif ($pluginType & Core::TEMPLATE_PLUGIN) { |
||
2183 | array_unshift($params, '$this'); |
||
2184 | $params = self::implode_r($params); |
||
2185 | $output = 'Plugin' . Core::toCamelCase($func) . |
||
2186 | $this->templatePlugins[$func]['uuid'] . '(' . $params . ')'; |
||
2187 | $this->templatePlugins[$func]['called'] = true; |
||
2188 | } |
||
2189 | |||
2190 | View Code Duplication | if (is_array($parsingParams)) { |
|
2191 | $parsingParams[] = array($output, $output); |
||
2192 | |||
2193 | return $parsingParams; |
||
2194 | } elseif ($curBlock === 'namedparam') { |
||
2195 | return array($output, $output); |
||
2196 | } |
||
2197 | |||
2198 | return $output; |
||
2199 | } |
||
2200 | |||
2201 | /** |
||
2202 | * Parses a string. |
||
2203 | * |
||
2204 | * @param string $in the string within which we must parse something |
||
2205 | * @param int $from the starting offset of the parsed area |
||
2206 | * @param int $to the ending offset of the parsed area |
||
2207 | * @param mixed $parsingParams must be an array if we are parsing a function or modifier's parameters, or false by |
||
2208 | * default |
||
2209 | * @param string $curBlock the current parser-block being processed |
||
2210 | * @param mixed $pointer a reference to a pointer that will be increased by the amount of characters parsed, |
||
2211 | * or null by default |
||
2212 | * |
||
2213 | * @return string parsed values |
||
2214 | * @throws CompilationException |
||
2215 | */ |
||
2216 | protected function parseString($in, $from, $to, $parsingParams = false, $curBlock = '', &$pointer = null) |
||
2276 | |||
2277 | /** |
||
2278 | * Parses a constant. |
||
2279 | * |
||
2280 | * @param string $in the string within which we must parse something |
||
2281 | * @param int $from the starting offset of the parsed area |
||
2282 | * @param int $to the ending offset of the parsed area |
||
2283 | * @param mixed $parsingParams must be an array if we are parsing a function or modifier's parameters, or false by |
||
2284 | * default |
||
2285 | * @param string $curBlock the current parser-block being processed |
||
2286 | * @param mixed $pointer a reference to a pointer that will be increased by the amount of characters parsed, |
||
2287 | * or null by default |
||
2288 | * |
||
2289 | * @return string parsed values |
||
2290 | * @throws CompilationException |
||
2291 | */ |
||
2292 | protected function parseConst($in, $from, $to, $parsingParams = false, $curBlock = '', &$pointer = null) |
||
2320 | |||
2321 | /** |
||
2322 | * Parses a constant. |
||
2323 | * |
||
2324 | * @param string $key the constant to parse |
||
2325 | * @param string $curBlock the current parser-block being processed |
||
2326 | * |
||
2327 | * @return string parsed constant |
||
2328 | */ |
||
2329 | protected function parseConstKey($key, $curBlock) |
||
2343 | |||
2344 | /** |
||
2345 | * Parses a variable. |
||
2346 | * |
||
2347 | * @param string $in the string within which we must parse something |
||
2348 | * @param int $from the starting offset of the parsed area |
||
2349 | * @param int $to the ending offset of the parsed area |
||
2350 | * @param mixed $parsingParams must be an array if we are parsing a function or modifier's parameters, or false by |
||
2351 | * default |
||
2352 | * @param string $curBlock the current parser-block being processed |
||
2353 | * @param mixed $pointer a reference to a pointer that will be increased by the amount of characters parsed, |
||
2354 | * or null by default |
||
2355 | * |
||
2356 | * @return string parsed values |
||
2357 | * @throws CompilationException |
||
2358 | */ |
||
2359 | protected function parseVar($in, $from, $to, $parsingParams = false, $curBlock = '', &$pointer = null) |
||
2566 | |||
2567 | /** |
||
2568 | * Parses any number of chained method calls/property reads. |
||
2569 | * |
||
2570 | * @param string $output the variable or whatever upon which the method are called |
||
2571 | * @param string $methodCall method call source, starting at "->" |
||
2572 | * @param string $curBlock the current parser-block being processed |
||
2573 | * @param int $pointer a reference to a pointer that will be increased by the amount of characters parsed |
||
2574 | * |
||
2575 | * @return string parsed call(s)/read(s) |
||
2576 | */ |
||
2577 | protected function parseMethodCall($output, $methodCall, $curBlock, &$pointer) |
||
2653 | |||
2654 | /** |
||
2655 | * Parses a constant variable (a variable that doesn't contain another variable) and preprocesses it to save |
||
2656 | * runtime processing time. |
||
2657 | * |
||
2658 | * @param string $key the variable to parse |
||
2659 | * @param string $curBlock the current parser-block being processed |
||
2660 | * |
||
2661 | * @return string parsed variable |
||
2662 | */ |
||
2663 | protected function parseVarKey($key, $curBlock) |
||
2767 | |||
2768 | /** |
||
2769 | * Flattens a variable tree, this helps in parsing very complex variables such as $var.foo[$foo.bar->baz].baz, |
||
2770 | * it computes the contents of the brackets first and works out from there. |
||
2771 | * |
||
2772 | * @param array $tree the variable tree parsed by he parseVar() method that must be flattened |
||
2773 | * @param bool $recursed leave that to false by default, it is only for internal use |
||
2774 | * |
||
2775 | * @return string flattened tree |
||
2776 | */ |
||
2777 | protected function flattenVarTree(array $tree, $recursed = false) |
||
2829 | |||
2830 | /** |
||
2831 | * Helper function that parses a variable. |
||
2832 | * |
||
2833 | * @param array $match the matched variable, array(1=>"string match") |
||
2834 | * |
||
2835 | * @return string parsed variable |
||
2836 | */ |
||
2837 | protected function replaceVarKeyHelper($match) |
||
2841 | |||
2842 | /** |
||
2843 | * Parses various constants, operators or non-quoted strings. |
||
2844 | * |
||
2845 | * @param string $in the string within which we must parse something |
||
2846 | * @param int $from the starting offset of the parsed area |
||
2847 | * @param int $to the ending offset of the parsed area |
||
2848 | * @param mixed $parsingParams must be an array if we are parsing a function or modifier's parameters, or false by |
||
2849 | * default |
||
2850 | * @param string $curBlock the current parser-block being processed |
||
2851 | * @param mixed $pointer a reference to a pointer that will be increased by the amount of characters parsed, |
||
2852 | * or null by default |
||
2853 | * |
||
2854 | * @return string parsed values |
||
2855 | * @throws Exception |
||
2856 | */ |
||
2857 | protected function parseOthers($in, $from, $to, $parsingParams = false, $curBlock = '', &$pointer = null) |
||
2990 | |||
2991 | /** |
||
2992 | * Replaces variables within a parsed string. |
||
2993 | * |
||
2994 | * @param string $string the parsed string |
||
2995 | * @param string $first the first character parsed in the string, which is the string delimiter (' or ") |
||
2996 | * @param string $curBlock the current parser-block being processed |
||
2997 | * |
||
2998 | * @return string the original string with variables replaced |
||
2999 | */ |
||
3000 | protected function replaceStringVars($string, $first, $curBlock = '') |
||
3045 | |||
3046 | /** |
||
3047 | * Replaces the modifiers applied to a string or a variable. |
||
3048 | * |
||
3049 | * @param array $m the regex matches that must be array(1=>"double or single quotes enclosing a string, |
||
3050 | * when applicable", 2=>"the string or var", 3=>"the modifiers matched") |
||
3051 | * @param string $curBlock the current parser-block being processed |
||
3052 | * @param null $pointer |
||
3053 | * |
||
3054 | * @return string the input enclosed with various function calls according to the modifiers found |
||
3055 | * @throws CompilationException |
||
3056 | * @throws Exception |
||
3057 | */ |
||
3058 | protected function replaceModifiers(array $m, $curBlock = null, &$pointer = null) |
||
3352 | |||
3353 | /** |
||
3354 | * Recursively implodes an array in a similar manner as var_export() does but with some tweaks |
||
3355 | * to handle pre-compiled values and the fact that we do not need to enclose everything with |
||
3356 | * "array" and do not require top-level keys to be displayed. |
||
3357 | * |
||
3358 | * @param array $params the array to implode |
||
3359 | * @param bool $recursiveCall if set to true, the function outputs key names for the top level |
||
3360 | * |
||
3361 | * @return string the imploded array |
||
3362 | */ |
||
3363 | public static function implode_r(array $params, $recursiveCall = false) |
||
3383 | |||
3384 | /** |
||
3385 | * Returns the plugin type of a plugin and adds it to the used plugins array if required. |
||
3386 | * |
||
3387 | * @param string $name plugin name, as found in the template |
||
3388 | * |
||
3389 | * @return int type as a multi bit flag composed of the Dwoo plugin types constants |
||
3390 | * @throws Exception |
||
3391 | * @throws SecurityException |
||
3392 | * @throws Exception |
||
3393 | */ |
||
3394 | protected function getPluginType($name) |
||
3491 | |||
3492 | /** |
||
3493 | * Allows a plugin to load another one at compile time, this will also mark |
||
3494 | * it as used by this template so it will be loaded at runtime (which can be |
||
3495 | * useful for compiled plugins that rely on another plugin when their compiled |
||
3496 | * code runs). |
||
3497 | * |
||
3498 | * @param string $name the plugin name |
||
3499 | * |
||
3500 | * @return void |
||
3501 | */ |
||
3502 | public function loadPlugin($name) |
||
3506 | |||
3507 | /** |
||
3508 | * Runs htmlentities over the matched <?php ?> blocks when the security policy enforces that. |
||
3509 | * |
||
3510 | * @param array $match matched php block |
||
3511 | * |
||
3512 | * @return string the htmlentities-converted string |
||
3513 | */ |
||
3514 | protected function phpTagEncodingHelper($match) |
||
3518 | |||
3519 | /** |
||
3520 | * Maps the parameters received from the template onto the parameters required by the given callback. |
||
3521 | * |
||
3522 | * @param array $params the array of parameters |
||
3523 | * @param callback $callback the function or method to reflect on to find out the required parameters |
||
3524 | * @param int $callType the type of call in the template, 0 = no params, 1 = php-style call, 2 = named |
||
3525 | * parameters call |
||
3526 | * @param array $map the parameter map to use, if not provided it will be built from the callback |
||
3527 | * |
||
3528 | * @return array parameters sorted in the correct order with missing optional parameters filled |
||
3529 | * @throws CompilationException |
||
3530 | */ |
||
3531 | protected function mapParams(array $params, $callback, $callType = 2, $map = null) |
||
3624 | |||
3625 | /** |
||
3626 | * Returns the parameter map of the given callback, it filters out entries typed as Dwoo and Compiler and turns the |
||
3627 | * rest parameter into a "*". |
||
3628 | * |
||
3629 | * @param callback $callback the function/method to reflect on |
||
3630 | * |
||
3631 | * @return array processed parameter map |
||
3632 | */ |
||
3633 | protected function getParamMap($callback) |
||
3665 | |||
3666 | /** |
||
3667 | * Returns a default instance of this compiler, used by default by all Dwoo templates that do not have a |
||
3668 | * specific compiler assigned and when you do not override the default compiler factory function. |
||
3669 | * |
||
3670 | * @see Core::setDefaultCompilerFactory() |
||
3671 | * @return Compiler |
||
3672 | */ |
||
3673 | public static function compilerFactory() |
||
3681 | } |
||
3682 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.