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
36 class Tx_Extbase_Persistence_LazyLoadingProxy
implements Iterator
, Tx_Extbase_Persistence_LoadingStrategyInterface
{
39 * The object this property is contained in.
43 private $parentObject;
46 * The name of the property represented by this proxy.
50 private $propertyName;
53 * The raw field value.
60 * Constructs this proxy instance.
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.
66 public function __construct($parentObject, $propertyName, $fieldValue) {
67 $this->parentObject
= $parentObject;
68 $this->propertyName
= $propertyName;
69 $this->fieldValue
= $fieldValue;
73 * Populate this proxy by asking the $population closure.
75 * @return object The instance (hopefully) returned
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;
89 return $this->parentObject
->_getProperty($this->propertyName
);
94 * Magic method call implementation.
96 * @param string $methodName The name of the property to get
97 * @param array $arguments The arguments given to the call
100 public function __call($methodName, $arguments) {
101 $realInstance = $this->_loadRealInstance();
102 return call_user_func_array(array($realInstance, $methodName), $arguments);
106 * Magic get call implementation.
108 * @param string $propertyName The name of the property to get
111 public function __get($propertyName) {
112 $realInstance = $this->_loadRealInstance();
113 return $realInstance->$propertyName;
117 * Magic set call implementation.
119 * @param string $propertyName The name of the property to set
120 * @param mixed $value The value for the property to set
123 public function __set($propertyName, $value) {
124 $realInstance = $this->_loadRealInstance();
125 $realInstance->$propertyName = $value;
129 * Magic isset call implementation.
131 * @param string $propertyName The name of the property to check
134 public function __isset($propertyName) {
135 $realInstance = $this->_loadRealInstance();
136 return isset($realInstance->$propertyName);
140 * Magic unset call implementation.
142 * @param string $propertyName The name of the property to unset
145 public function __unset($propertyName) {
146 $realInstance = $this->_loadRealInstance();
147 unset($realInstance->$propertyName);
151 * Returns the current value of the storage array
155 public function current() {
156 $realInstance = $this->_loadRealInstance();
157 return current($realInstance);
161 * Returns the current key storage array
165 public function key() {
166 $realInstance = $this->_loadRealInstance();
167 return key($realInstance);
171 * Returns the next position of the storage array
175 public function next() {
176 $realInstance = $this->_loadRealInstance();
181 * Resets the array pointer of the storage
185 public function rewind() {
186 $realInstance = $this->_loadRealInstance();
187 reset($realInstance);
191 * Checks if the array pointer of the storage points to a valid position
195 public function valid() {
196 return $this->current() !== FALSE;