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