2 /***************************************************************
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
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.
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
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.
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
29 * A proxy that can replace any object and replaces itself in it's parent on
30 * first access (call, get, set, isset, unset).
33 * @subpackage Persistence
34 * @version $Id: LazyLoadingProxy.php 2591 2009-06-09 19:23:47Z k-fish $
36 class Tx_Extbase_Persistence_LazyLoadingProxy
implements Iterator
, Tx_Extbase_Persistence_LoadingStrategyInterface
{
39 * @var Tx_Extbase_Persistence_QueryFactoryInterface
41 protected $queryFactory;
44 * The object this property is contained in.
48 private $parentObject;
51 * The name of the property represented by this proxy.
55 private $propertyName;
58 * The raw field value.
66 * @var Tx_Extbase_Persistence_Mapper_ColumnMap
71 * Constructs this proxy instance.
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
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;
87 * Populate this proxy by asking the $population closure.
89 * @return object The instance (hopefully) returned
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);
102 $this->parentObject
->_setProperty($this->propertyName
, $realInstance);
103 $this->parentObject
->_memorizeCleanState($this->propertyName
);
104 return $realInstance;
106 return $this->parentObject
->_getProperty($this->propertyName
);
111 * Magic method call implementation.
113 * @param string $methodName The name of the property to get
114 * @param array $arguments The arguments given to the call
117 public function __call($methodName, $arguments) {
118 $realInstance = $this->_loadRealInstance();
119 return call_user_func_array(array($realInstance, $methodName), $arguments);
123 * Magic get call implementation.
125 * @param string $propertyName The name of the property to get
128 public function __get($propertyName) {
129 $realInstance = $this->_loadRealInstance();
130 return $realInstance->$propertyName;
134 * Magic set call implementation.
136 * @param string $propertyName The name of the property to set
137 * @param mixed $value The value for the property to set
140 public function __set($propertyName, $value) {
141 $realInstance = $this->_loadRealInstance();
142 $realInstance->$propertyName = $value;
146 * Magic isset call implementation.
148 * @param string $propertyName The name of the property to check
151 public function __isset($propertyName) {
152 $realInstance = $this->_loadRealInstance();
153 return isset($realInstance->$propertyName);
157 * Magic unset call implementation.
159 * @param string $propertyName The name of the property to unset
162 public function __unset($propertyName) {
163 $realInstance = $this->_loadRealInstance();
164 unset($realInstance->$propertyName);
168 * Returns the current value of the storage array
172 public function current() {
173 $realInstance = $this->_loadRealInstance();
174 return current($realInstance);
178 * Returns the current key storage array
182 public function key() {
183 $realInstance = $this->_loadRealInstance();
184 return key($realInstance);
188 * Returns the next position of the storage array
192 public function next() {
193 $realInstance = $this->_loadRealInstance();
198 * Resets the array pointer of the storage
202 public function rewind() {
203 $realInstance = $this->_loadRealInstance();
204 reset($realInstance);
208 * Checks if the array pointer of the storage points to a valid position
212 public function valid() {
213 return $this->current() !== FALSE;