[!!!][TASK] Refactor property access in compiled fluid templates
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / Core / Parser / SyntaxTree / ObjectAccessorNode.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Core\Parser\SyntaxTree;
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 /**
15 * A node which handles object access. This means it handles structures like {object.accessor.bla}
16 */
17 class ObjectAccessorNode extends \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode {
18
19 /**
20 * Object path which will be called. Is a list like "post.name.email"
21 *
22 * @var string
23 */
24 protected $objectPath;
25
26 /**
27 * Constructor. Takes an object path as input.
28 *
29 * The first part of the object path has to be a variable in the
30 * TemplateVariableContainer.
31 *
32 * @param string $objectPath An Object Path, like object1.object2.object3
33 */
34 public function __construct($objectPath) {
35 $this->objectPath = $objectPath;
36 }
37
38 /**
39 * Internally used for building up cached templates; do not use directly!
40 *
41 * @return string
42 * @internal
43 */
44 public function getObjectPath() {
45 return $this->objectPath;
46 }
47
48 /**
49 * Evaluate this node and return the correct object.
50 *
51 * Handles each part (denoted by .) in $this->objectPath in the following order:
52 * - call appropriate getter
53 * - call public property, if exists
54 * - fail
55 *
56 * The first part of the object path has to be a variable in the
57 * TemplateVariableContainer.
58 *
59 * @param \TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface $renderingContext
60 * @return mixed The evaluated object, can be any object type.
61 */
62 public function evaluate(\TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface $renderingContext) {
63 return self::getPropertyPath($renderingContext->getTemplateVariableContainer(), $this->objectPath, $renderingContext);
64 }
65
66 /**
67 * Gets a property path from a given object or array.
68 *
69 * If propertyPath is "bla.blubb", then we first call getProperty($object, 'bla'),
70 * and on the resulting object we call getProperty(..., 'blubb').
71 *
72 * For arrays the keys are checked likewise.
73 *
74 * @param mixed $subject An object or array
75 * @param string $propertyPath
76 * @param \TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface $renderingContext
77 * @return mixed Value of the property
78 */
79 static public function getPropertyPath($subject, $propertyPath, \TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface $renderingContext) {
80 $propertyPathSegments = explode('.', $propertyPath);
81 foreach ($propertyPathSegments as $pathSegment) {
82 if ($subject === NULL || is_scalar($subject)) {
83 return NULL;
84 }
85 $propertyExists = FALSE;
86 $propertyValue = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyInternal($subject, $pathSegment, FALSE, $propertyExists);
87 if ($propertyExists !== TRUE && (is_array($subject) || $subject instanceof \ArrayAccess) && isset($subject[$pathSegment])) {
88 $subject = $subject[$pathSegment];
89 } else {
90 $subject = $propertyValue;
91 }
92 }
93 return $subject;
94 }
95
96 /**
97 * get property accessors for a given property path.
98 *
99 * @param mixed $subject
100 * @param array $propertyPathSegments
101 * @return null|string
102 */
103 static public function getPropertyAccessors($subject, $propertyPathSegments) {
104 $accessors = array();
105 foreach ($propertyPathSegments as $pathSegment) {
106 if ($subject === NULL || is_scalar($subject)) {
107 return NULL;
108 }
109
110 if (is_array($subject)) {
111 $accessors[] = array('t' => 'a', 'c' => $pathSegment);
112 }
113
114 if (is_object($subject)) {
115 $getterMethodName = 'get' . ucfirst($pathSegment);
116 if (!is_callable(array($subject, $getterMethodName))) {
117 $getterMethodName = 'is' . ucfirst($pathSegment);
118 }
119 if (!is_callable(array($subject, $getterMethodName))) {
120 $getterMethodName = 'has' . ucfirst($pathSegment);
121 }
122 if (!is_callable(array($subject, $getterMethodName))) {
123 return NULL;
124 }
125
126 $accessors[] = array('t' => 'o', 'c' => $getterMethodName);
127 }
128
129 $subject = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyInternal($subject, $pathSegment, FALSE, $exists);
130 }
131
132 return $accessors;
133 }
134
135 /**
136 * Resolves property accessors from compiled templates.
137 *
138 * @param mixed $subject
139 * @param array $accessors
140 * @return mixed
141 */
142 static public function resolvePropertyAccessors($subject, array $accessors) {
143 foreach ($accessors as $accessor) {
144 if ($subject === NULL) {
145 return NULL;
146 }
147
148 if ($accessor['t'] === 'a') {
149 $subject = $subject[$accessor['c']];
150 }
151
152 if ($accessor['t'] === 'o') {
153 $subject = $subject->{$accessor['c']}();
154 }
155 }
156
157 return $subject;
158 }
159 }