TYPO3 CMS  TYPO3_7-6
LazyLoadingProxy.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
18 
24 {
28  protected $dataMapper;
29 
35  private $parentObject;
36 
42  private $propertyName;
43 
49  private $fieldValue;
50 
54  public function injectDataMapper(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper $dataMapper)
55  {
56  $this->dataMapper = $dataMapper;
57  }
58 
67  {
68  $this->parentObject = $parentObject;
69  $this->propertyName = $propertyName;
70  $this->fieldValue = $fieldValue;
71  }
72 
78  public function _loadRealInstance()
79  {
80  // this check safeguards against a proxy being activated multiple times
81  // usually that does not happen, but if the proxy is held from outside
82  // its parent ... the result would be weird.
83  if ($this->parentObject->_getProperty($this->propertyName) instanceof \TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy) {
84  $objects = $this->dataMapper->fetchRelated($this->parentObject, $this->propertyName, $this->fieldValue, false, false);
85  $propertyValue = $this->dataMapper->mapResultToPropertyValue($this->parentObject, $this->propertyName, $objects);
86  $this->parentObject->_setProperty($this->propertyName, $propertyValue);
87  $this->parentObject->_memorizeCleanState($this->propertyName);
88  return $propertyValue;
89  } else {
90  return $this->parentObject->_getProperty($this->propertyName);
91  }
92  }
93 
101  public function __call($methodName, $arguments)
102  {
103  $realInstance = $this->_loadRealInstance();
104  if (!is_object($realInstance)) {
105  return null;
106  }
107  return call_user_func_array([$realInstance, $methodName], $arguments);
108  }
109 
116  public function __get($propertyName)
117  {
118  $realInstance = $this->_loadRealInstance();
119  return $realInstance->{$propertyName};
120  }
121 
129  public function __set($propertyName, $value)
130  {
131  $realInstance = $this->_loadRealInstance();
132  $realInstance->{$propertyName} = $value;
133  }
134 
141  public function __isset($propertyName)
142  {
143  $realInstance = $this->_loadRealInstance();
144  return isset($realInstance->{$propertyName});
145  }
146 
153  public function __unset($propertyName)
154  {
155  $realInstance = $this->_loadRealInstance();
156  unset($realInstance->{$propertyName});
157  }
158 
164  public function __toString()
165  {
166  $realInstance = $this->_loadRealInstance();
167  return $realInstance->__toString();
168  }
169 
175  public function current()
176  {
177  $realInstance = $this->_loadRealInstance();
178  return current($realInstance);
179  }
180 
186  public function key()
187  {
188  $realInstance = $this->_loadRealInstance();
189  return key($realInstance);
190  }
191 
197  public function next()
198  {
199  $realInstance = $this->_loadRealInstance();
200  next($realInstance);
201  }
202 
208  public function rewind()
209  {
210  $realInstance = $this->_loadRealInstance();
211  reset($realInstance);
212  }
213 
219  public function valid()
220  {
221  return $this->current() !== false;
222  }
223 }
injectDataMapper(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper $dataMapper)
__construct($parentObject, $propertyName, $fieldValue)