[CLEANUP] Add missing empty lines after copyright comments II
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / Generic / PersistenceManager.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Persistence\Generic;
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
17 use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
18 use TYPO3\CMS\Extbase\Persistence\QueryInterface;
19
20 /**
21 * The Extbase Persistence Manager
22 *
23 * @api
24 */
25 class PersistenceManager implements \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface, \TYPO3\CMS\Core\SingletonInterface {
26
27 /**
28 * @var array
29 */
30 protected $newObjects = array();
31
32 /**
33 * @var ObjectStorage
34 */
35 protected $changedObjects;
36
37 /**
38 * @var ObjectStorage
39 */
40 protected $addedObjects;
41
42 /**
43 * @var ObjectStorage
44 */
45 protected $removedObjects;
46
47 /**
48 * @var \TYPO3\CMS\Extbase\Persistence\Generic\QueryFactoryInterface
49 * @inject
50 */
51 protected $queryFactory;
52
53 /**
54 * @var \TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface
55 * @inject
56 */
57 protected $backend;
58
59 /**
60 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Session
61 * @inject
62 */
63 protected $persistenceSession;
64
65 /**
66 * Create new instance
67 */
68 public function __construct() {
69 $this->addedObjects = new ObjectStorage();
70 $this->removedObjects = new ObjectStorage();
71 $this->changedObjects = new ObjectStorage();
72 }
73
74 /**
75 * Registers a repository
76 *
77 * @param string $className The class name of the repository to be registered
78 * @return void
79 */
80 public function registerRepositoryClassName($className) {
81 }
82
83 /**
84 * Returns the number of records matching the query.
85 *
86 * @param QueryInterface $query
87 * @return int
88 * @api
89 */
90 public function getObjectCountByQuery(QueryInterface $query) {
91 return $this->backend->getObjectCountByQuery($query);
92 }
93
94 /**
95 * Returns the object data matching the $query.
96 *
97 * @param QueryInterface $query
98 * @return array
99 * @api
100 */
101 public function getObjectDataByQuery(QueryInterface $query) {
102 return $this->backend->getObjectDataByQuery($query);
103 }
104
105 /**
106 * Returns the (internal) identifier for the object, if it is known to the
107 * backend. Otherwise NULL is returned.
108 *
109 * Note: this returns an identifier even if the object has not been
110 * persisted in case of AOP-managed entities. Use isNewObject() if you need
111 * to distinguish those cases.
112 *
113 * @param object $object
114 * @return mixed The identifier for the object if it is known, or NULL
115 * @api
116 */
117 public function getIdentifierByObject($object) {
118 return $this->backend->getIdentifierByObject($object);
119 }
120
121 /**
122 * Returns the object with the (internal) identifier, if it is known to the
123 * backend. Otherwise NULL is returned.
124 *
125 * @param mixed $identifier
126 * @param string $objectType
127 * @param bool $useLazyLoading Set to TRUE if you want to use lazy loading for this object
128 * @return object The object for the identifier if it is known, or NULL
129 * @api
130 */
131 public function getObjectByIdentifier($identifier, $objectType = NULL, $useLazyLoading = FALSE) {
132 if (isset($this->newObjects[$identifier])) {
133 return $this->newObjects[$identifier];
134 }
135 if ($this->persistenceSession->hasIdentifier($identifier, $objectType)) {
136 return $this->persistenceSession->getObjectByIdentifier($identifier, $objectType);
137 } else {
138 return $this->backend->getObjectByIdentifier($identifier, $objectType);
139 }
140 }
141
142 /**
143 * Commits new objects and changes to objects in the current persistence
144 * session into the backend
145 *
146 * @return void
147 * @api
148 */
149 public function persistAll() {
150 // hand in only aggregate roots, leaving handling of subobjects to
151 // the underlying storage layer
152 // reconstituted entities must be fetched from the session and checked
153 // for changes by the underlying backend as well!
154 $this->backend->setAggregateRootObjects($this->addedObjects);
155 $this->backend->setChangedEntities($this->changedObjects);
156 $this->backend->setDeletedEntities($this->removedObjects);
157 $this->backend->commit();
158
159 $this->addedObjects = new ObjectStorage();
160 $this->removedObjects = new ObjectStorage();
161 $this->changedObjects = new ObjectStorage();
162 }
163
164 /**
165 * Return a query object for the given type.
166 *
167 * @param string $type
168 * @return QueryInterface
169 */
170 public function createQueryForType($type) {
171 return $this->queryFactory->create($type);
172 }
173
174 /**
175 * Adds an object to the persistence.
176 *
177 * @param object $object The object to add
178 * @return void
179 * @api
180 */
181 public function add($object) {
182 $this->addedObjects->attach($object);
183 $this->removedObjects->detach($object);
184 }
185
186 /**
187 * Removes an object to the persistence.
188 *
189 * @param object $object The object to remove
190 * @return void
191 * @api
192 */
193 public function remove($object) {
194 if ($this->addedObjects->contains($object)) {
195 $this->addedObjects->detach($object);
196 } else {
197 $this->removedObjects->attach($object);
198 }
199 }
200
201 /**
202 * Update an object in the persistence.
203 *
204 * @param object $object The modified object
205 * @return void
206 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
207 * @api
208 */
209 public function update($object) {
210 if ($this->isNewObject($object)) {
211 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);
212 }
213 $this->changedObjects->attach($object);
214 }
215
216 /**
217 * Injects the Extbase settings, called by Extbase.
218 *
219 * @param array $settings
220 * @return void
221 * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException
222 * @api
223 */
224 public function injectSettings(array $settings) {
225 throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException(__METHOD__);
226 }
227
228 /**
229 * Initializes the persistence manager, called by Extbase.
230 *
231 * @return void
232 */
233 public function initializeObject() {
234 $this->backend->setPersistenceManager($this);
235 }
236
237 /**
238 * Clears the in-memory state of the persistence.
239 *
240 * Managed instances become detached, any fetches will
241 * return data directly from the persistence "backend".
242 *
243 * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException
244 * @return void
245 */
246 public function clearState() {
247 $this->newObjects = array();
248 $this->addedObjects = new ObjectStorage();
249 $this->removedObjects = new ObjectStorage();
250 $this->changedObjects = new ObjectStorage();
251 $this->persistenceSession->destroy();
252 }
253
254 /**
255 * Checks if the given object has ever been persisted.
256 *
257 * @param object $object The object to check
258 * @return bool TRUE if the object is new, FALSE if the object exists in the persistence session
259 * @api
260 */
261 public function isNewObject($object) {
262 return ($this->persistenceSession->hasObject($object) === FALSE);
263 }
264
265 /**
266 * Registers an object which has been created or cloned during this request.
267 *
268 * A "new" object does not necessarily
269 * have to be known by any repository or be persisted in the end.
270 *
271 * Objects registered with this method must be known to the getObjectByIdentifier()
272 * method.
273 *
274 * @param object $object The new object to register
275 * @return void
276 */
277 public function registerNewObject($object) {
278 $identifier = $this->getIdentifierByObject($object);
279 $this->newObjects[$identifier] = $object;
280 }
281
282 /**
283 * Converts the given object into an array containing the identity of the domain object.
284 *
285 * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException
286 * @param object $object The object to be converted
287 * @return array
288 * @api
289 */
290 public function convertObjectToIdentityArray($object) {
291 throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException(__METHOD__);
292 }
293
294 /**
295 * Recursively iterates through the given array and turns objects
296 * into arrays containing the identity of the domain object.
297 *
298 * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException
299 * @param array $array The array to be iterated over
300 * @return array
301 * @api
302 * @see convertObjectToIdentityArray()
303 */
304 public function convertObjectsToIdentityArrays(array $array) {
305 throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException(__METHOD__);
306 }
307
308 /**
309 * Tear down the persistence
310 *
311 * This method is called in functional tests to reset the storage between tests.
312 * The implementation is optional and depends on the underlying persistence backend.
313 *
314 * @return void
315 */
316 public function tearDown() {
317 if (method_exists($this->backend, 'tearDown')) {
318 $this->backend->tearDown();
319 }
320 }
321
322 }