‪TYPO3CMS  11.5
LazyLoadingProxy.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
22 
29 {
30  protected ?‪DataMapper ‪$dataMapper = null;
31 
37  private ‪$parentObject;
38 
44  private ‪$propertyName;
45 
51  private ‪$fieldValue;
52 
62  {
63  $this->parentObject = ‪$parentObject;
64  $this->propertyName = ‪$propertyName;
65  $this->fieldValue = ‪$fieldValue;
66  if (‪$dataMapper === null) {
67  ‪$dataMapper = GeneralUtility::makeInstance(DataMapper::class);
68  }
69  $this->dataMapper = ‪$dataMapper;
70  }
71 
77  public function ‪_loadRealInstance()
78  {
79  // this check safeguards against a proxy being activated multiple times
80  // usually that does not happen, but if the proxy is held from outside
81  // its parent ... the result would be weird.
82  if ($this->parentObject instanceof ‪AbstractDomainObject
83  && $this->parentObject->‪_getProperty($this->propertyName) instanceof ‪LazyLoadingProxy
84  && $this->dataMapper
85  ) {
86  $objects = $this->dataMapper->fetchRelated($this->parentObject, $this->propertyName, $this->fieldValue, 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  }
92  return $this->parentObject->_getProperty($this->propertyName);
93  }
94 
98  public function ‪_getTypeAndUidString()
99  {
100  $type = $this->dataMapper->getType(get_class($this->parentObject), $this->propertyName);
101  return $type . ':' . ‪$this->fieldValue;
102  }
103 
107  public function ‪getUid(): int
108  {
109  return (int)‪$this->fieldValue;
110  }
111 
119  public function ‪__call($methodName, $arguments)
120  {
121  $realInstance = $this->‪_loadRealInstance();
122  if (!is_object($realInstance)) {
123  return null;
124  }
126  $callable = [$realInstance, $methodName];
127  return $callable(...$arguments);
128  }
129 
136  public function ‪__get(‪$propertyName)
137  {
138  $realInstance = $this->‪_loadRealInstance();
139 
140  if ($realInstance instanceof AbstractDomainObject) {
141  return $realInstance->_getProperty(‪$propertyName);
142  }
143  return $realInstance->{‪$propertyName};
144  }
145 
152  public function __set($propertyName, $value)
153  {
154  $realInstance = $this->_loadRealInstance();
155  $realInstance->{$propertyName} = $value;
156  }
157 
164  public function __isset($propertyName)
165  {
166  $realInstance = $this->_loadRealInstance();
167  return isset($realInstance->{$propertyName});
168  }
169 
175  public function __unset($propertyName)
176  {
177  $realInstance = $this->_loadRealInstance();
178  unset($realInstance->{$propertyName});
179  }
180 
186  public function __toString()
187  {
188  $realInstance = $this->_loadRealInstance();
189  return $realInstance->__toString();
190  }
191 
198  #[\ReturnTypeWillChange]
199  public function current()
200  {
201  // todo: make sure current() can be performed on $realInstance
202  $realInstance = $this->_loadRealInstance();
203  return current($realInstance);
204  }
205 
211  #[\ReturnTypeWillChange]
212  public function key()
213  {
214  // todo: make sure key() can be performed on $realInstance
215  $realInstance = $this->_loadRealInstance();
216  return key($realInstance);
217  }
218 
223  #[\ReturnTypeWillChange]
224  public function next()
225  {
226  // todo: make sure next() can be performed on $realInstance
227  $realInstance = $this->_loadRealInstance();
228  next($realInstance);
229  }
230 
235  #[\ReturnTypeWillChange]
236  public function rewind()
237  {
238  // todo: make sure reset() can be performed on $realInstance
239  $realInstance = $this->_loadRealInstance();
240  reset($realInstance);
241  }
242 
249  #[\ReturnTypeWillChange]
250  public function valid()
251  {
252  return $this->current() !== false;
253  }
254 
255  public function __serialize(): array
256  {
257  $properties = get_object_vars($this);
258  unset($properties['dataMapper']);
259  return $properties;
260  }
261 
262  public function __unserialize(array $data): void
263  {
264  foreach ($data as $propertyName => $propertyValue) {
265  $this->{$propertyName} = $propertyValue;
266  }
267 
268  $this->dataMapper = GeneralUtility::getContainer()->get(DataMapper::class);
269  }
270 }
‪TYPO3\CMS\Extbase\Persistence\Generic\LoadingStrategyInterface
Definition: LoadingStrategyInterface.php:21
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\next
‪next()
Definition: LazyLoadingProxy.php:221
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\key
‪int key()
Definition: LazyLoadingProxy.php:209
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\__get
‪mixed __get($propertyName)
Definition: LazyLoadingProxy.php:133
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\__call
‪mixed __call($methodName, $arguments)
Definition: LazyLoadingProxy.php:116
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\__toString
‪string __toString()
Definition: LazyLoadingProxy.php:183
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\$parentObject
‪DomainObjectInterface $parentObject
Definition: LazyLoadingProxy.php:36
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
Definition: DataMapper.php:51
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\valid
‪bool valid()
Definition: LazyLoadingProxy.php:247
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\__isset
‪bool __isset($propertyName)
Definition: LazyLoadingProxy.php:161
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\__serialize
‪__serialize()
Definition: LazyLoadingProxy.php:252
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\_loadRealInstance
‪object _loadRealInstance()
Definition: LazyLoadingProxy.php:74
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\current
‪mixed current()
Definition: LazyLoadingProxy.php:196
‪TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
Definition: DomainObjectInterface.php:29
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject
Definition: AbstractDomainObject.php:31
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\$propertyName
‪string $propertyName
Definition: LazyLoadingProxy.php:42
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\_getTypeAndUidString
‪string _getTypeAndUidString()
Definition: LazyLoadingProxy.php:95
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\rewind
‪rewind()
Definition: LazyLoadingProxy.php:233
‪TYPO3\CMS\Extbase\Persistence\Generic
Definition: Backend.php:16
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\__construct
‪__construct($parentObject, $propertyName, $fieldValue, ?DataMapper $dataMapper=null)
Definition: LazyLoadingProxy.php:58
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy
Definition: LazyLoadingProxy.php:29
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\__unset
‪__unset($propertyName)
Definition: LazyLoadingProxy.php:172
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\_getProperty
‪mixed _getProperty(string $propertyName)
Definition: AbstractDomainObject.php:122
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\getUid
‪int getUid()
Definition: LazyLoadingProxy.php:104
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\$dataMapper
‪DataMapper $dataMapper
Definition: LazyLoadingProxy.php:30
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\__unserialize
‪__unserialize(array $data)
Definition: LazyLoadingProxy.php:259
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\$fieldValue
‪mixed $fieldValue
Definition: LazyLoadingProxy.php:48
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\__set
‪__set($propertyName, $value)
Definition: LazyLoadingProxy.php:149