Complex classes like Template 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 Template, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | class Template |
||
41 | { |
||
42 | const CLEAN_TEMPLATE_YES = true; |
||
43 | const CLEAN_TEMPLATE_NO = false; |
||
44 | |||
45 | protected $prefix; |
||
46 | protected $cObj; |
||
47 | protected $templateFile; |
||
48 | protected $template; |
||
49 | protected $workOnSubpart; |
||
50 | protected $viewHelperIncludePath; |
||
51 | protected $helpers = []; |
||
52 | protected $loadedHelperFiles = []; |
||
53 | protected $variables = []; |
||
54 | protected $markers = []; |
||
55 | protected $subparts = []; |
||
56 | protected $loops = []; |
||
57 | |||
58 | protected $debugMode = false; |
||
59 | |||
60 | /** |
||
61 | * Constructor for the html marker template engine. |
||
62 | * |
||
63 | * @param ContentObjectRenderer $contentObject content object |
||
64 | * @param string $templateFile path to the template file |
||
65 | * @param string $subpart name of the subpart to work on |
||
66 | */ |
||
67 | 26 | public function __construct( |
|
68 | ContentObjectRenderer $contentObject, |
||
69 | $templateFile, |
||
70 | $subpart |
||
71 | ) { |
||
72 | 26 | $this->cObj = $contentObject; |
|
73 | 26 | $this->templateFile = $templateFile; |
|
74 | |||
75 | 26 | $this->loadHtmlFile($templateFile); |
|
76 | 26 | $this->workOnSubpart($subpart); |
|
77 | 26 | } |
|
78 | |||
79 | /** |
||
80 | * @return \TYPO3\CMS\Core\Service\MarkerBasedTemplateService |
||
81 | */ |
||
82 | 26 | protected function getTemplateService() |
|
83 | { |
||
84 | 26 | return GeneralUtility::makeInstance(MarkerBasedTemplateService::class); |
|
85 | } |
||
86 | |||
87 | /** |
||
88 | * Copy constructor, sets the clone object's template content to original |
||
89 | * object's work subpart. |
||
90 | * |
||
91 | */ |
||
92 | 25 | public function __clone() |
|
93 | { |
||
94 | 25 | $this->setTemplateContent($this->getWorkOnSubpart()); |
|
95 | 25 | } |
|
96 | |||
97 | /** |
||
98 | * Loads the content of a html template file. Resolves paths beginning with |
||
99 | * "EXT:". |
||
100 | * |
||
101 | * @param string $htmlFile path to html template file |
||
102 | */ |
||
103 | 26 | public function loadHtmlFile($htmlFile) |
|
104 | { |
||
105 | 26 | $path = $GLOBALS['TSFE']->tmpl->getFileName($htmlFile); |
|
106 | 26 | if ($path !== null && file_exists($path)) { |
|
107 | 26 | $this->template = file_get_contents($path); |
|
108 | 26 | } |
|
109 | 26 | if (empty($this->template)) { |
|
110 | throw new \RuntimeException( |
||
111 | 'Could not load template file "' . htmlspecialchars($htmlFile) . '"', |
||
112 | 1327490358 |
||
113 | ); |
||
114 | } |
||
115 | 26 | } |
|
116 | |||
117 | /** |
||
118 | * Sets the content for the template we're working on |
||
119 | * |
||
120 | * @param string $templateContent the template's content - usually HTML |
||
121 | * @return void |
||
122 | */ |
||
123 | 25 | public function setWorkingTemplateContent($templateContent) |
|
124 | { |
||
125 | 25 | $this->workOnSubpart = $templateContent; |
|
126 | 25 | } |
|
127 | |||
128 | /** |
||
129 | * Finds the view helpers in the template and loads them. |
||
130 | * |
||
131 | * @param string $content |
||
132 | */ |
||
133 | 26 | protected function initializeViewHelpers($content) |
|
134 | { |
||
135 | 26 | $viewHelpersFound = $this->findViewHelpers($content); |
|
136 | |||
137 | 26 | foreach ($viewHelpersFound as $helperKey) { |
|
138 | 24 | if (!isset($this->helpers[strtolower($helperKey)])) { |
|
139 | 18 | $viewHelperLoaded = $this->loadViewHelper($helperKey); |
|
140 | |||
141 | 18 | if (!$viewHelperLoaded) { |
|
142 | // skipping processing in case we couldn't find a class |
||
143 | // to handle the view helper |
||
144 | continue; |
||
145 | } |
||
146 | |||
147 | 18 | $this->addViewHelper($helperKey); |
|
148 | 18 | } |
|
149 | 26 | } |
|
150 | 26 | } |
|
151 | |||
152 | /** |
||
153 | * Adds an include path where the template engine should look for template |
||
154 | * view helpers. |
||
155 | * |
||
156 | * @param string $extensionKey Extension key |
||
157 | * @param string $viewHelperPath Path inside the extension to look for view helpers |
||
158 | */ |
||
159 | 26 | public function addViewHelperIncludePath($extensionKey, $viewHelperPath) |
|
160 | { |
||
161 | 26 | $this->viewHelperIncludePath[$extensionKey] = $viewHelperPath; |
|
162 | 26 | } |
|
163 | |||
164 | /** |
||
165 | * Adds a view helper |
||
166 | * |
||
167 | * @param string $helperName view helper name |
||
168 | * @param array $arguments optional array of arguments |
||
169 | * @return bool |
||
170 | */ |
||
171 | 26 | public function addViewHelper($helperName, array $arguments = []) |
|
172 | { |
||
173 | 26 | $success = false; |
|
174 | |||
175 | 26 | if (!isset($this->helpers[strtolower($helperName)])) { |
|
176 | 26 | $viewHelperClassName = $this->loadViewHelper($helperName); |
|
177 | |||
178 | // could be FALSE if not matching view helper class was found |
||
179 | 26 | if ($viewHelperClassName) { |
|
180 | try { |
||
181 | 26 | $helperInstance = GeneralUtility::makeInstance($viewHelperClassName, |
|
182 | 26 | $arguments); |
|
183 | 26 | $success = $this->addViewHelperObject($helperName, |
|
184 | 26 | $helperInstance); |
|
185 | 26 | } catch (\Exception $e) { |
|
186 | $configuration = Util::getSolrConfiguration(); |
||
187 | if ($configuration->getLoggingExceptions()) { |
||
188 | GeneralUtility::devLog('exception while adding a viewhelper', |
||
189 | 'solr', 3, [ |
||
190 | $e->__toString() |
||
191 | ]); |
||
192 | } |
||
193 | } |
||
194 | 26 | } |
|
195 | 26 | } |
|
196 | |||
197 | 26 | return $success; |
|
198 | } |
||
199 | |||
200 | 26 | protected function loadViewHelper($helperKey) |
|
201 | { |
||
202 | 26 | if (isset($this->loadedHelperFiles[strtolower($helperKey)])) { |
|
203 | 18 | return $this->loadedHelperFiles[strtolower($helperKey)]['class']; |
|
204 | } |
||
205 | |||
206 | 26 | foreach ($this->viewHelperIncludePath as $extensionKey => $viewHelperPath) { |
|
207 | 26 | $viewHelperRealPath = $viewHelperPath; |
|
208 | 26 | if (GeneralUtility::isFirstPartOfStr($viewHelperPath, 'Classes/')) { |
|
209 | 26 | $viewHelperRealPath = substr($viewHelperPath, 8); |
|
210 | 26 | } |
|
211 | 26 | if (substr($viewHelperRealPath, -1) == '/') { |
|
212 | 26 | $viewHelperRealPath = substr($viewHelperRealPath, 0, -1); |
|
213 | 26 | } |
|
214 | |||
215 | 26 | $ucHelperKey = Util::underscoredToUpperCamelCase($helperKey); |
|
216 | 26 | $vendorNameSpace = 'ApacheSolrForTypo3\\Solr\\'; |
|
217 | 26 | $possibleFilename = $ucHelperKey . '.php'; |
|
218 | 26 | $possibleClassName = $vendorNameSpace . str_replace('/', '\\', |
|
219 | 26 | $viewHelperRealPath) . '\\' . $ucHelperKey; |
|
220 | |||
221 | 26 | $viewHelperIncludePath = ExtensionManagementUtility::extPath($extensionKey) |
|
222 | 26 | . $viewHelperPath . $possibleFilename; |
|
223 | |||
224 | 26 | if (file_exists($viewHelperIncludePath)) { |
|
225 | 26 | include_once($viewHelperIncludePath); |
|
226 | 26 | $this->loadedHelperFiles[strtolower($helperKey)] = [ |
|
227 | 26 | 'file' => $viewHelperIncludePath, |
|
228 | 'class' => $possibleClassName |
||
229 | 26 | ]; |
|
230 | |||
231 | 26 | return $possibleClassName; |
|
232 | } |
||
233 | } |
||
234 | |||
235 | // view helper could not be found |
||
236 | return false; |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * adds an already instantiated view helper |
||
241 | * |
||
242 | * @param $helperName |
||
243 | * @param ViewHelper $helperObject |
||
244 | * @return bool |
||
245 | */ |
||
246 | 26 | public function addViewHelperObject($helperName, ViewHelper $helperObject) |
|
247 | { |
||
248 | 26 | $success = false; |
|
249 | |||
250 | 26 | $helperName = strtolower($helperName); |
|
251 | |||
252 | 26 | if (!isset($this->helpers[$helperName])) { |
|
253 | 26 | $this->helpers[$helperName] = $helperObject; |
|
254 | 26 | $success = true; |
|
255 | 26 | } |
|
256 | |||
257 | 26 | return $success; |
|
258 | } |
||
259 | |||
260 | /** |
||
261 | * Renders the template and fills its markers. |
||
262 | * |
||
263 | * @param bool $cleanTemplate |
||
264 | * @return string the rendered html template with markers replaced with their content |
||
265 | */ |
||
266 | 26 | public function render($cleanTemplate = false) |
|
267 | { |
||
268 | |||
269 | // process loops |
||
270 | 26 | foreach ($this->loops as $key => $loopVariables) { |
|
271 | 23 | $this->renderLoop($key); |
|
272 | 26 | } |
|
273 | |||
274 | // process variables |
||
275 | 26 | foreach ($this->variables as $variableKey => $variable) { |
|
276 | 25 | $variableKey = strtoupper($variableKey); |
|
277 | 25 | $variableMarkers = $this->getVariableMarkers($variableKey, |
|
278 | 25 | $this->workOnSubpart); |
|
279 | |||
280 | 25 | if (count($variableMarkers)) { |
|
281 | 25 | $resolvedMarkers = $this->resolveVariableMarkers($variableMarkers, |
|
282 | 25 | $variable); |
|
283 | |||
284 | 25 | $this->workOnSubpart = $this->getTemplateService()->substituteMarkerArray( |
|
285 | 25 | $this->workOnSubpart, |
|
286 | 25 | $resolvedMarkers, |
|
287 | '###|###' |
||
288 | 25 | ); |
|
289 | 25 | } |
|
290 | 26 | } |
|
291 | |||
292 | // process markers |
||
293 | 26 | $this->workOnSubpart = $this->getTemplateService()->substituteMarkerArray( |
|
294 | 26 | $this->workOnSubpart, |
|
295 | 26 | $this->markers |
|
296 | 26 | ); |
|
297 | |||
298 | // process subparts |
||
299 | 26 | foreach ($this->subparts as $subpart => $content) { |
|
300 | 25 | $this->workOnSubpart = $this->getTemplateService()->substituteSubpart( |
|
301 | 25 | $this->workOnSubpart, |
|
302 | 25 | $subpart, |
|
303 | $content |
||
304 | 25 | ); |
|
305 | 26 | } |
|
306 | |||
307 | // process view helpers, they need to be the last objects processing the template |
||
308 | 26 | $this->initializeViewHelpers($this->workOnSubpart); |
|
309 | 26 | $this->workOnSubpart = $this->renderViewHelpers($this->workOnSubpart); |
|
310 | |||
311 | // process conditions |
||
312 | 26 | $this->workOnSubpart = $this->processConditions($this->workOnSubpart); |
|
313 | |||
314 | // finally, do a cleanup if not disabled |
||
315 | 26 | if ($cleanTemplate) { |
|
316 | 26 | $this->cleanTemplate(); |
|
317 | 26 | } |
|
318 | |||
319 | 26 | return $this->workOnSubpart; |
|
320 | } |
||
321 | |||
322 | /** |
||
323 | * Escapes marker hashes and the pipe symbol so that they will not be |
||
324 | * executed in templates. |
||
325 | * |
||
326 | * @param string $content Content potentially containing markers |
||
327 | * @return string Content with markers escaped |
||
328 | */ |
||
329 | 25 | public static function escapeMarkers($content) |
|
330 | { |
||
331 | // escape marker hashes |
||
332 | 25 | $content = str_replace('###', '###', $content); |
|
333 | // escape pipe character used for parameter separation |
||
334 | 25 | $content = str_replace('|', '|', $content); |
|
335 | |||
336 | 25 | return $content; |
|
337 | } |
||
338 | |||
339 | /** |
||
340 | * cleans the template from non-replaced markers and subparts |
||
341 | * |
||
342 | * @return void |
||
343 | */ |
||
344 | 26 | public function cleanTemplate() |
|
345 | { |
||
346 | 26 | $remainingMarkers = $this->findMarkers(); |
|
347 | |||
348 | 26 | foreach ($remainingMarkers as $remainingMarker) { |
|
349 | 23 | $isSubpart = preg_match_all( |
|
350 | 23 | '/(\<\!\-\-[\s]+###' . $remainingMarker . '###.*###' |
|
351 | 23 | . $remainingMarker . '###.+\-\-\>)/sU', |
|
352 | 23 | $this->workOnSubpart, |
|
353 | 23 | $matches, |
|
354 | PREG_SET_ORDER |
||
355 | 23 | ); |
|
356 | |||
357 | 23 | if ($isSubpart) { |
|
358 | 23 | $this->workOnSubpart = str_replace( |
|
359 | 23 | $matches[0][1], |
|
360 | 23 | '', |
|
361 | 23 | $this->workOnSubpart |
|
362 | 23 | ); |
|
363 | 23 | } else { |
|
364 | $this->workOnSubpart = str_replace( |
||
365 | '###' . $remainingMarker . '###', |
||
366 | '', |
||
367 | $this->workOnSubpart |
||
368 | ); |
||
369 | } |
||
370 | 26 | } |
|
371 | |||
372 | 26 | $unresolvedConditions = $this->findConditions($this->workOnSubpart); |
|
373 | 26 | foreach ($unresolvedConditions as $unresolvedCondition) { |
|
374 | // if condition evaluates to FALSE, remove the content from the template |
||
375 | $this->workOnSubpart = $this->getTemplateService()->substituteSubpart( |
||
376 | $this->workOnSubpart, |
||
377 | $unresolvedCondition['marker'], |
||
378 | '' |
||
|
|||
379 | ); |
||
380 | 26 | } |
|
381 | 26 | } |
|
382 | |||
383 | /** |
||
384 | * Renders view helpers, detects whether it is a regular marker view helper |
||
385 | * or a subpart view helper and passes rendering on to more specialized |
||
386 | * render methods for each type. |
||
387 | * |
||
388 | * @param string $content The content to process by view helpers |
||
389 | * @return string the view helper processed content |
||
390 | */ |
||
391 | 26 | protected function renderViewHelpers($content) |
|
392 | { |
||
393 | 26 | $viewHelpersFound = $this->findViewHelpers($content); |
|
394 | |||
395 | 26 | foreach ($viewHelpersFound as $helperKey) { |
|
396 | 24 | if (array_key_exists(strtolower($helperKey), $this->helpers)) { |
|
397 | 24 | $helper = $this->helpers[strtolower($helperKey)]; |
|
398 | |||
399 | 24 | if ($helper instanceof SubpartViewHelper) { |
|
400 | $content = $this->renderSubpartViewHelper($helper, |
||
401 | $helperKey, $content); |
||
402 | } else { |
||
403 | 24 | $content = $this->renderMarkerViewHelper($helper, |
|
404 | 24 | $helperKey, $content); |
|
405 | } |
||
406 | 24 | } |
|
407 | 26 | } |
|
408 | |||
409 | 26 | return $content; |
|
410 | } |
||
411 | |||
412 | /** |
||
413 | * Renders single marker view helpers. |
||
414 | * |
||
415 | * @param ViewHelper $viewHelper View helper instance to execute. |
||
416 | * @param string $helperKey The view helper marker key. |
||
417 | * @param string $content Markup that contains the unsubstituted view helper marker. |
||
418 | * @return string Markup with the view helper replaced by the content it returned. |
||
419 | */ |
||
420 | 24 | protected function renderMarkerViewHelper( |
|
421 | ViewHelper $viewHelper, |
||
422 | $helperKey, |
||
423 | $content |
||
424 | ) { |
||
425 | 24 | $viewHelperArgumentLists = $this->getViewHelperArgumentLists($helperKey, |
|
426 | 24 | $content); |
|
427 | |||
428 | 24 | foreach ($viewHelperArgumentLists as $viewHelperArgumentList) { |
|
429 | 24 | $viewHelperArguments = explode('|', $viewHelperArgumentList); |
|
430 | // TODO check whether one of the parameters is a Helper |
||
431 | // itself, if so resolve it before handing it off to the |
||
432 | // actual helper, this way the order in which viewhelpers |
||
433 | // get added to the template do not matter anymore |
||
434 | // may use findViewHelpers() |
||
435 | |||
436 | // checking whether any of the helper arguments should be |
||
437 | // replaced by a variable available to the template |
||
438 | 24 | foreach ($viewHelperArguments as $i => $helperArgument) { |
|
439 | 24 | $lowercaseHelperArgument = strtolower($helperArgument); |
|
440 | 24 | if (array_key_exists($lowercaseHelperArgument, |
|
441 | 24 | $this->variables)) { |
|
442 | $viewHelperArguments[$i] = $this->variables[$lowercaseHelperArgument]; |
||
443 | } |
||
444 | 24 | } |
|
445 | |||
446 | 24 | $viewHelperContent = $viewHelper->execute($viewHelperArguments); |
|
447 | |||
448 | 24 | $content = $this->getTemplateService()->substituteMarker( |
|
449 | 24 | $content, |
|
450 | 24 | '###' . $helperKey . ':' . $viewHelperArgumentList . '###', |
|
451 | $viewHelperContent |
||
452 | 24 | ); |
|
453 | 24 | } |
|
454 | |||
455 | 24 | return $content; |
|
456 | } |
||
457 | |||
458 | /** |
||
459 | * Renders subpart view helpers. |
||
460 | * |
||
461 | * @param SubpartViewHelper $viewHelper View helper instance to execute. |
||
462 | * @param string $helperKey The view helper marker key. |
||
463 | * @param string $content Markup that contains the unsubstituted view helper subpart. |
||
464 | * @return string Markup with the view helper replaced by the content it returned. |
||
465 | */ |
||
466 | protected function renderSubpartViewHelper( |
||
467 | SubpartViewHelper $viewHelper, |
||
468 | $helperKey, |
||
469 | $content |
||
470 | ) { |
||
471 | $viewHelperArgumentLists = $this->getViewHelperArgumentLists($helperKey, |
||
472 | $content); |
||
473 | |||
474 | foreach ($viewHelperArgumentLists as $viewHelperArgumentList) { |
||
475 | $subpartMarker = '###' . $helperKey . ':' . $viewHelperArgumentList . '###'; |
||
476 | |||
477 | $subpart = $this->getTemplateService()->getSubpart( |
||
478 | $content, |
||
479 | $subpartMarker |
||
480 | ); |
||
481 | |||
482 | $viewHelperArguments = explode('|', $viewHelperArgumentList); |
||
483 | |||
484 | $subpartTemplate = clone $this; |
||
485 | $subpartTemplate->setWorkingTemplateContent($subpart); |
||
486 | $viewHelper->setTemplate($subpartTemplate); |
||
487 | |||
488 | try { |
||
489 | $viewHelperContent = $viewHelper->execute($viewHelperArguments); |
||
490 | } catch (\UnexpectedValueException $e) { |
||
491 | $configuration = Util::getSolrConfiguration(); |
||
492 | if ($configuration->getLoggingExceptions()) { |
||
493 | GeneralUtility::devLog('Exception while rendering a viewhelper', |
||
494 | 'solr', 3, [ |
||
495 | $e->__toString() |
||
496 | ]); |
||
497 | } |
||
498 | |||
499 | $viewHelperContent = ''; |
||
500 | } |
||
501 | |||
502 | $content = $this->getTemplateService()->substituteSubpart( |
||
503 | $content, |
||
504 | $subpartMarker, |
||
505 | $viewHelperContent, |
||
506 | false |
||
507 | ); |
||
508 | |||
509 | // there might be more occurrences of the same subpart maker with |
||
510 | // the same arguments but different markup to be used... |
||
511 | // that's the case with the facet subpart view helper f.e. |
||
512 | $furtherOccurrences = strpos($content, $subpartMarker); |
||
513 | if ($furtherOccurrences !== false) { |
||
514 | $content = $this->renderSubpartViewHelper($viewHelper, |
||
515 | $helperKey, $content); |
||
516 | } |
||
517 | } |
||
518 | |||
519 | return $content; |
||
520 | } |
||
521 | |||
522 | /** |
||
523 | * Renders the loop for a given loop name. |
||
524 | * |
||
525 | * @param string $loopName Key from $this->loops to render |
||
526 | */ |
||
527 | 23 | protected function renderLoop($loopName) |
|
528 | { |
||
529 | 23 | $loopContent = ''; |
|
530 | 23 | $loopTemplate = $this->getSubpart('LOOP:' . $loopName); |
|
531 | |||
532 | 23 | $loopContentMarker = 'loop_content:' . $loopName; |
|
533 | 23 | $loopSingleItem = $this->getSubpart($loopContentMarker, $loopTemplate); |
|
534 | 23 | if (empty($loopSingleItem)) { |
|
535 | // backwards compatible fallback for unnamed loops |
||
536 | 23 | $loopContentMarker = 'loop_content'; |
|
537 | 23 | $loopSingleItem = $this->getSubpart($loopContentMarker, |
|
538 | 23 | $loopTemplate); |
|
539 | 23 | } |
|
540 | |||
541 | 23 | $loopMarker = strtoupper($this->loops[$loopName]['marker']); |
|
542 | 23 | $loopVariables = $this->loops[$loopName]['data']; |
|
543 | 23 | $foundMarkers = $this->getMarkersFromTemplate($loopSingleItem, |
|
544 | 23 | $loopMarker . '\.'); |
|
545 | 23 | $loopCount = count($loopVariables); |
|
546 | |||
547 | 23 | if (count($foundMarkers)) { |
|
548 | 23 | $iterationCount = 0; |
|
549 | 23 | foreach ($loopVariables as $value) { |
|
550 | 20 | $resolvedMarkers = $this->resolveVariableMarkers($foundMarkers, |
|
551 | 20 | $value); |
|
552 | 20 | $resolvedMarkers['LOOP_CURRENT_ITERATION_COUNT'] = ++$iterationCount; |
|
553 | |||
554 | // pass the whole object / array / variable as is (serialized though) |
||
555 | 20 | $resolvedMarkers[$loopMarker] = serialize($value); |
|
556 | |||
557 | 20 | $currentIterationContent = $this->getTemplateService()->substituteMarkerArray( |
|
558 | 20 | $loopSingleItem, |
|
559 | 20 | $resolvedMarkers, |
|
560 | '###|###' |
||
561 | 20 | ); |
|
562 | |||
563 | 20 | $inLoopMarkers = $this->getMarkersFromTemplate( |
|
564 | 20 | $currentIterationContent, |
|
565 | 20 | 'LOOP:', |
|
566 | false |
||
567 | 20 | ); |
|
568 | |||
569 | 20 | $inLoopMarkers = $this->filterProtectedLoops($inLoopMarkers); |
|
570 | |||
571 | 20 | $currentIterationContent = $this->processInLoopMarkers( |
|
572 | 20 | $currentIterationContent, |
|
573 | 20 | $loopName, |
|
574 | 20 | $inLoopMarkers, |
|
575 | $value |
||
576 | 20 | ); |
|
577 | |||
578 | 20 | $currentIterationContent = $this->processConditions($currentIterationContent); |
|
579 | |||
580 | 20 | $loopContent .= $currentIterationContent; |
|
581 | 23 | } |
|
582 | 23 | } |
|
583 | |||
584 | 23 | $loopContent = $this->getTemplateService()->substituteSubpart( |
|
585 | 23 | $loopTemplate, |
|
586 | 23 | '###' . strtoupper($loopContentMarker) . '###', |
|
587 | $loopContent |
||
588 | 23 | ); |
|
589 | |||
590 | 23 | $loopContent = $this->getTemplateService()->substituteMarkerArray( |
|
591 | 23 | $loopContent, |
|
592 | 23 | ['LOOP_ELEMENT_COUNT' => $loopCount], |
|
593 | '###|###' |
||
594 | 23 | ); |
|
595 | |||
596 | 23 | $this->workOnSubpart = $this->getTemplateService()->substituteSubpart( |
|
597 | 23 | $this->workOnSubpart, |
|
598 | 23 | '###LOOP:' . strtoupper($loopName) . '###', |
|
599 | $loopContent |
||
600 | 23 | ); |
|
601 | 23 | } |
|
602 | |||
603 | /** |
||
604 | * Processes marker in a loop that start with LOOP:. |
||
605 | * |
||
606 | * This is useful especially for calling view helpers with the current |
||
607 | * iteration's value as a parameter. |
||
608 | * |
||
609 | * @param string $content |
||
610 | * @param string $loopName |
||
611 | * @param array $markers |
||
612 | * @param string $currentIterationValue |
||
613 | * @return string |
||
614 | */ |
||
615 | 20 | protected function processInLoopMarkers( |
|
616 | $content, |
||
617 | $loopName, |
||
618 | array $markers, |
||
619 | $currentIterationValue |
||
620 | ) { |
||
621 | 20 | foreach ($markers as $marker) { |
|
622 | list($helperName, $helperArguments) = explode(':', $marker); |
||
623 | |||
624 | $helperName = strtolower($helperName); |
||
625 | $helperArguments = explode('|', $helperArguments); |
||
626 | |||
627 | // checking whether any of the helper arguments should be |
||
628 | // replaced by the current iteration's value |
||
629 | if (isset($this->loops[$loopName])) { |
||
630 | foreach ($helperArguments as $i => $helperArgument) { |
||
631 | if (strtoupper($this->loops[$loopName]['marker']) == strtoupper($helperArgument)) { |
||
632 | $helperArguments[$i] = $currentIterationValue; |
||
633 | } |
||
634 | } |
||
635 | } |
||
636 | |||
637 | if (array_key_exists($helperName, $this->helpers)) { |
||
638 | $markerContent = $this->helpers[$helperName]->execute($helperArguments); |
||
639 | } else { |
||
640 | throw new \RuntimeException( |
||
641 | 'No matching view helper found for marker "' . $marker . '".', |
||
642 | 1311005284 |
||
643 | ); |
||
644 | } |
||
645 | |||
646 | $content = str_replace('###LOOP:' . $marker . '###', $markerContent, |
||
647 | $content); |
||
648 | 20 | } |
|
649 | |||
650 | 20 | return $content; |
|
651 | } |
||
652 | |||
653 | /** |
||
654 | * Some marker subparts must be protected and only rendered by their |
||
655 | * according commands. This method filters these protected markers from |
||
656 | * others when rendering loops so that they are not replaced and left in |
||
657 | * the template for rendering by the correct command. |
||
658 | * |
||
659 | * @param array $loopMarkers An array of loop markers found during rendering of a loop. |
||
660 | * @return array The array with protected subpart markers removed. |
||
661 | */ |
||
662 | 20 | protected function filterProtectedLoops($loopMarkers) |
|
663 | { |
||
664 | 20 | $protectedMarkers = ['result_documents']; |
|
665 | |||
666 | 20 | foreach ($loopMarkers as $key => $loopMarker) { |
|
667 | if (in_array(strtolower($loopMarker), $protectedMarkers)) { |
||
668 | unset($loopMarkers[$key]); |
||
669 | } |
||
670 | 20 | } |
|
671 | |||
672 | 20 | return $loopMarkers; |
|
673 | } |
||
674 | |||
675 | /** |
||
676 | * Processes conditions: finds and evaluates them in HTML code. |
||
677 | * |
||
678 | * @param string $content HTML |
||
679 | * @return string |
||
680 | */ |
||
681 | 26 | protected function processConditions($content) |
|
682 | { |
||
683 | // find conditions |
||
684 | 26 | $conditions = $this->findConditions($content); |
|
685 | |||
686 | // evaluate conditions |
||
687 | 26 | foreach ($conditions as $condition) { |
|
688 | 20 | if ($this->isVariableMarker($condition['comparand1']) |
|
689 | 20 | || $this->isVariableMarker($condition['comparand2']) |
|
690 | 20 | ) { |
|
691 | // unresolved marker => skip, will be resolved later |
||
692 | continue; |
||
693 | } |
||
694 | |||
695 | 20 | $conditionResult = $this->evaluateCondition( |
|
696 | 20 | $condition['comparand1'], |
|
697 | 20 | $condition['comparand2'], |
|
698 | 20 | $condition['operator'] |
|
699 | 20 | ); |
|
700 | |||
701 | 20 | if ($conditionResult) { |
|
702 | // if condition evaluates to TRUE, simply replace it with |
||
703 | // the original content to have the surrounding markers removed |
||
704 | 3 | $content = $this->getTemplateService()->substituteSubpart( |
|
705 | 3 | $content, |
|
706 | 3 | $condition['marker'], |
|
707 | 3 | $condition['content'] |
|
708 | 3 | ); |
|
709 | 3 | } else { |
|
710 | // if condition evaluates to FALSE, remove the content from the template |
||
711 | 20 | $content = $this->getTemplateService()->substituteSubpart( |
|
712 | 20 | $content, |
|
713 | 20 | $condition['marker'], |
|
714 | '' |
||
715 | 20 | ); |
|
716 | } |
||
717 | 26 | } |
|
718 | |||
719 | 26 | return $content; |
|
720 | } |
||
721 | |||
722 | /** |
||
723 | * Finds conditions in HTML code. |
||
724 | * |
||
725 | * Conditions are subparts with markers in the form of |
||
726 | * |
||
727 | * ###IF:comparand1|operator|comparand2### |
||
728 | * Some content only visible if the condition evaluates as TRUE |
||
729 | * ###IF:comparand1|operator|comparand2### |
||
730 | * |
||
731 | * The returned result is an array of arrays describing a found condition. |
||
732 | * Each conditions is described as follows: |
||
733 | * [marker] the complete marker used to specify the condition |
||
734 | * [content] the content wrapped by the condition |
||
735 | * [operator] the condition operator |
||
736 | * [comparand1] and [comparand2] the comparands. |
||
737 | * |
||
738 | * @param string $content HTML |
||
739 | * @return array An array describing the conditions found |
||
740 | */ |
||
741 | 26 | protected function findConditions($content) |
|
742 | { |
||
743 | 26 | $conditions = []; |
|
744 | 26 | $ifMarkers = $this->getViewHelperArgumentLists('IF', $content, false); |
|
745 | |||
746 | 26 | foreach ($ifMarkers as $ifMarker) { |
|
747 | 20 | list($comparand1, $operator, $comparand2) = explode('|', $ifMarker); |
|
748 | |||
749 | 20 | $ifContent = $this->getTemplateService()->getSubpart( |
|
750 | 20 | $content, |
|
751 | 20 | '###IF:' . $ifMarker . '###' |
|
752 | 20 | ); |
|
753 | |||
754 | 20 | $conditions[] = [ |
|
755 | 20 | 'marker' => '###IF:' . $ifMarker . '###', |
|
756 | 20 | 'content' => $ifContent, |
|
757 | 20 | 'operator' => trim($operator), |
|
758 | 20 | 'comparand1' => $comparand1, |
|
759 | 'comparand2' => $comparand2 |
||
760 | 20 | ]; |
|
761 | 26 | } |
|
762 | |||
763 | 26 | return $conditions; |
|
764 | } |
||
765 | |||
766 | /** |
||
767 | * Evaluates conditions. |
||
768 | * |
||
769 | * Supported operators are ==, !=, <, <=, >, >=, % |
||
770 | * |
||
771 | * @param string $comparand1 First comparand |
||
772 | * @param string $comparand2 Second comparand |
||
773 | * @param string $operator Operator |
||
774 | * @return bool Boolean evaluation of the condition. |
||
775 | * @throws \InvalidArgumentException for unknown $operator |
||
776 | */ |
||
777 | 20 | protected function evaluateCondition($comparand1, $comparand2, $operator) |
|
778 | { |
||
779 | switch ($operator) { |
||
780 | 20 | case '==': |
|
781 | 20 | $conditionResult = ($comparand1 == $comparand2); |
|
782 | 20 | break; |
|
783 | 20 | case '!=': |
|
784 | 20 | $conditionResult = ($comparand1 != $comparand2); |
|
785 | 20 | break; |
|
786 | case '<': |
||
787 | $conditionResult = ($comparand1 < $comparand2); |
||
788 | break; |
||
789 | case '<=': |
||
790 | $conditionResult = ($comparand1 <= $comparand2); |
||
791 | break; |
||
792 | case '>': |
||
793 | $conditionResult = ($comparand1 > $comparand2); |
||
794 | break; |
||
795 | case '>=': |
||
796 | $conditionResult = ($comparand1 >= $comparand2); |
||
797 | break; |
||
798 | case '%': |
||
799 | $conditionResult = ($comparand1 % $comparand2); |
||
800 | break; |
||
801 | default: |
||
802 | throw new \InvalidArgumentException( |
||
803 | 'Unknown condition operator "' . htmlspecialchars($operator) . '"', |
||
804 | 1344340207 |
||
805 | ); |
||
806 | } |
||
807 | |||
808 | // explicit casting, just in case |
||
809 | 20 | $conditionResult = (boolean)$conditionResult; |
|
810 | |||
811 | 20 | return $conditionResult; |
|
812 | } |
||
813 | |||
814 | /** |
||
815 | * Resolves variables to marker. Markers can be simple markers like |
||
816 | * ###MY_MARKER## or "nested" markers which divide their sub values by a |
||
817 | * dot: ###MY_MARKER.MY_VALUE### ###MY_MARKER.MY_OTHER_VALUE###. |
||
818 | * |
||
819 | * @param array $markers array with markers to resolve |
||
820 | * @param mixed $variableValue the marker's value, which can be an array of values, an object with certain getter methods or a simple string |
||
821 | * @return array with marker as index and value for it |
||
822 | */ |
||
823 | 25 | protected function resolveVariableMarkers(array $markers, $variableValue) |
|
824 | { |
||
825 | 25 | $resolvedMarkers = []; |
|
826 | |||
827 | 25 | $normalizedKeysArray = []; |
|
828 | 25 | foreach ($variableValue as $key => $value) { |
|
829 | 25 | $key = $this->normalizeString($key); |
|
830 | 25 | $normalizedKeysArray[$key] = $value; |
|
831 | 25 | } |
|
832 | |||
833 | 25 | foreach ($markers as $marker) { |
|
834 | 25 | $dotPosition = strpos($marker, '.'); |
|
835 | |||
836 | 25 | if ($dotPosition !== false) { |
|
837 | 25 | $resolvedValue = null; |
|
838 | |||
839 | // the marker contains a dot, thus we have to resolve the |
||
840 | // second part of the marker |
||
841 | 25 | $valueSelector = substr($marker, $dotPosition + 1); |
|
842 | 25 | $valueSelector = $this->normalizeString($valueSelector); |
|
843 | |||
844 | 25 | if (is_array($variableValue) && array_key_exists($valueSelector, |
|
845 | 25 | $normalizedKeysArray) |
|
846 | 25 | ) { |
|
847 | 25 | $resolvedValue = $normalizedKeysArray[$valueSelector]; |
|
848 | 25 | } elseif (is_object($variableValue)) { |
|
849 | $resolveMethod = 'get' . Util::camelize($valueSelector); |
||
850 | $resolvedValue = $variableValue->$resolveMethod(); |
||
851 | } |
||
852 | 25 | } else { |
|
853 | $resolvedValue = $variableValue[strtolower($marker)]; |
||
854 | } |
||
855 | |||
856 | 25 | if (is_null($resolvedValue)) { |
|
857 | 23 | if ($this->debugMode) { |
|
858 | $resolvedValue = '!!! Marker "' . $marker . '" could not be resolved.'; |
||
859 | } else { |
||
860 | 23 | $resolvedValue = ''; |
|
861 | } |
||
862 | 23 | } |
|
863 | |||
864 | 25 | if (is_array($resolvedValue)) { |
|
865 | // handling multivalue fields, @see ApacheSolrForTypo3\Solr\ViewHelper\Multivalue |
||
866 | $resolvedValue = serialize($resolvedValue); |
||
867 | } |
||
868 | |||
869 | 25 | $resolvedMarkers[$marker] = $resolvedValue; |
|
870 | 25 | } |
|
871 | |||
872 | 25 | return $resolvedMarkers; |
|
873 | } |
||
874 | |||
875 | /** |
||
876 | * Normalizes the various input formats of the markers to a common format. |
||
877 | * |
||
878 | * Example: |
||
879 | * |
||
880 | * FILE_MIME_TYPE_STRING_S => file_mime_type_string_s |
||
881 | * file_mime_type_string_s => file_mime_type_string_s |
||
882 | * fileMimeType_stringS => file_mime_type_string_s |
||
883 | * |
||
884 | * @param string $selector A string in upper case with underscores, lowercase with underscores, camel case, or a mix. |
||
885 | * @return string A lowercased, underscorized version of the given string |
||
886 | */ |
||
887 | 25 | protected function normalizeString($selector) |
|
888 | { |
||
889 | 25 | static $normalizeCache = []; |
|
890 | |||
891 | 25 | if (!isset($normalizeCache[$selector])) { |
|
892 | 25 | $originalSelector = $selector; |
|
893 | 25 | $selector = str_replace('-', '_', $selector); |
|
894 | |||
895 | // when switching from lowercase to Uppercase in camel cased |
||
896 | // strings, insert an underscore |
||
897 | 25 | $underscorized = preg_replace('/([a-z])([A-Z])/', '\\1_\\2', |
|
898 | 25 | $selector); |
|
899 | |||
900 | // for all other cases - all upper or all lower case |
||
901 | // we simply lowercase the complete string |
||
902 | 25 | $normalizeCache[$originalSelector] = strtolower($underscorized); |
|
903 | 25 | } |
|
904 | |||
905 | 25 | return $normalizeCache[$selector]; |
|
906 | } |
||
907 | |||
908 | /** |
||
909 | * Selects a subpart to work on / to apply all operations to. |
||
910 | * |
||
911 | * @param string $subpartName subpart name |
||
912 | */ |
||
913 | 26 | public function workOnSubpart($subpartName) |
|
917 | |||
918 | /** |
||
919 | * Retrieves a subpart from the given html template. |
||
920 | * |
||
921 | * @param string $subpartName subpart marker name, can be lowercase, doesn't need the ### delimiters |
||
922 | * @param string $alternativeTemplate |
||
923 | * @return string the html subpart |
||
924 | */ |
||
925 | 26 | public function getSubpart($subpartName, $alternativeTemplate = '') |
|
926 | { |
||
927 | 26 | $template = $this->workOnSubpart; |
|
928 | |||
929 | // set alternative template to work on |
||
930 | 26 | if (!empty($alternativeTemplate)) { |
|
931 | 26 | $template = $alternativeTemplate; |
|
932 | 26 | } |
|
933 | |||
934 | 26 | $subpart = $this->getTemplateService()->getSubpart( |
|
935 | 26 | $template, |
|
936 | 26 | '###' . strtoupper($subpartName) . '###' |
|
937 | 26 | ); |
|
938 | |||
939 | 26 | return $subpart; |
|
940 | } |
||
941 | |||
942 | /** |
||
943 | * Sets a marker's value. |
||
944 | * |
||
945 | * @param string $marker marker name, can be lower case, doesn't need the ### delimiters |
||
946 | * @param string $content the marker's value |
||
947 | */ |
||
948 | public function addMarker($marker, $content) |
||
952 | |||
953 | /** |
||
954 | * Sets an array of markers with their values. |
||
955 | * |
||
956 | * @param array $markers array of markers |
||
957 | */ |
||
958 | public function addMarkerArray(array $markers) |
||
959 | { |
||
960 | foreach ($markers as $marker => $content) { |
||
961 | $this->addMarker($marker, $content); |
||
962 | } |
||
963 | } |
||
964 | |||
965 | /** |
||
966 | * Sets a subpart's value. |
||
967 | * |
||
968 | * @param string $subpartMarker subpart name, can be lower case, doesn't need the ### delimiters |
||
969 | * @param string $content the subpart's value |
||
970 | */ |
||
971 | 25 | public function addSubpart($subpartMarker, $content) |
|
975 | |||
976 | /** |
||
977 | * Assigns a variable to the html template. |
||
978 | * Simple variables can be used like regular markers or in the form |
||
979 | * VAR:"VARIABLE_NAME" (without the quotes). Objects can be used in the |
||
980 | * form VAR:"OBJECT_NAME"."PROPERTY_NAME" (without the quotes). |
||
981 | * |
||
982 | * @param string $key variable key |
||
983 | * @param mixed $value variable value |
||
984 | */ |
||
985 | 25 | public function addVariable($key, $value) |
|
991 | |||
992 | /** |
||
993 | * Adds a named loop. The given array is looped over in the template. |
||
994 | * |
||
995 | * @param string $loopName loop name |
||
996 | * @param string $markerName |
||
997 | * @param array $variables variables array |
||
998 | */ |
||
999 | 23 | public function addLoop($loopName, $markerName, array $variables) |
|
1006 | |||
1007 | /** |
||
1008 | * Gets a list of Markers from the selected subpart. |
||
1009 | * |
||
1010 | * @param string $template marker name |
||
1011 | * @param string $markerPrefix |
||
1012 | * @param bool $capturePrefix |
||
1013 | * @return array Array of markers |
||
1014 | */ |
||
1015 | 26 | public function getMarkersFromTemplate( |
|
1016 | $template, |
||
1017 | $markerPrefix = '', |
||
1018 | $capturePrefix = true |
||
1019 | ) { |
||
1020 | 26 | $regex = '!###([A-Z0-9_-|:.]*)\###!is'; |
|
1021 | |||
1022 | 26 | if (!empty($markerPrefix)) { |
|
1023 | 23 | if ($capturePrefix) { |
|
1024 | 23 | $regex = '!###(' . strtoupper($markerPrefix) . '[A-Z0-9_-|:.]*)\###!is'; |
|
1025 | 23 | } else { |
|
1026 | 20 | $regex = '!###' . strtoupper($markerPrefix) . '([A-Z0-9_-|:.]*)\###!is'; |
|
1027 | } |
||
1028 | 23 | } |
|
1029 | |||
1030 | 26 | preg_match_all($regex, $template, $match); |
|
1031 | 26 | $markers = array_unique($match[1]); |
|
1032 | |||
1033 | 26 | return $markers; |
|
1034 | } |
||
1035 | |||
1036 | /** |
||
1037 | * returns the markers found in the template |
||
1038 | * |
||
1039 | * @param string $markerPrefix a prefix to limit the result to markers beginning with the specified prefix |
||
1040 | * @return array Array of markers names |
||
1041 | */ |
||
1042 | 26 | public function findMarkers($markerPrefix = '') |
|
1043 | { |
||
1044 | 26 | return $this->getMarkersFromTemplate($this->workOnSubpart, |
|
1045 | 26 | $markerPrefix); |
|
1046 | } |
||
1047 | |||
1048 | /** |
||
1049 | * Gets a list of view helper marker arguments for a given view helper from |
||
1050 | * the selected subpart. |
||
1051 | * |
||
1052 | * @param string $helperMarker marker name, can be lower case, doesn't need the ### delimiters |
||
1053 | * @param string $subpart subpart markup to search in |
||
1054 | * @param bool $removeDuplicates Optionally determines whether duplicate view helpers are removed. Defaults to TRUE. |
||
1055 | * @return array Array of markers |
||
1056 | */ |
||
1057 | 26 | protected function getViewHelperArgumentLists($helperMarker, $subpart, $removeDuplicates = true) |
|
1058 | { |
||
1059 | 26 | $markers = self::extractViewHelperArguments($helperMarker, $subpart); |
|
1060 | |||
1061 | 26 | if ($removeDuplicates) { |
|
1062 | 24 | $markers = array_unique($markers); |
|
1063 | 24 | } |
|
1064 | |||
1065 | 26 | return $markers; |
|
1066 | } |
||
1067 | |||
1068 | /** |
||
1069 | * This helper function is used to extract all ViewHelper arguments by a name of the ViewHelper. |
||
1070 | * |
||
1071 | * Arguments can be: strings or even other markers. |
||
1072 | * |
||
1073 | * @param string $helperMarker |
||
1074 | * @param string $subpart |
||
1075 | * @return array |
||
1076 | */ |
||
1077 | 36 | public static function extractViewHelperArguments($helperMarker, $subpart) |
|
1090 | |||
1091 | /** |
||
1092 | * Finds view helpers used in the current subpart being worked on. |
||
1093 | * |
||
1094 | * @param string $content A string that should be searched for view helpers. |
||
1095 | * @return array A list of view helper names used in the template. |
||
1096 | */ |
||
1097 | 26 | public function findViewHelpers($content) |
|
1098 | { |
||
1099 | 26 | preg_match_all('!###([\w]+):.*?\###!is', $content, $match); |
|
1100 | 26 | $viewHelpers = array_unique($match[1]); |
|
1101 | |||
1102 | // remove / protect LOOP, LOOP_CONTENT subparts |
||
1103 | 26 | $loopIndex = array_search('LOOP', $viewHelpers); |
|
1104 | 26 | if ($loopIndex !== false) { |
|
1105 | 1 | unset($viewHelpers[$loopIndex]); |
|
1106 | 1 | } |
|
1107 | 26 | $loopContentIndex = array_search('LOOP_CONTENT', $viewHelpers); |
|
1108 | 26 | if ($loopContentIndex !== false) { |
|
1109 | unset($viewHelpers[$loopContentIndex]); |
||
1110 | } |
||
1111 | |||
1112 | // remove / protect IF subparts |
||
1113 | 26 | $ifIndex = array_search('IF', $viewHelpers); |
|
1114 | 26 | if ($ifIndex !== false) { |
|
1115 | 3 | unset($viewHelpers[$ifIndex]); |
|
1116 | 3 | } |
|
1117 | |||
1118 | 26 | return $viewHelpers; |
|
1119 | } |
||
1120 | |||
1121 | /** |
||
1122 | * Gets a list of given markers from the selected subpart. |
||
1123 | * |
||
1124 | * @param string $variableMarker marker name, can be lower case, doesn't need the ### delimiters |
||
1125 | * @param string $subpart subpart name |
||
1126 | * @return array array of markers |
||
1127 | */ |
||
1128 | 25 | public function getVariableMarkers($variableMarker, $subpart) |
|
1129 | { |
||
1130 | 25 | preg_match_all( |
|
1131 | 25 | '!###(' . $variableMarker . '\.[A-Z0-9_-]*)\###!is', |
|
1132 | 25 | $subpart, |
|
1133 | $match |
||
1134 | 25 | ); |
|
1135 | 25 | $markers = array_unique($match[1]); |
|
1136 | |||
1137 | 25 | return $markers; |
|
1138 | } |
||
1139 | |||
1140 | /** |
||
1141 | * Checks whether a given string is a variable marker |
||
1142 | * |
||
1143 | * @param string $potentialVariableMarker String to check whether it is a variable marker |
||
1144 | * @return bool TRUE if the string is identified being a variable marker, FALSE otherwise |
||
1145 | */ |
||
1146 | 20 | public function isVariableMarker($potentialVariableMarker) |
|
1153 | |||
1154 | /** |
||
1155 | * @param $templateContent |
||
1156 | * @return mixed |
||
1157 | */ |
||
1158 | 25 | public function setTemplateContent($templateContent) |
|
1162 | |||
1163 | public function getTemplateContent() |
||
1167 | |||
1168 | 25 | public function getWorkOnSubpart() |
|
1172 | |||
1173 | /** |
||
1174 | * Sets the debug mode on or off. |
||
1175 | * |
||
1176 | * @param bool $mode debug mode, TRUE to enable debug mode, FALSE to turn off again, off by default |
||
1177 | */ |
||
1178 | public function setDebugMode($mode) |
||
1182 | } |
||
1183 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: