f92d070ed944a5faa4bbd695b73744896dd4278a
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / Generic / LazyLoadingProxy.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Persistence\Generic;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface;
18
19 /**
20 * A proxy that can replace any object and replaces itself in it's parent on
21 * first access (call, get, set, isset, unset).
22 */
23 class LazyLoadingProxy implements \Iterator, \TYPO3\CMS\Extbase\Persistence\Generic\LoadingStrategyInterface
24 {
25 /**
26 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
27 */
28 protected $dataMapper;
29
30 /**
31 * The object this property is contained in.
32 *
33 * @var DomainObjectInterface
34 */
35 private $parentObject;
36
37 /**
38 * The name of the property represented by this proxy.
39 *
40 * @var string
41 */
42 private $propertyName;
43
44 /**
45 * The raw field value.
46 *
47 * @var mixed
48 */
49 private $fieldValue;
50
51 /**
52 * @param \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper $dataMapper
53 */
54 public function injectDataMapper(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper $dataMapper)
55 {
56 $this->dataMapper = $dataMapper;
57 }
58
59 /**
60 * Constructs this proxy instance.
61 *
62 * @param DomainObjectInterface $parentObject The object instance this proxy is part of
63 * @param string $propertyName The name of the proxied property in it's parent
64 * @param mixed $fieldValue The raw field value.
65 */
66 public function __construct($parentObject, $propertyName, $fieldValue)
67 {
68 $this->parentObject = $parentObject;
69 $this->propertyName = $propertyName;
70 $this->fieldValue = $fieldValue;
71 }
72
73 /**
74 * Populate this proxy by asking the $population closure.
75 *
76 * @return object The instance (hopefully) returned
77 */
78 public function _loadRealInstance()
79 {
80 // this check safeguards against a proxy being activated multiple times
81 // usually that does not happen, but if the proxy is held from outside
82 // its parent ... the result would be weird.
83 if ($this->parentObject->_getProperty($this->propertyName) instanceof \TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy) {
84 $objects = $this->dataMapper->fetchRelated($this->parentObject, $this->propertyName, $this->fieldValue, false, false);
85 $propertyValue = $this->dataMapper->mapResultToPropertyValue($this->parentObject, $this->propertyName, $objects);
86 $this->parentObject->_setProperty($this->propertyName, $propertyValue);
87 $this->parentObject->_memorizeCleanState($this->propertyName);
88 return $propertyValue;
89 } else {
90 return $this->parentObject->_getProperty($this->propertyName);
91 }
92 }
93
94 /**
95 * Magic method call implementation.
96 *
97 * @param string $methodName The name of the property to get
98 * @param array $arguments The arguments given to the call
99 * @return mixed
100 */
101 public function __call($methodName, $arguments)
102 {
103 $realInstance = $this->_loadRealInstance();
104 if (!is_object($realInstance)) {
105 return null;
106 }
107 return call_user_func_array([$realInstance, $methodName], $arguments);
108 }
109
110 /**
111 * Magic get call implementation.
112 *
113 * @param string $propertyName The name of the property to get
114 * @return mixed
115 */
116 public function __get($propertyName)
117 {
118 $realInstance = $this->_loadRealInstance();
119 return $realInstance->{$propertyName};
120 }
121
122 /**
123 * Magic set call implementation.
124 *
125 * @param string $propertyName The name of the property to set
126 * @param mixed $value The value for the property to set
127 */
128 public function __set($propertyName, $value)
129 {
130 $realInstance = $this->_loadRealInstance();
131 $realInstance->{$propertyName} = $value;
132 }
133
134 /**
135 * Magic isset call implementation.
136 *
137 * @param string $propertyName The name of the property to check
138 * @return bool
139 */
140 public function __isset($propertyName)
141 {
142 $realInstance = $this->_loadRealInstance();
143 return isset($realInstance->{$propertyName});
144 }
145
146 /**
147 * Magic unset call implementation.
148 *
149 * @param string $propertyName The name of the property to unset
150 */
151 public function __unset($propertyName)
152 {
153 $realInstance = $this->_loadRealInstance();
154 unset($realInstance->{$propertyName});
155 }
156
157 /**
158 * Magic toString call implementation.
159 *
160 * @return string
161 */
162 public function __toString()
163 {
164 $realInstance = $this->_loadRealInstance();
165 return $realInstance->__toString();
166 }
167
168 /**
169 * Returns the current value of the storage array
170 *
171 * @return mixed
172 */
173 public function current()
174 {
175 $realInstance = $this->_loadRealInstance();
176 return current($realInstance);
177 }
178
179 /**
180 * Returns the current key storage array
181 *
182 * @return int
183 */
184 public function key()
185 {
186 $realInstance = $this->_loadRealInstance();
187 return key($realInstance);
188 }
189
190 /**
191 * Returns the next position of the storage array
192 */
193 public function next()
194 {
195 $realInstance = $this->_loadRealInstance();
196 next($realInstance);
197 }
198
199 /**
200 * Resets the array pointer of the storage
201 */
202 public function rewind()
203 {
204 $realInstance = $this->_loadRealInstance();
205 reset($realInstance);
206 }
207
208 /**
209 * Checks if the array pointer of the storage points to a valid position
210 *
211 * @return bool
212 */
213 public function valid()
214 {
215 return $this->current() !== false;
216 }
217 }