TYPO3 CMS  TYPO3_7-6
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 
104  protected function initialize()
105  {
106  if (!$this->isInitialized) {
107  $this->isInitialized = true;
108  $objects = $this->dataMapper->fetchRelated($this->parentObject, $this->propertyName, $this->fieldValue, false);
109  foreach ($objects as $object) {
110  parent::attach($object);
111  }
112  $this->_memorizeCleanState();
114  $this->parentObject->_memorizeCleanState($this->propertyName);
115  }
116  }
117  }
118 
123  {
124  return $this->parentObject->_getCleanProperty($this->propertyName) === $this;
125  }
126 
127  // Delegation to the ObjectStorage methods below
133  public function addAll(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $storage)
134  {
135  $this->initialize();
136  parent::addAll($storage);
137  }
138 
145  public function attach($object, $data = null)
146  {
147  $this->initialize();
148  parent::attach($object, $data);
149  }
150 
157  public function contains($object)
158  {
159  $this->initialize();
160  return parent::contains($object);
161  }
162 
169  public function count()
170  {
171  $columnMap = $this->dataMapper->getDataMap(get_class($this->parentObject))->getColumnMap($this->propertyName);
172  $numberOfElements = null;
173  if (!$this->isInitialized && $columnMap->getTypeOfRelation() === Mapper\ColumnMap::RELATION_HAS_MANY) {
174  $numberOfElements = $this->dataMapper->countRelated($this->parentObject, $this->propertyName, $this->fieldValue);
175  } else {
176  $this->initialize();
177  $numberOfElements = count($this->storage);
178  }
179  if (is_null($numberOfElements)) {
180  throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception('The number of elements could not be determined.', 1252514486);
181  }
182  return $numberOfElements;
183  }
184 
190  public function current()
191  {
192  $this->initialize();
193  return parent::current();
194  }
195 
201  public function detach($object)
202  {
203  $this->initialize();
204  parent::detach($object);
205  }
206 
212  public function key()
213  {
214  $this->initialize();
215  return parent::key();
216  }
217 
221  public function next()
222  {
223  $this->initialize();
224  parent::next();
225  }
226 
233  public function offsetExists($object)
234  {
235  $this->initialize();
236  return parent::offsetExists($object);
237  }
238 
245  public function offsetGet($object)
246  {
247  $this->initialize();
248  return parent::offsetGet($object);
249  }
250 
257  public function offsetSet($object, $info)
258  {
259  $this->initialize();
260  parent::offsetSet($object, $info);
261  }
262 
269  public function offsetUnset($object)
270  {
271  $this->initialize();
272  parent::offsetUnset($object);
273  }
274 
281  public function removeAll(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $storage)
282  {
283  $this->initialize();
284  parent::removeAll($storage);
285  }
286 
290  public function rewind()
291  {
292  $this->initialize();
293  parent::rewind();
294  }
295 
301  public function valid()
302  {
303  $this->initialize();
304  return parent::valid();
305  }
306 
312  public function toArray()
313  {
314  $this->initialize();
315  return parent::toArray();
316  }
317 
322  public function getPosition($object)
323  {
324  $this->initialize();
325  return parent::getPosition($object);
326  }
327 }
__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)