TYPO3 CMS  TYPO3_8-7
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 
101  public function registerRepositoryClassName($className)
102  {
103  }
104 
112  public function getObjectCountByQuery(QueryInterface $query)
113  {
114  return $this->backend->getObjectCountByQuery($query);
115  }
116 
124  public function getObjectDataByQuery(QueryInterface $query)
125  {
126  return $this->backend->getObjectDataByQuery($query);
127  }
128 
141  public function getIdentifierByObject($object)
142  {
143  return $this->backend->getIdentifierByObject($object);
144  }
145 
156  public function getObjectByIdentifier($identifier, $objectType = null, $useLazyLoading = false)
157  {
158  if (isset($this->newObjects[$identifier])) {
159  return $this->newObjects[$identifier];
160  }
161  if ($this->persistenceSession->hasIdentifier($identifier, $objectType)) {
162  return $this->persistenceSession->getObjectByIdentifier($identifier, $objectType);
163  }
164  return $this->backend->getObjectByIdentifier($identifier, $objectType);
165  }
166 
173  public function persistAll()
174  {
175  // hand in only aggregate roots, leaving handling of subobjects to
176  // the underlying storage layer
177  // reconstituted entities must be fetched from the session and checked
178  // for changes by the underlying backend as well!
179  $this->backend->setAggregateRootObjects($this->addedObjects);
180  $this->backend->setChangedEntities($this->changedObjects);
181  $this->backend->setDeletedEntities($this->removedObjects);
182  $this->backend->commit();
183 
184  $this->addedObjects = new ObjectStorage();
185  $this->removedObjects = new ObjectStorage();
186  $this->changedObjects = new ObjectStorage();
187  }
188 
195  public function createQueryForType($type)
196  {
197  return $this->queryFactory->create($type);
198  }
199 
206  public function add($object)
207  {
208  $this->addedObjects->attach($object);
209  $this->removedObjects->detach($object);
210  }
211 
218  public function remove($object)
219  {
220  if ($this->addedObjects->contains($object)) {
221  $this->addedObjects->detach($object);
222  } else {
223  $this->removedObjects->attach($object);
224  }
225  }
226 
234  public function update($object)
235  {
236  if ($this->isNewObject($object)) {
237  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);
238  }
239  $this->changedObjects->attach($object);
240  }
241 
249  public function injectSettings(array $settings)
250  {
251  throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException(__METHOD__, 1476108078);
252  }
253 
257  public function initializeObject()
258  {
259  $this->backend->setPersistenceManager($this);
260  }
261 
270  public function clearState()
271  {
272  $this->newObjects = [];
273  $this->addedObjects = new ObjectStorage();
274  $this->removedObjects = new ObjectStorage();
275  $this->changedObjects = new ObjectStorage();
276  $this->persistenceSession->destroy();
277  }
278 
286  public function isNewObject($object)
287  {
288  return $this->persistenceSession->hasObject($object) === false;
289  }
290 
302  public function registerNewObject($object)
303  {
304  $identifier = $this->getIdentifierByObject($object);
305  $this->newObjects[$identifier] = $object;
306  }
307 
316  public function convertObjectToIdentityArray($object)
317  {
318  throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException(__METHOD__, 1476108103);
319  }
320 
331  public function convertObjectsToIdentityArrays(array $array)
332  {
333  throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException(__METHOD__, 1476108111);
334  }
335 
342  public function tearDown()
343  {
344  if (method_exists($this->backend, 'tearDown')) {
345  $this->backend->tearDown();
346  }
347  }
348 }
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)