[!!!][TASK] Refactor property access in compiled fluid templates
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / Core / Compiler / AbstractCompiledTemplate.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Core\Compiler;
3
4 /* *
5 * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License, either version 3 *
9 * of the License, or (at your option) any later version. *
10 * *
11 * The TYPO3 project - inspiring people to share! *
12 * */
13 /**
14 * Abstract Fluid Compiled template.
15 *
16 * INTERNAL!!
17 */
18 abstract class AbstractCompiledTemplate implements \TYPO3\CMS\Fluid\Core\Parser\ParsedTemplateInterface {
19
20 /**
21 * @var array
22 */
23 protected $viewHelpersByPositionAndContext = array();
24
25 // These tokens are replaced by the Backporter for implementing different behavior in TYPO3 v4
26 /**
27 * @var \TYPO3\CMS\Extbase\Object\Container\Container
28 */
29 static protected $objectContainer;
30
31 /**
32 * @var string
33 */
34 static protected $defaultEncoding = NULL;
35
36 /**
37 * Public such that it is callable from within closures
38 *
39 * @param int $uniqueCounter
40 * @param \TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface $renderingContext
41 * @param string $viewHelperName
42 * @return \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
43 * @internal
44 */
45 public function getViewHelper($uniqueCounter, \TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface $renderingContext, $viewHelperName) {
46 if (self::$objectContainer === NULL) {
47 self::$objectContainer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\Container\Container::class);
48 }
49 if (isset($this->viewHelpersByPositionAndContext[$uniqueCounter])) {
50 if ($this->viewHelpersByPositionAndContext[$uniqueCounter]->contains($renderingContext)) {
51 $viewHelper = $this->viewHelpersByPositionAndContext[$uniqueCounter][$renderingContext];
52 $viewHelper->resetState();
53 return $viewHelper;
54 } else {
55 $viewHelperInstance = self::$objectContainer->getInstance($viewHelperName);
56 if ($viewHelperInstance instanceof \TYPO3\CMS\Core\SingletonInterface) {
57 $viewHelperInstance->resetState();
58 }
59 $this->viewHelpersByPositionAndContext[$uniqueCounter]->attach($renderingContext, $viewHelperInstance);
60 return $viewHelperInstance;
61 }
62 } else {
63 $this->viewHelpersByPositionAndContext[$uniqueCounter] = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Persistence\ObjectStorage::class);
64 $viewHelperInstance = self::$objectContainer->getInstance($viewHelperName);
65 if ($viewHelperInstance instanceof \TYPO3\CMS\Core\SingletonInterface) {
66 $viewHelperInstance->resetState();
67 }
68 $this->viewHelpersByPositionAndContext[$uniqueCounter]->attach($renderingContext, $viewHelperInstance);
69 return $viewHelperInstance;
70 }
71 }
72
73 /**
74 * @return bool
75 */
76 public function isCompilable() {
77 return FALSE;
78 }
79
80 /**
81 * @return bool
82 */
83 public function isCompiled() {
84 return TRUE;
85 }
86
87 /**
88 * @return string
89 * @internal
90 */
91 static public function resolveDefaultEncoding() {
92 if (static::$defaultEncoding === NULL) {
93 static::$defaultEncoding = 'UTF-8';
94 }
95 return static::$defaultEncoding;
96 }
97
98 /**
99 * @param array $replacements
100 */
101 public function replacePropertyAccessors($replacements, $file) {
102 $code = file_get_contents($file);
103
104 foreach ($replacements as $pattern => $replacementCode) {
105 $code = preg_replace($pattern, $replacementCode, $code);
106 }
107
108 file_put_contents($file, $code);
109 }
110
111 }