[TASK] Sync CMS Fluid with Flow Fluid 1.1 (part1)
[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 object 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 $propertyExists = FALSE;
83 $propertyValue = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyInternal($subject, $pathSegment, FALSE, $propertyExists);
84 if ($propertyExists !== TRUE && (is_array($subject) || $subject instanceof \ArrayAccess) && isset($subject[$pathSegment])) {
85 $subject = $subject[$pathSegment];
86 } else {
87 $subject = $propertyValue;
88 }
89
90 if ($subject instanceof \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\RenderingContextAwareInterface) {
91 $subject->setRenderingContext($renderingContext);
92 }
93 }
94 return $subject;
95 }
96 }
97
98 ?>