b4001c1ea1e2c3531a282c45257153653b362934
[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 }
90 return $this->parentObject->_getProperty($this->propertyName);
91 }
92
93 /**
94 * Magic method call implementation.
95 *
96 * @param string $methodName The name of the property to get
97 * @param array $arguments The arguments given to the call
98 * @return mixed
99 */
100 public function __call($methodName, $arguments)
101 {
102 $realInstance = $this->_loadRealInstance();
103 if (!is_object($realInstance)) {
104 return null;
105 }
106 return call_user_func_array([$realInstance, $methodName], $arguments);
107 }
108
109 /**
110 * Magic get call implementation.
111 *
112 * @param string $propertyName The name of the property to get
113 * @return mixed
114 */
115 public function __get($propertyName)
116 {
117 $realInstance = $this->_loadRealInstance();
118 return $realInstance->{$propertyName};
119 }
120
121 /**
122 * Magic set call implementation.
123 *
124 * @param string $propertyName The name of the property to set
125 * @param mixed $value The value for the property to set
126 */
127 public function __set($propertyName, $value)
128 {
129 $realInstance = $this->_loadRealInstance();
130 $realInstance->{$propertyName} = $value;
131 }
132
133 /**
134 * Magic isset call implementation.
135 *
136 * @param string $propertyName The name of the property to check
137 * @return bool
138 */
139 public function __isset($propertyName)
140 {
141 $realInstance = $this->_loadRealInstance();
142 return isset($realInstance->{$propertyName});
143 }
144
145 /**
146 * Magic unset call implementation.
147 *
148 * @param string $propertyName The name of the property to unset
149 */
150 public function __unset($propertyName)
151 {
152 $realInstance = $this->_loadRealInstance();
153 unset($realInstance->{$propertyName});
154 }
155
156 /**
157 * Magic toString call implementation.
158 *
159 * @return string
160 */
161 public function __toString()
162 {
163 $realInstance = $this->_loadRealInstance();
164 return $realInstance->__toString();
165 }
166
167 /**
168 * Returns the current value of the storage array
169 *
170 * @return mixed
171 */
172 public function current()
173 {
174 $realInstance = $this->_loadRealInstance();
175 return current($realInstance);
176 }
177
178 /**
179 * Returns the current key storage array
180 *
181 * @return int
182 */
183 public function key()
184 {
185 $realInstance = $this->_loadRealInstance();
186 return key($realInstance);
187 }
188
189 /**
190 * Returns the next position of the storage array
191 */
192 public function next()
193 {
194 $realInstance = $this->_loadRealInstance();
195 next($realInstance);
196 }
197
198 /**
199 * Resets the array pointer of the storage
200 */
201 public function rewind()
202 {
203 $realInstance = $this->_loadRealInstance();
204 reset($realInstance);
205 }
206
207 /**
208 * Checks if the array pointer of the storage points to a valid position
209 *
210 * @return bool
211 */
212 public function valid()
213 {
214 return $this->current() !== false;
215 }
216 }