[+BUGFIX] Extbase (Persistence): The LazyLoadingProxy implements the Iterator interfa...
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / LazyLoadingProxy.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
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 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * A proxy that can replace any object and replaces itself in it's parent on
30 * first access (call, get, set, isset, unset).
31 *
32 * @package Extbase
33 * @subpackage Persistence
34 * @version $Id: LazyLoadingProxy.php 2591 2009-06-09 19:23:47Z k-fish $
35 */
36 class Tx_Extbase_Persistence_LazyLoadingProxy implements Iterator, Tx_Extbase_Persistence_LoadingStrategyInterface {
37
38 /**
39 * @var Tx_Extbase_Persistence_QueryFactoryInterface
40 */
41 protected $queryFactory;
42
43 /**
44 * The object this property is contained in.
45 *
46 * @var object
47 */
48 private $parentObject;
49
50 /**
51 * The name of the property represented by this proxy.
52 *
53 * @var string
54 */
55 private $propertyName;
56
57 /**
58 * The raw field value.
59 *
60 * @var mixed
61 */
62 private $fieldValue;
63
64 /**
65 *
66 * @var Tx_Extbase_Persistence_Mapper_ColumnMap
67 */
68 private $columnMap;
69
70 /**
71 * Constructs this proxy instance.
72 *
73 * @param object $parentObject The object instance this proxy is part of
74 * @param string $propertyName The name of the proxied property in it's parent
75 * @param mixed $fieldValue The raw field value.
76 * @param Tx_Extbase_Persistence_Mapper_DataMap $dataMap The corresponding Data Map of the property
77 */
78 public function __construct($parentObject, $propertyName, $fieldValue, Tx_Extbase_Persistence_Mapper_ColumnMap $columnMap) {
79 $this->queryFactory = t3lib_div::makeInstance('Tx_Extbase_Persistence_QueryFactory');
80 $this->parentObject = $parentObject;
81 $this->propertyName = $propertyName;
82 $this->fieldValue = $fieldValue;
83 $this->columnMap = $columnMap;
84 }
85
86 /**
87 * Populate this proxy by asking the $population closure.
88 *
89 * @return object The instance (hopefully) returned
90 */
91 public function _loadRealInstance() {
92 // this check safeguards against a proxy being activated multiple times
93 // usually that does not happen, but if the proxy is held from outside
94 // it's parent... the result would be weird.
95 if ($this->parentObject->_getProperty($this->propertyName) instanceof Tx_Extbase_Persistence_LazyLoadingProxy) {
96 $dataMapper = Tx_Extbase_Dispatcher::getPersistenceManager()->getBackend()->getDataMapper();
97 $objects = $dataMapper->fetchRelatedObjects($this->parentObject, $this->propertyName, $this->fieldValue, $this->columnMap);
98 $realInstance = new Tx_Extbase_Persistence_ObjectStorage();
99 foreach ($objects as $object) {
100 $realInstance->attach($object);
101 }
102 $this->parentObject->_setProperty($this->propertyName, $realInstance);
103 $this->parentObject->_memorizeCleanState($this->propertyName);
104 return $realInstance;
105 } else {
106 return $this->parentObject->_getProperty($this->propertyName);
107 }
108 }
109
110 /**
111 * Magic method call implementation.
112 *
113 * @param string $methodName The name of the property to get
114 * @param array $arguments The arguments given to the call
115 * @return mixed
116 */
117 public function __call($methodName, $arguments) {
118 $realInstance = $this->_loadRealInstance();
119 return call_user_func_array(array($realInstance, $methodName), $arguments);
120 }
121
122 /**
123 * Magic get call implementation.
124 *
125 * @param string $propertyName The name of the property to get
126 * @return mixed
127 */
128 public function __get($propertyName) {
129 $realInstance = $this->_loadRealInstance();
130 return $realInstance->$propertyName;
131 }
132
133 /**
134 * Magic set call implementation.
135 *
136 * @param string $propertyName The name of the property to set
137 * @param mixed $value The value for the property to set
138 * @return void
139 */
140 public function __set($propertyName, $value) {
141 $realInstance = $this->_loadRealInstance();
142 $realInstance->$propertyName = $value;
143 }
144
145 /**
146 * Magic isset call implementation.
147 *
148 * @param string $propertyName The name of the property to check
149 * @return boolean
150 */
151 public function __isset($propertyName) {
152 $realInstance = $this->_loadRealInstance();
153 return isset($realInstance->$propertyName);
154 }
155
156 /**
157 * Magic unset call implementation.
158 *
159 * @param string $propertyName The name of the property to unset
160 * @return void
161 */
162 public function __unset($propertyName) {
163 $realInstance = $this->_loadRealInstance();
164 unset($realInstance->$propertyName);
165 }
166
167 /**
168 * Returns the current value of the storage array
169 *
170 * @return void
171 */
172 public function current() {
173 $realInstance = $this->_loadRealInstance();
174 return current($realInstance);
175 }
176
177 /**
178 * Returns the current key storage array
179 *
180 * @return void
181 */
182 public function key() {
183 $realInstance = $this->_loadRealInstance();
184 return key($realInstance);
185 }
186
187 /**
188 * Returns the next position of the storage array
189 *
190 * @return void
191 */
192 public function next() {
193 $realInstance = $this->_loadRealInstance();
194 next($realInstance);
195 }
196
197 /**
198 * Resets the array pointer of the storage
199 *
200 * @return void
201 */
202 public function rewind() {
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 void
211 */
212 public function valid() {
213 return $this->current() !== FALSE;
214 }
215
216
217
218 }
219 ?>