‪TYPO3CMS  9.5
LazyObjectStorage.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 
20 
27 {
36  private ‪$warning = 'You should never see this warning. If you do, you probably used PHP array functions like current() on the TYPO3\\CMS\\Extbase\\Persistence\\Generic\\LazyObjectStorage. To retrieve the first result, you can use the rewind() and current() methods.';
37 
41  protected ‪$dataMapper;
42 
48  protected ‪$parentObject;
49 
55  protected ‪$propertyName;
56 
62  protected ‪$fieldValue;
63 
67  protected ‪$isInitialized = false;
68 
72  protected ‪$objectManager;
73 
78  {
79  $this->objectManager = ‪$objectManager;
80  }
81 
87  public function ‪isInitialized()
88  {
90  }
91 
100  public function ‪__construct(‪$parentObject, ‪$propertyName, ‪$fieldValue, ?DataMapper ‪$dataMapper = null)
101  {
102  $this->parentObject = ‪$parentObject;
103  $this->propertyName = ‪$propertyName;
104  $this->fieldValue = ‪$fieldValue;
105  reset($this->storage);
106  $this->dataMapper = ‪$dataMapper;
107  }
108 
112  public function ‪initializeObject()
113  {
114  if (!$this->dataMapper) {
115  $this->dataMapper = $this->objectManager->get(DataMapper::class);
116  }
117  }
118 
122  protected function ‪initialize()
123  {
124  if (!$this->‪isInitialized) {
125  $this->‪isInitialized = true;
126  $objects = $this->dataMapper->fetchRelated($this->parentObject, $this->propertyName, $this->fieldValue, false);
127  foreach ($objects as $object) {
128  parent::attach($object);
129  }
130  $this->‪_memorizeCleanState();
132  $this->parentObject->_memorizeCleanState($this->propertyName);
133  }
134  }
135  }
136 
141  {
142  return $this->parentObject->_getCleanProperty($this->propertyName) === $this;
143  }
144 
145  // Delegation to the ObjectStorage methods below
151  public function ‪addAll(\‪TYPO3\CMS\‪Extbase\Persistence\‪ObjectStorage ‪$storage)
152  {
153  $this->‪initialize();
154  parent::addAll(‪$storage);
155  }
156 
163  public function ‪attach($object, $data = null)
164  {
165  $this->‪initialize();
166  parent::attach($object, $data);
167  }
168 
175  public function ‪contains($object)
176  {
177  $this->‪initialize();
178  return parent::contains($object);
179  }
180 
187  public function ‪count()
188  {
189  $columnMap = $this->dataMapper->getDataMap(get_class($this->parentObject))->getColumnMap($this->propertyName);
190  $numberOfElements = null;
191  if (!$this->‪isInitialized && $columnMap->getTypeOfRelation() === Mapper\‪ColumnMap::RELATION_HAS_MANY) {
192  $numberOfElements = $this->dataMapper->countRelated($this->parentObject, $this->propertyName, $this->fieldValue);
193  } else {
194  $this->‪initialize();
195  $numberOfElements = ‪count($this->storage);
196  }
197  if ($numberOfElements === null) {
198  throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception('The number of elements could not be determined.', 1252514486);
199  }
200  return $numberOfElements;
201  }
202 
208  public function ‪current()
209  {
210  $this->‪initialize();
211  return parent::current();
212  }
213 
219  public function ‪detach($object)
220  {
221  $this->‪initialize();
222  parent::detach($object);
223  }
224 
230  public function ‪key()
231  {
232  $this->‪initialize();
233  return parent::key();
234  }
235 
239  public function ‪next()
240  {
241  $this->‪initialize();
242  parent::next();
243  }
244 
251  public function ‪offsetExists($value)
252  {
253  $this->‪initialize();
254  return parent::offsetExists($value);
255  }
256 
263  public function ‪offsetGet($value)
264  {
265  $this->‪initialize();
266  return parent::offsetGet($value);
267  }
268 
275  public function ‪offsetSet($object, $info)
276  {
277  $this->‪initialize();
278  parent::offsetSet($object, $info);
279  }
280 
286  public function ‪offsetUnset($value)
287  {
288  $this->‪initialize();
289  parent::offsetUnset($value);
290  }
291 
297  public function ‪removeAll(\‪TYPO3\CMS\‪Extbase\Persistence\‪ObjectStorage ‪$storage)
298  {
299  $this->‪initialize();
300  parent::removeAll(‪$storage);
301  }
302 
306  public function ‪rewind()
307  {
308  $this->‪initialize();
309  parent::rewind();
310  }
311 
317  public function ‪valid()
318  {
319  $this->‪initialize();
320  return parent::valid();
321  }
322 
328  public function ‪toArray()
329  {
330  $this->‪initialize();
331  return parent::toArray();
332  }
333 
338  public function ‪getPosition($object)
339  {
340  $this->‪initialize();
341  return parent::getPosition($object);
342  }
343 }
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\injectObjectManager
‪injectObjectManager(ObjectManagerInterface $objectManager)
Definition: LazyObjectStorage.php:70
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\offsetUnset
‪offsetUnset($value)
Definition: LazyObjectStorage.php:279
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\offsetGet
‪mixed offsetGet($value)
Definition: LazyObjectStorage.php:256
‪TYPO3\CMS\Extbase\Persistence\Generic\LoadingStrategyInterface
Definition: LoadingStrategyInterface.php:21
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\$dataMapper
‪DataMapper $dataMapper
Definition: LazyObjectStorage.php:39
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\$fieldValue
‪mixed $fieldValue
Definition: LazyObjectStorage.php:57
‪TYPO3
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\rewind
‪rewind()
Definition: LazyObjectStorage.php:299
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\$isInitialized
‪bool $isInitialized
Definition: LazyObjectStorage.php:61
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\$parentObject
‪TYPO3 CMS Extbase DomainObject DomainObjectInterface $parentObject
Definition: LazyObjectStorage.php:45
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
Definition: DataMapper.php:32
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap\RELATION_HAS_MANY
‪const RELATION_HAS_MANY
Definition: ColumnMap.php:28
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\$objectManager
‪ObjectManagerInterface $objectManager
Definition: LazyObjectStorage.php:65
‪TYPO3\CMS\Extbase\Object\ObjectManagerInterface
Definition: ObjectManagerInterface.php:23
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage
Definition: ObjectStorage.php:26
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\current
‪object current()
Definition: LazyObjectStorage.php:201
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\count
‪int count()
Definition: LazyObjectStorage.php:180
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\toArray
‪array toArray()
Definition: LazyObjectStorage.php:321
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\getPosition
‪int null getPosition($object)
Definition: LazyObjectStorage.php:331
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\initializeObject
‪initializeObject()
Definition: LazyObjectStorage.php:105
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\$storage
‪array $storage
Definition: ObjectStorage.php:49
‪TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
Definition: DomainObjectInterface.php:26
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\attach
‪attach($object, $data=null)
Definition: LazyObjectStorage.php:156
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\_memorizeCleanState
‪_memorizeCleanState()
Definition: ObjectStorage.php:334
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\offsetSet
‪offsetSet($object, $info)
Definition: LazyObjectStorage.php:268
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\next
‪next()
Definition: LazyObjectStorage.php:232
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\removeAll
‪removeAll(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $storage)
Definition: LazyObjectStorage.php:290
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\offsetExists
‪bool offsetExists($value)
Definition: LazyObjectStorage.php:244
‪TYPO3\CMS\Extbase\Persistence\Generic
Definition: Backend.php:2
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\isInitialized
‪bool isInitialized()
Definition: LazyObjectStorage.php:80
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\$warning
‪string $warning
Definition: LazyObjectStorage.php:35
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\initialize
‪initialize()
Definition: LazyObjectStorage.php:115
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\valid
‪bool valid()
Definition: LazyObjectStorage.php:310
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\isStorageAlreadyMemorizedInParentCleanState
‪bool isStorageAlreadyMemorizedInParentCleanState()
Definition: LazyObjectStorage.php:133
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\__construct
‪__construct($parentObject, $propertyName, $fieldValue, ?DataMapper $dataMapper=null)
Definition: LazyObjectStorage.php:93
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\contains
‪bool contains($object)
Definition: LazyObjectStorage.php:168
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\addAll
‪addAll(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $storage)
Definition: LazyObjectStorage.php:144
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\key
‪string key()
Definition: LazyObjectStorage.php:223
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage
Definition: LazyObjectStorage.php:27
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\$propertyName
‪string $propertyName
Definition: LazyObjectStorage.php:51
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\detach
‪detach($object)
Definition: LazyObjectStorage.php:212