TYPO3 CMS  TYPO3_7-6
PersistenceManager.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 
19 
26 {
30  protected $newObjects = [];
31 
35  protected $changedObjects;
36 
40  protected $addedObjects;
41 
45  protected $removedObjects;
46 
50  protected $queryFactory;
51 
55  protected $backend;
56 
61 
65  public function injectQueryFactory(\TYPO3\CMS\Extbase\Persistence\Generic\QueryFactoryInterface $queryFactory)
66  {
67  $this->queryFactory = $queryFactory;
68  }
69 
73  public function injectBackend(\TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface $backend)
74  {
75  $this->backend = $backend;
76  }
77 
81  public function injectPersistenceSession(\TYPO3\CMS\Extbase\Persistence\Generic\Session $persistenceSession)
82  {
83  $this->persistenceSession = $persistenceSession;
84  }
85 
89  public function __construct()
90  {
91  $this->addedObjects = new ObjectStorage();
92  $this->removedObjects = new ObjectStorage();
93  $this->changedObjects = new ObjectStorage();
94  }
95 
102  public function registerRepositoryClassName($className)
103  {
104  }
105 
113  public function getObjectCountByQuery(QueryInterface $query)
114  {
115  return $this->backend->getObjectCountByQuery($query);
116  }
117 
125  public function getObjectDataByQuery(QueryInterface $query)
126  {
127  return $this->backend->getObjectDataByQuery($query);
128  }
129 
142  public function getIdentifierByObject($object)
143  {
144  return $this->backend->getIdentifierByObject($object);
145  }
146 
157  public function getObjectByIdentifier($identifier, $objectType = null, $useLazyLoading = false)
158  {
159  if (isset($this->newObjects[$identifier])) {
160  return $this->newObjects[$identifier];
161  }
162  if ($this->persistenceSession->hasIdentifier($identifier, $objectType)) {
163  return $this->persistenceSession->getObjectByIdentifier($identifier, $objectType);
164  } else {
165  return $this->backend->getObjectByIdentifier($identifier, $objectType);
166  }
167  }
168 
176  public function persistAll()
177  {
178  // hand in only aggregate roots, leaving handling of subobjects to
179  // the underlying storage layer
180  // reconstituted entities must be fetched from the session and checked
181  // for changes by the underlying backend as well!
182  $this->backend->setAggregateRootObjects($this->addedObjects);
183  $this->backend->setChangedEntities($this->changedObjects);
184  $this->backend->setDeletedEntities($this->removedObjects);
185  $this->backend->commit();
186 
187  $this->addedObjects = new ObjectStorage();
188  $this->removedObjects = new ObjectStorage();
189  $this->changedObjects = new ObjectStorage();
190  }
191 
198  public function createQueryForType($type)
199  {
200  return $this->queryFactory->create($type);
201  }
202 
210  public function add($object)
211  {
212  $this->addedObjects->attach($object);
213  $this->removedObjects->detach($object);
214  }
215 
223  public function remove($object)
224  {
225  if ($this->addedObjects->contains($object)) {
226  $this->addedObjects->detach($object);
227  } else {
228  $this->removedObjects->attach($object);
229  }
230  }
231 
240  public function update($object)
241  {
242  if ($this->isNewObject($object)) {
243  throw new \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException('The object of type "' . get_class($object) . '" given to update must be persisted already, but is new.', 1249479819);
244  }
245  $this->changedObjects->attach($object);
246  }
247 
256  public function injectSettings(array $settings)
257  {
258  throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException(__METHOD__);
259  }
260 
266  public function initializeObject()
267  {
268  $this->backend->setPersistenceManager($this);
269  }
270 
280  public function clearState()
281  {
282  $this->newObjects = [];
283  $this->addedObjects = new ObjectStorage();
284  $this->removedObjects = new ObjectStorage();
285  $this->changedObjects = new ObjectStorage();
286  $this->persistenceSession->destroy();
287  }
288 
296  public function isNewObject($object)
297  {
298  return $this->persistenceSession->hasObject($object) === false;
299  }
300 
313  public function registerNewObject($object)
314  {
315  $identifier = $this->getIdentifierByObject($object);
316  $this->newObjects[$identifier] = $object;
317  }
318 
327  public function convertObjectToIdentityArray($object)
328  {
329  throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException(__METHOD__);
330  }
331 
342  public function convertObjectsToIdentityArrays(array $array)
343  {
344  throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException(__METHOD__);
345  }
346 
355  public function tearDown()
356  {
357  if (method_exists($this->backend, 'tearDown')) {
358  $this->backend->tearDown();
359  }
360  }
361 }
getObjectByIdentifier($identifier, $objectType=null, $useLazyLoading=false)
injectPersistenceSession(\TYPO3\CMS\Extbase\Persistence\Generic\Session $persistenceSession)
injectQueryFactory(\TYPO3\CMS\Extbase\Persistence\Generic\QueryFactoryInterface $queryFactory)
injectBackend(\TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface $backend)