‪TYPO3CMS  ‪main
PersistenceManager.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
25 
30 {
31  protected array ‪$newObjects = [];
38 
44  public function ‪__construct(
48  ) {
49  $this->queryFactory = ‪$queryFactory;
50  $this->backend = ‪$backend;
51  $this->persistenceSession = ‪$persistenceSession;
52 
53  $this->addedObjects = new ‪ObjectStorage();
54  $this->removedObjects = new ‪ObjectStorage();
55  $this->changedObjects = new ‪ObjectStorage();
56  }
57 
64  public function ‪registerRepositoryClassName(string $className): void {}
65 
69  public function ‪getObjectCountByQuery(‪QueryInterface $query): int
70  {
71  return $this->backend->getObjectCountByQuery($query);
72  }
73 
78  public function ‪getObjectDataByQuery(‪QueryInterface $query): array
79  {
80  return $this->backend->getObjectDataByQuery($query);
81  }
82 
93  public function ‪getIdentifierByObject(object $object): ?string
94  {
95  return $this->backend->getIdentifierByObject($object);
96  }
97 
105  public function ‪getObjectByIdentifier(string|int ‪$identifier, ?string $objectType = null, bool $useLazyLoading = false): ?object
106  {
107  if (isset($this->newObjects[‪$identifier])) {
108  return $this->newObjects[‪$identifier];
109  }
110  if ($this->persistenceSession->hasIdentifier((string)‪$identifier, $objectType)) {
111  return $this->persistenceSession->getObjectByIdentifier((string)‪$identifier, $objectType);
112  }
113  return $this->backend->getObjectByIdentifier((string)‪$identifier, $objectType);
114  }
115 
120  public function ‪persistAll(): void
121  {
122  // hand in only aggregate roots, leaving handling of subobjects to
123  // the underlying storage layer
124  // reconstituted entities must be fetched from the session and checked
125  // for changes by the underlying backend as well!
126  $this->backend->setAggregateRootObjects($this->addedObjects);
127  $this->backend->setChangedEntities($this->changedObjects);
128  $this->backend->setDeletedEntities($this->removedObjects);
129  $this->backend->commit();
130 
131  $this->addedObjects = new ‪ObjectStorage();
132  $this->removedObjects = new ‪ObjectStorage();
133  $this->changedObjects = new ‪ObjectStorage();
134  }
135 
145  public function ‪createQueryForType(string $type): ‪QueryInterface
146  {
147  return $this->queryFactory->create($type);
148  }
149 
155  public function ‪add(object $object): void
156  {
157  $this->addedObjects->attach($object);
158  $this->removedObjects->detach($object);
159  }
160 
166  public function remove(object $object): void
167  {
168  if ($this->addedObjects->contains($object)) {
169  $this->addedObjects->detach($object);
170  } else {
171  $this->removedObjects->attach($object);
172  }
173  }
174 
181  public function ‪update(object $object): void
182  {
183  if ($this->‪isNewObject($object)) {
184  throw new ‪UnknownObjectException('The object of type "' . get_class($object) . '" given to update must be persisted already, but is new.', 1249479819);
185  }
186  $this->changedObjects->attach($object);
187  }
188 
193  public function ‪initializeObject(): void
194  {
195  $this->backend->setPersistenceManager($this);
196  }
197 
207  public function ‪clearState(): void
208  {
209  $this->newObjects = [];
210  $this->addedObjects = new ‪ObjectStorage();
211  $this->removedObjects = new ‪ObjectStorage();
212  $this->changedObjects = new ‪ObjectStorage();
213  $this->persistenceSession->destroy();
214  }
215 
222  public function ‪isNewObject(object $object): bool
223  {
224  return $this->persistenceSession->hasObject($object) === false;
225  }
226 
240  public function ‪registerNewObject(object $object): void
241  {
242  ‪$identifier = $this->‪getIdentifierByObject($object);
243  $this->newObjects[‪$identifier] = $object;
244  }
245 
254  public function ‪tearDown(): void
255  {
256  if (method_exists($this->backend, 'tearDown')) {
257  $this->backend->tearDown();
258  }
259  }
260 }
‪TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager\initializeObject
‪initializeObject()
Definition: PersistenceManager.php:193
‪TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager\add
‪add(object $object)
Definition: PersistenceManager.php:155
‪TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager\update
‪update(object $object)
Definition: PersistenceManager.php:181
‪TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager\isNewObject
‪bool isNewObject(object $object)
Definition: PersistenceManager.php:222
‪TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
Definition: PersistenceManagerInterface.php:24
‪TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager\$changedObjects
‪ObjectStorage $changedObjects
Definition: PersistenceManager.php:32
‪TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager\$persistenceSession
‪Session $persistenceSession
Definition: PersistenceManager.php:37
‪TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager\getObjectByIdentifier
‪object null getObjectByIdentifier(string|int $identifier, ?string $objectType=null, bool $useLazyLoading=false)
Definition: PersistenceManager.php:105
‪TYPO3\CMS\Extbase\Persistence\QueryInterface
Definition: QueryInterface.php:30
‪TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager\__construct
‪__construct(QueryFactoryInterface $queryFactory, BackendInterface $backend, Session $persistenceSession)
Definition: PersistenceManager.php:44
‪TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager\clearState
‪clearState()
Definition: PersistenceManager.php:207
‪TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager\registerRepositoryClassName
‪registerRepositoryClassName(string $className)
Definition: PersistenceManager.php:64
‪TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager\getObjectCountByQuery
‪getObjectCountByQuery(QueryInterface $query)
Definition: PersistenceManager.php:69
‪TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager\$backend
‪BackendInterface $backend
Definition: PersistenceManager.php:36
‪TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager\registerNewObject
‪registerNewObject(object $object)
Definition: PersistenceManager.php:240
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage
Definition: ObjectStorage.php:34
‪TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager\$queryFactory
‪QueryFactoryInterface $queryFactory
Definition: PersistenceManager.php:35
‪TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager\$addedObjects
‪ObjectStorage $addedObjects
Definition: PersistenceManager.php:33
‪TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface
Definition: BackendInterface.php:26
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryFactoryInterface
Definition: QueryFactoryInterface.php:22
‪TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager
Definition: PersistenceManager.php:30
‪TYPO3\CMS\Extbase\Persistence\Generic\Session
Definition: Session.php:27
‪TYPO3\CMS\Extbase\Persistence\Generic
Definition: Backend.php:18
‪TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager\persistAll
‪persistAll()
Definition: PersistenceManager.php:120
‪TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
Definition: UnknownObjectException.php:25
‪TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager\tearDown
‪tearDown()
Definition: PersistenceManager.php:254
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager\$removedObjects
‪ObjectStorage $removedObjects
Definition: PersistenceManager.php:34
‪TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager\getIdentifierByObject
‪string null getIdentifierByObject(object $object)
Definition: PersistenceManager.php:93
‪TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager\$newObjects
‪array $newObjects
Definition: PersistenceManager.php:31
‪TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager\getObjectDataByQuery
‪list< array< string, mixed > > getObjectDataByQuery(QueryInterface $query)
Definition: PersistenceManager.php:78
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37
‪TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager\createQueryForType
‪QueryInterface< T > createQueryForType(string $type)
Definition: PersistenceManager.php:145