‪TYPO3CMS  10.4
LazyObjectStorage.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 
23 
30 {
39  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.';
40 
44  protected ‪$dataMapper;
45 
51  protected ‪$parentObject;
52 
58  protected ‪$propertyName;
59 
65  protected ‪$fieldValue;
66 
70  protected ‪$isInitialized = false;
71 
75  protected ‪$objectManager;
76 
81  {
82  $this->objectManager = ‪$objectManager;
83  }
84 
90  public function ‪isInitialized()
91  {
93  }
94 
103  public function ‪__construct(‪$parentObject, ‪$propertyName, ‪$fieldValue, ?DataMapper ‪$dataMapper = null)
104  {
105  $this->parentObject = ‪$parentObject;
106  $this->propertyName = ‪$propertyName;
107  $this->fieldValue = ‪$fieldValue;
108  reset($this->storage);
109  $this->dataMapper = ‪$dataMapper;
110  }
111 
115  public function ‪initializeObject()
116  {
117  if (!$this->dataMapper) {
118  $this->dataMapper = $this->objectManager->get(DataMapper::class);
119  }
120  }
121 
125  protected function ‪initialize()
126  {
127  if (!$this->‪isInitialized) {
128  $this->‪isInitialized = true;
129  $objects = $this->dataMapper->fetchRelated($this->parentObject, $this->propertyName, $this->fieldValue, false);
130  foreach ($objects as $object) {
131  parent::attach($object);
132  }
133  $this->‪_memorizeCleanState();
135  $this->parentObject->_memorizeCleanState($this->propertyName);
136  }
137  }
138  }
139 
144  {
145  return $this->parentObject->_getCleanProperty($this->propertyName) === $this;
146  }
147 
148  // Delegation to the ObjectStorage methods below
154  public function ‪addAll(‪ObjectStorage ‪$storage)
155  {
156  $this->‪initialize();
157  parent::addAll(‪$storage);
158  }
159 
166  public function ‪attach($object, $data = null)
167  {
168  $this->‪initialize();
169  parent::attach($object, $data);
170  }
171 
178  public function ‪contains($object)
179  {
180  $this->‪initialize();
181  return parent::contains($object);
182  }
183 
190  public function ‪count()
191  {
192  $columnMap = $this->dataMapper->getDataMap(get_class($this->parentObject))->getColumnMap($this->propertyName);
193  $numberOfElements = null;
194  if (!$this->‪isInitialized && $columnMap->getTypeOfRelation() === ‪ColumnMap::RELATION_HAS_MANY) {
195  $numberOfElements = $this->dataMapper->countRelated($this->parentObject, $this->propertyName, $this->fieldValue);
196  } else {
197  $this->‪initialize();
198  $numberOfElements = ‪count($this->storage);
199  }
200  if ($numberOfElements === null) {
201  throw new ‪Exception('The number of elements could not be determined.', 1252514486);
202  }
203  return $numberOfElements;
204  }
205 
211  public function ‪current()
212  {
213  $this->‪initialize();
214  return parent::current();
215  }
216 
222  public function ‪detach($object)
223  {
224  $this->‪initialize();
225  parent::detach($object);
226  }
227 
233  public function ‪key()
234  {
235  $this->‪initialize();
236  return parent::key();
237  }
238 
242  public function ‪next()
243  {
244  $this->‪initialize();
245  parent::next();
246  }
247 
254  public function ‪offsetExists($value)
255  {
256  $this->‪initialize();
257  return parent::offsetExists($value);
258  }
259 
266  public function ‪offsetGet($value)
267  {
268  $this->‪initialize();
269  return parent::offsetGet($value);
270  }
271 
278  public function ‪offsetSet($object, $info)
279  {
280  $this->‪initialize();
281  parent::offsetSet($object, $info);
282  }
283 
289  public function ‪offsetUnset($value)
290  {
291  $this->‪initialize();
292  parent::offsetUnset($value);
293  }
294 
300  public function ‪removeAll(‪ObjectStorage ‪$storage)
301  {
302  $this->‪initialize();
303  parent::removeAll(‪$storage);
304  }
305 
309  public function ‪rewind()
310  {
311  $this->‪initialize();
312  parent::rewind();
313  }
314 
320  public function ‪valid()
321  {
322  $this->‪initialize();
323  return parent::valid();
324  }
325 
331  public function ‪toArray()
332  {
333  $this->‪initialize();
334  return parent::toArray();
335  }
336 
341  public function ‪getPosition($object)
342  {
343  $this->‪initialize();
344  return parent::getPosition($object);
345  }
346 }
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\injectObjectManager
‪injectObjectManager(ObjectManagerInterface $objectManager)
Definition: LazyObjectStorage.php:73
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\offsetUnset
‪offsetUnset($value)
Definition: LazyObjectStorage.php:282
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\offsetGet
‪mixed offsetGet($value)
Definition: LazyObjectStorage.php:259
‪TYPO3\CMS\Extbase\Persistence\Generic\LoadingStrategyInterface
Definition: LoadingStrategyInterface.php:22
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\$fieldValue
‪mixed $fieldValue
Definition: LazyObjectStorage.php:60
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap
Definition: ColumnMap.php:28
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\rewind
‪rewind()
Definition: LazyObjectStorage.php:302
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\$dataMapper
‪DataMapper null $dataMapper
Definition: LazyObjectStorage.php:42
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\$isInitialized
‪bool $isInitialized
Definition: LazyObjectStorage.php:64
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\$parentObject
‪TYPO3 CMS Extbase DomainObject DomainObjectInterface $parentObject
Definition: LazyObjectStorage.php:48
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
Definition: DataMapper.php:52
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap\RELATION_HAS_MANY
‪const RELATION_HAS_MANY
Definition: ColumnMap.php:42
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\$objectManager
‪ObjectManagerInterface $objectManager
Definition: LazyObjectStorage.php:68
‪TYPO3\CMS\Extbase\Object\ObjectManagerInterface
Definition: ObjectManagerInterface.php:26
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage
Definition: ObjectStorage.php:28
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\count
‪int count()
Definition: LazyObjectStorage.php:183
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\toArray
‪array toArray()
Definition: LazyObjectStorage.php:324
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\getPosition
‪int null getPosition($object)
Definition: LazyObjectStorage.php:334
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\initializeObject
‪initializeObject()
Definition: LazyObjectStorage.php:108
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\$storage
‪array $storage
Definition: ObjectStorage.php:51
‪TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
Definition: DomainObjectInterface.php:29
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\removeAll
‪removeAll(ObjectStorage $storage)
Definition: LazyObjectStorage.php:293
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\attach
‪attach($object, $data=null)
Definition: LazyObjectStorage.php:159
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\_memorizeCleanState
‪_memorizeCleanState()
Definition: ObjectStorage.php:337
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\offsetSet
‪offsetSet($object, $info)
Definition: LazyObjectStorage.php:271
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\next
‪next()
Definition: LazyObjectStorage.php:235
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\addAll
‪addAll(ObjectStorage $storage)
Definition: LazyObjectStorage.php:147
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\offsetExists
‪bool offsetExists($value)
Definition: LazyObjectStorage.php:247
‪TYPO3\CMS\Extbase\Persistence\Generic
Definition: Backend.php:16
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\isInitialized
‪bool isInitialized()
Definition: LazyObjectStorage.php:83
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\$warning
‪string $warning
Definition: LazyObjectStorage.php:38
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\initialize
‪initialize()
Definition: LazyObjectStorage.php:118
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\valid
‪bool valid()
Definition: LazyObjectStorage.php:313
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\isStorageAlreadyMemorizedInParentCleanState
‪bool isStorageAlreadyMemorizedInParentCleanState()
Definition: LazyObjectStorage.php:136
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\__construct
‪__construct($parentObject, $propertyName, $fieldValue, ?DataMapper $dataMapper=null)
Definition: LazyObjectStorage.php:96
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\contains
‪bool contains($object)
Definition: LazyObjectStorage.php:171
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\current
‪DomainObjectInterface current()
Definition: LazyObjectStorage.php:204
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\key
‪string key()
Definition: LazyObjectStorage.php:226
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage
Definition: LazyObjectStorage.php:30
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\$propertyName
‪string $propertyName
Definition: LazyObjectStorage.php:54
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\detach
‪detach($object)
Definition: LazyObjectStorage.php:215
‪TYPO3\CMS\Extbase\Persistence\Generic\Exception
Definition: Exception.php:24