TYPO3 CMS  TYPO3_8-7
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 
18 
24 {
33  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.';
34 
38  protected $dataMapper;
39 
45  protected $parentObject;
46 
52  protected $propertyName;
53 
59  protected $fieldValue;
60 
64  protected $isInitialized = false;
65 
69  public function injectDataMapper(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper $dataMapper)
70  {
71  $this->dataMapper = $dataMapper;
72  }
73 
79  public function isInitialized()
80  {
81  return $this->isInitialized;
82  }
83 
92  {
93  $this->parentObject = $parentObject;
94  $this->propertyName = $propertyName;
95  $this->fieldValue = $fieldValue;
96  reset($this->storage);
97  }
98 
102  protected function initialize()
103  {
104  if (!$this->isInitialized) {
105  $this->isInitialized = true;
106  $objects = $this->dataMapper->fetchRelated($this->parentObject, $this->propertyName, $this->fieldValue, false);
107  foreach ($objects as $object) {
108  parent::attach($object);
109  }
110  $this->_memorizeCleanState();
112  $this->parentObject->_memorizeCleanState($this->propertyName);
113  }
114  }
115  }
116 
121  {
122  return $this->parentObject->_getCleanProperty($this->propertyName) === $this;
123  }
124 
125  // Delegation to the ObjectStorage methods below
131  public function addAll(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $storage)
132  {
133  $this->initialize();
134  parent::addAll($storage);
135  }
136 
143  public function attach($object, $data = null)
144  {
145  $this->initialize();
146  parent::attach($object, $data);
147  }
148 
155  public function contains($object)
156  {
157  $this->initialize();
158  return parent::contains($object);
159  }
160 
167  public function count()
168  {
169  $columnMap = $this->dataMapper->getDataMap(get_class($this->parentObject))->getColumnMap($this->propertyName);
170  $numberOfElements = null;
171  if (!$this->isInitialized && $columnMap->getTypeOfRelation() === Mapper\ColumnMap::RELATION_HAS_MANY) {
172  $numberOfElements = $this->dataMapper->countRelated($this->parentObject, $this->propertyName, $this->fieldValue);
173  } else {
174  $this->initialize();
175  $numberOfElements = count($this->storage);
176  }
177  if (is_null($numberOfElements)) {
178  throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception('The number of elements could not be determined.', 1252514486);
179  }
180  return $numberOfElements;
181  }
182 
188  public function current()
189  {
190  $this->initialize();
191  return parent::current();
192  }
193 
199  public function detach($object)
200  {
201  $this->initialize();
202  parent::detach($object);
203  }
204 
210  public function key()
211  {
212  $this->initialize();
213  return parent::key();
214  }
215 
219  public function next()
220  {
221  $this->initialize();
222  parent::next();
223  }
224 
231  public function offsetExists($object)
232  {
233  $this->initialize();
234  return parent::offsetExists($object);
235  }
236 
243  public function offsetGet($object)
244  {
245  $this->initialize();
246  return parent::offsetGet($object);
247  }
248 
255  public function offsetSet($object, $info)
256  {
257  $this->initialize();
258  parent::offsetSet($object, $info);
259  }
260 
266  public function offsetUnset($object)
267  {
268  $this->initialize();
269  parent::offsetUnset($object);
270  }
271 
277  public function removeAll(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $storage)
278  {
279  $this->initialize();
280  parent::removeAll($storage);
281  }
282 
286  public function rewind()
287  {
288  $this->initialize();
289  parent::rewind();
290  }
291 
297  public function valid()
298  {
299  $this->initialize();
300  return parent::valid();
301  }
302 
308  public function toArray()
309  {
310  $this->initialize();
311  return parent::toArray();
312  }
313 
318  public function getPosition($object)
319  {
320  $this->initialize();
321  return parent::getPosition($object);
322  }
323 }
__construct($parentObject, $propertyName, $fieldValue)
removeAll(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $storage)
addAll(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $storage)
injectDataMapper(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper $dataMapper)