Raised DBAL version from 1.1.5 to 1.1.6
[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 2262 2010-05-02 09:37:34Z jocrau $
35 */
36 class Tx_Extbase_Persistence_LazyLoadingProxy implements Iterator, Tx_Extbase_Persistence_LoadingStrategyInterface {
37
38 /**
39 * The object this property is contained in.
40 *
41 * @var object
42 */
43 private $parentObject;
44
45 /**
46 * The name of the property represented by this proxy.
47 *
48 * @var string
49 */
50 private $propertyName;
51
52 /**
53 * The raw field value.
54 *
55 * @var mixed
56 */
57 private $fieldValue;
58
59 /**
60 * Constructs this proxy instance.
61 *
62 * @param object $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 $this->parentObject = $parentObject;
68 $this->propertyName = $propertyName;
69 $this->fieldValue = $fieldValue;
70 }
71
72 /**
73 * Populate this proxy by asking the $population closure.
74 *
75 * @return object The instance (hopefully) returned
76 */
77 public function _loadRealInstance() {
78 // this check safeguards against a proxy being activated multiple times
79 // usually that does not happen, but if the proxy is held from outside
80 // it's parent... the result would be weird.
81 if ($this->parentObject->_getProperty($this->propertyName) instanceof Tx_Extbase_Persistence_LazyLoadingProxy) {
82 $dataMapper = Tx_Extbase_Dispatcher::getPersistenceManager()->getBackend()->getDataMapper();
83 $objects = $dataMapper->fetchRelated($this->parentObject, $this->propertyName, $this->fieldValue, FALSE, FALSE);
84 $propertyValue = $dataMapper->mapResultToPropertyValue($this->parentObject, $this->propertyName, $objects);
85 $this->parentObject->_setProperty($this->propertyName, $propertyValue);
86 $this->parentObject->_memorizeCleanState($this->propertyName);
87 return $propertyValue;
88 } else {
89 return $this->parentObject->_getProperty($this->propertyName);
90 }
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 $realInstance = $this->_loadRealInstance();
102 return call_user_func_array(array($realInstance, $methodName), $arguments);
103 }
104
105 /**
106 * Magic get call implementation.
107 *
108 * @param string $propertyName The name of the property to get
109 * @return mixed
110 */
111 public function __get($propertyName) {
112 $realInstance = $this->_loadRealInstance();
113 return $realInstance->$propertyName;
114 }
115
116 /**
117 * Magic set call implementation.
118 *
119 * @param string $propertyName The name of the property to set
120 * @param mixed $value The value for the property to set
121 * @return void
122 */
123 public function __set($propertyName, $value) {
124 $realInstance = $this->_loadRealInstance();
125 $realInstance->$propertyName = $value;
126 }
127
128 /**
129 * Magic isset call implementation.
130 *
131 * @param string $propertyName The name of the property to check
132 * @return boolean
133 */
134 public function __isset($propertyName) {
135 $realInstance = $this->_loadRealInstance();
136 return isset($realInstance->$propertyName);
137 }
138
139 /**
140 * Magic unset call implementation.
141 *
142 * @param string $propertyName The name of the property to unset
143 * @return void
144 */
145 public function __unset($propertyName) {
146 $realInstance = $this->_loadRealInstance();
147 unset($realInstance->$propertyName);
148 }
149
150 /**
151 * Magic toString call implementation.
152 *
153 * @return void
154 */
155 public function __toString() {
156 $realInstance = $this->_loadRealInstance();
157 return $realInstance->__toString();
158 }
159
160 /**
161 * Returns the current value of the storage array
162 *
163 * @return void
164 */
165 public function current() {
166 $realInstance = $this->_loadRealInstance();
167 return current($realInstance);
168 }
169
170 /**
171 * Returns the current key storage array
172 *
173 * @return void
174 */
175 public function key() {
176 $realInstance = $this->_loadRealInstance();
177 return key($realInstance);
178 }
179
180 /**
181 * Returns the next position of the storage array
182 *
183 * @return void
184 */
185 public function next() {
186 $realInstance = $this->_loadRealInstance();
187 next($realInstance);
188 }
189
190 /**
191 * Resets the array pointer of the storage
192 *
193 * @return void
194 */
195 public function rewind() {
196 $realInstance = $this->_loadRealInstance();
197 reset($realInstance);
198 }
199
200 /**
201 * Checks if the array pointer of the storage points to a valid position
202 *
203 * @return void
204 */
205 public function valid() {
206 return $this->current() !== FALSE;
207 }
208
209
210
211 }
212 ?>