‪TYPO3CMS  ‪main
LazyLoadingProxy.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
23 
30 {
31  protected ?‪DataMapper ‪$dataMapper = null;
32 
38  private ‪$parentObject;
39 
45  private ‪$propertyName;
46 
52  private ‪$fieldValue;
53 
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 ‪DomainObjectInterface
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 
104  public function ‪getUid(): int
105  {
106  return (int)‪$this->fieldValue;
107  }
108 
116  public function ‪__call($methodName, $arguments)
117  {
118  $realInstance = $this->‪_loadRealInstance();
119  if (!is_object($realInstance)) {
120  return null;
121  }
123  $callable = [$realInstance, $methodName];
124  return $callable(...$arguments);
125  }
126 
133  public function ‪__get(‪$propertyName)
134  {
135  $realInstance = $this->‪_loadRealInstance();
136 
137  if ($realInstance instanceof DomainObjectInterface) {
138  return $realInstance->_getProperty(‪$propertyName);
139  }
140  return $realInstance?->{‪$propertyName};
141  }
142 
149  public function __set($propertyName, $value)
150  {
151  $realInstance = $this->_loadRealInstance();
152  $realInstance->{$propertyName} = $value;
153  }
154 
161  public function __isset($propertyName)
162  {
163  $realInstance = $this->_loadRealInstance();
164  return isset($realInstance->{$propertyName});
165  }
166 
172  public function __unset($propertyName)
173  {
174  $realInstance = $this->_loadRealInstance();
175  unset($realInstance->{$propertyName});
176  }
177 
183  public function __toString()
184  {
185  $realInstance = $this->_loadRealInstance();
186  return $realInstance->__toString();
187  }
188 
192  public function current(): mixed
193  {
194  // todo: make sure current() can be performed on $realInstance
195  $realInstance = $this->_loadRealInstance();
196  return current($realInstance);
197  }
198 
203  public function key(): mixed
204  {
205  // todo: make sure key() can be performed on $realInstance
206  $realInstance = $this->_loadRealInstance();
207  return key($realInstance);
208  }
209 
213  public function next(): void
214  {
215  // todo: make sure next() can be performed on $realInstance
216  $realInstance = $this->_loadRealInstance();
217  next($realInstance);
218  }
219 
223  public function rewind(): void
224  {
225  // todo: make sure reset() can be performed on $realInstance
226  $realInstance = $this->_loadRealInstance();
227  reset($realInstance);
228  }
229 
233  public function valid(): bool
234  {
235  return $this->current() !== false;
236  }
237 
238  public function __serialize(): array
239  {
240  $properties = get_object_vars($this);
241  unset($properties['dataMapper']);
242  return $properties;
243  }
244 
245  public function __unserialize(array $data): void
246  {
247  foreach ($data as $propertyName => $propertyValue) {
248  $this->{$propertyName} = $propertyValue;
249  }
250 
251  $this->dataMapper = GeneralUtility::getContainer()->get(DataMapper::class);
252  }
253 }
‪TYPO3\CMS\Extbase\Persistence\Generic\LoadingStrategyInterface
Definition: LoadingStrategyInterface.php:21
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\next
‪next()
Definition: LazyLoadingProxy.php:210
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\current
‪current()
Definition: LazyLoadingProxy.php:189
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\__get
‪mixed __get($propertyName)
Definition: LazyLoadingProxy.php:130
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\key
‪int string null key()
Definition: LazyLoadingProxy.php:200
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\__call
‪mixed __call($methodName, $arguments)
Definition: LazyLoadingProxy.php:113
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\__toString
‪string __toString()
Definition: LazyLoadingProxy.php:180
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\$parentObject
‪DomainObjectInterface $parentObject
Definition: LazyLoadingProxy.php:37
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
Definition: DataMapper.php:60
‪TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface\_getProperty
‪_getProperty(string $propertyName)
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\__isset
‪bool __isset($propertyName)
Definition: LazyLoadingProxy.php:158
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\_loadRealInstance
‪object null _loadRealInstance()
Definition: LazyLoadingProxy.php:74
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\__serialize
‪__serialize()
Definition: LazyLoadingProxy.php:235
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\getUid
‪getUid()
Definition: LazyLoadingProxy.php:101
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\valid
‪valid()
Definition: LazyLoadingProxy.php:230
‪TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
Definition: DomainObjectInterface.php:33
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\$propertyName
‪string $propertyName
Definition: LazyLoadingProxy.php:43
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\_getTypeAndUidString
‪string _getTypeAndUidString()
Definition: LazyLoadingProxy.php:95
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\rewind
‪rewind()
Definition: LazyLoadingProxy.php:220
‪TYPO3\CMS\Extbase\Persistence\Generic
Definition: Backend.php:18
‪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:30
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\__unset
‪__unset($propertyName)
Definition: LazyLoadingProxy.php:169
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\$dataMapper
‪DataMapper $dataMapper
Definition: LazyLoadingProxy.php:31
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\__unserialize
‪__unserialize(array $data)
Definition: LazyLoadingProxy.php:242
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\$fieldValue
‪mixed $fieldValue
Definition: LazyLoadingProxy.php:49
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy\__set
‪__set($propertyName, $value)
Definition: LazyLoadingProxy.php:146