7a496d667ec857042d040d30fa60944d4844bc0a
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / Generic / LazyLoadingProxy.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Persistence\Generic;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2013 Extbase Team (http://forge.typo3.org/projects/typo3v4-mvc)
8 * Extbase is a backport of TYPO3 Flow. All credits go to the TYPO3 Flow team.
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the textfile GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30 /**
31 * A proxy that can replace any object and replaces itself in it's parent on
32 * first access (call, get, set, isset, unset).
33 */
34 class LazyLoadingProxy implements \Iterator, \TYPO3\CMS\Extbase\Persistence\Generic\LoadingStrategyInterface {
35
36 /**
37 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
38 */
39 protected $dataMapper;
40
41 /**
42 * The object this property is contained in.
43 *
44 * @var \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
45 */
46 private $parentObject;
47
48 /**
49 * The name of the property represented by this proxy.
50 *
51 * @var string
52 */
53 private $propertyName;
54
55 /**
56 * The raw field value.
57 *
58 * @var mixed
59 */
60 private $fieldValue;
61
62 /**
63 * Constructs this proxy instance.
64 *
65 * @param object $parentObject The object instance this proxy is part of
66 * @param string $propertyName The name of the proxied property in it's parent
67 * @param mixed $fieldValue The raw field value.
68 */
69 public function __construct($parentObject, $propertyName, $fieldValue) {
70 $this->parentObject = $parentObject;
71 $this->propertyName = $propertyName;
72 $this->fieldValue = $fieldValue;
73 }
74
75 /**
76 * Injects the DataMapper to map nodes to objects
77 *
78 * @param \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper $dataMapper
79 * @return void
80 */
81 public function injectDataMapper(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper $dataMapper) {
82 $this->dataMapper = $dataMapper;
83 }
84
85 /**
86 * Populate this proxy by asking the $population closure.
87 *
88 * @return object The instance (hopefully) returned
89 */
90 public function _loadRealInstance() {
91 // this check safeguards against a proxy being activated multiple times
92 // usually that does not happen, but if the proxy is held from outside
93 // it's parent... the result would be weird.
94 if ($this->parentObject->_getProperty($this->propertyName) instanceof \TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy) {
95 $objects = $this->dataMapper->fetchRelated($this->parentObject, $this->propertyName, $this->fieldValue, FALSE, FALSE);
96 $propertyValue = $this->dataMapper->mapResultToPropertyValue($this->parentObject, $this->propertyName, $objects);
97 $this->parentObject->_setProperty($this->propertyName, $propertyValue);
98 $this->parentObject->_memorizeCleanState($this->propertyName);
99 return $propertyValue;
100 } else {
101 return $this->parentObject->_getProperty($this->propertyName);
102 }
103 }
104
105 /**
106 * Magic method call implementation.
107 *
108 * @param string $methodName The name of the property to get
109 * @param array $arguments The arguments given to the call
110 * @return mixed
111 */
112 public function __call($methodName, $arguments) {
113 $realInstance = $this->_loadRealInstance();
114 if (!is_object($realInstance)) {
115 return NULL;
116 }
117 return call_user_func_array(array($realInstance, $methodName), $arguments);
118 }
119
120 /**
121 * Magic get call implementation.
122 *
123 * @param string $propertyName The name of the property to get
124 * @return mixed
125 */
126 public function __get($propertyName) {
127 $realInstance = $this->_loadRealInstance();
128 return $realInstance->{$propertyName};
129 }
130
131 /**
132 * Magic set call implementation.
133 *
134 * @param string $propertyName The name of the property to set
135 * @param mixed $value The value for the property to set
136 * @return void
137 */
138 public function __set($propertyName, $value) {
139 $realInstance = $this->_loadRealInstance();
140 $realInstance->{$propertyName} = $value;
141 }
142
143 /**
144 * Magic isset call implementation.
145 *
146 * @param string $propertyName The name of the property to check
147 * @return boolean
148 */
149 public function __isset($propertyName) {
150 $realInstance = $this->_loadRealInstance();
151 return isset($realInstance->{$propertyName});
152 }
153
154 /**
155 * Magic unset call implementation.
156 *
157 * @param string $propertyName The name of the property to unset
158 * @return void
159 */
160 public function __unset($propertyName) {
161 $realInstance = $this->_loadRealInstance();
162 unset($realInstance->{$propertyName});
163 }
164
165 /**
166 * Magic toString call implementation.
167 *
168 * @return string
169 */
170 public function __toString() {
171 $realInstance = $this->_loadRealInstance();
172 return $realInstance->__toString();
173 }
174
175 /**
176 * Returns the current value of the storage array
177 *
178 * @return mixed
179 */
180 public function current() {
181 $realInstance = $this->_loadRealInstance();
182 return current($realInstance);
183 }
184
185 /**
186 * Returns the current key storage array
187 *
188 * @return integer
189 */
190 public function key() {
191 $realInstance = $this->_loadRealInstance();
192 return key($realInstance);
193 }
194
195 /**
196 * Returns the next position of the storage array
197 *
198 * @return void
199 */
200 public function next() {
201 $realInstance = $this->_loadRealInstance();
202 next($realInstance);
203 }
204
205 /**
206 * Resets the array pointer of the storage
207 *
208 * @return void
209 */
210 public function rewind() {
211 $realInstance = $this->_loadRealInstance();
212 reset($realInstance);
213 }
214
215 /**
216 * Checks if the array pointer of the storage points to a valid position
217 *
218 * @return boolean
219 */
220 public function valid() {
221 return $this->current() !== FALSE;
222 }
223 }
224
225 ?>