30617a976eb37a3de8f0098b0e40af0f0a1d4a81
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / Generic / PersistenceManager.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Persistence\Generic;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2013 Extbase Team (http://forge.typo3.org/projects/typo3v4-mvc)
8 * Extbase is a backport of TYPO3 Flow. All credits go to the TYPO3 Flow team.
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the textfile GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30 /**
31 * The Extbase Persistence Manager
32 *
33 * @api
34 */
35 class PersistenceManager implements \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface, \TYPO3\CMS\Core\SingletonInterface {
36
37 /**
38 * @var array
39 */
40 protected $newObjects = array();
41
42 /**
43 * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage
44 */
45 protected $changedObjects;
46
47 /**
48 * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage
49 */
50 protected $addedObjects;
51
52 /**
53 * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage
54 */
55 protected $removedObjects;
56
57 /**
58 * @var \TYPO3\CMS\Extbase\Persistence\Generic\QueryFactoryInterface
59 */
60 protected $queryFactory;
61
62 /**
63 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Backend
64 */
65 protected $backend;
66
67 /**
68 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Session
69 */
70 protected $persistenceSession;
71
72 /**
73 * Create new instance
74 */
75 public function __construct() {
76 $this->addedObjects = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
77 $this->removedObjects = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
78 $this->changedObjects = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
79 }
80
81 /**
82 * Injects the Persistence Backend
83 *
84 * @param \TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface $backend The persistence backend
85 * @return void
86 */
87 public function injectBackend(\TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface $backend) {
88 $this->backend = $backend;
89 }
90
91 /**
92 * Injects a QueryFactory instance
93 *
94 * @param \TYPO3\CMS\Extbase\Persistence\Generic\QueryFactoryInterface $queryFactory
95 * @return void
96 */
97 public function injectQueryFactory(\TYPO3\CMS\Extbase\Persistence\Generic\QueryFactoryInterface $queryFactory) {
98 $this->queryFactory = $queryFactory;
99 }
100
101 /**
102 * Injects the Persistence Session
103 *
104 * @param \TYPO3\CMS\Extbase\Persistence\Generic\Session $session The persistence session
105 * @return void
106 */
107 public function injectPersistenceSession(\TYPO3\CMS\Extbase\Persistence\Generic\Session $session) {
108 $this->persistenceSession = $session;
109 }
110
111 /**
112 * Registers a repository
113 *
114 * @param string $className The class name of the repository to be reigistered
115 * @deprecated since 6.1, will be remove two versions later
116 * @return void
117 */
118 public function registerRepositoryClassName($className) {
119 }
120
121 /**
122 * Returns the number of records matching the query.
123 *
124 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
125 * @return integer
126 * @api
127 */
128 public function getObjectCountByQuery(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query) {
129 return $this->backend->getObjectCountByQuery($query);
130 }
131
132 /**
133 * Returns the object data matching the $query.
134 *
135 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
136 * @return array
137 * @api
138 */
139 public function getObjectDataByQuery(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query) {
140 return $this->backend->getObjectDataByQuery($query);
141 }
142
143 /**
144 * Returns the (internal) identifier for the object, if it is known to the
145 * backend. Otherwise NULL is returned.
146 *
147 * Note: this returns an identifier even if the object has not been
148 * persisted in case of AOP-managed entities. Use isNewObject() if you need
149 * to distinguish those cases.
150 *
151 * @param object $object
152 * @return mixed The identifier for the object if it is known, or NULL
153 * @api
154 */
155 public function getIdentifierByObject($object) {
156 return $this->persistenceSession->getIdentifierByObject($object);
157 }
158
159 /**
160 * Returns the object with the (internal) identifier, if it is known to the
161 * backend. Otherwise NULL is returned.
162 *
163 * @param mixed $identifier
164 * @param string $objectType
165 * @param boolean $useLazyLoading Set to TRUE if you want to use lazy loading for this object
166 * @return object The object for the identifier if it is known, or NULL
167 * @api
168 */
169 public function getObjectByIdentifier($identifier, $objectType = NULL, $useLazyLoading = FALSE) {
170 if (isset($this->newObjects[$identifier])) {
171 return $this->newObjects[$identifier];
172 }
173 if ($this->persistenceSession->hasIdentifier($identifier, $objectType)) {
174 return $this->persistenceSession->getObjectByIdentifier($identifier, $objectType);
175 } else {
176 return $this->backend->getObjectByIdentifier($identifier, $objectType);
177 }
178 }
179
180 /**
181 * Commits new objects and changes to objects in the current persistence
182 * session into the backend
183 *
184 * @return void
185 * @api
186 */
187 public function persistAll() {
188 // hand in only aggregate roots, leaving handling of subobjects to
189 // the underlying storage layer
190 // reconstituted entities must be fetched from the session and checked
191 // for changes by the underlying backend as well!
192 $this->backend->setAggregateRootObjects($this->addedObjects);
193 $this->backend->setChangedEntities($this->changedObjects);
194 $this->backend->setDeletedEntities($this->removedObjects);
195 $this->backend->commit();
196
197 $this->addedObjects = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
198 $this->removedObjects = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
199 $this->changedObjects = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
200 }
201
202 /**
203 * Return a query object for the given type.
204 *
205 * @param string $type
206 * @return \TYPO3\CMS\Extbase\Persistence\QueryInterface
207 */
208 public function createQueryForType($type) {
209 return $this->queryFactory->create($type);
210 }
211
212 /**
213 * Adds an object to the persistence.
214 *
215 * @param object $object The object to add
216 * @return void
217 * @api
218 */
219 public function add($object) {
220 $this->addedObjects->attach($object);
221 $this->removedObjects->detach($object);
222 }
223
224 /**
225 * Removes an object to the persistence.
226 *
227 * @param object $object The object to remove
228 * @return void
229 * @api
230 */
231 public function remove($object) {
232 if ($this->addedObjects->contains($object)) {
233 $this->addedObjects->detach($object);
234 } else {
235 $this->removedObjects->attach($object);
236 }
237 }
238
239 /**
240 * Update an object in the persistence.
241 *
242 * @param object $object The modified object
243 * @return void
244 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
245 * @api
246 */
247 public function update($object) {
248 if ($this->isNewObject($object)) {
249 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);
250 }
251 $this->changedObjects->attach($object);
252 }
253
254 /**
255 * Injects the Extbase settings, called by Extbase.
256 *
257 * @param array $settings
258 * @return void
259 * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException
260 * @api
261 */
262 public function injectSettings(array $settings) {
263 throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException(__METHOD__);
264 }
265
266 /**
267 * Initializes the persistence manager, called by Extbase.
268 *
269 * @return void
270 */
271 public function initializeObject() {
272 $this->backend->setPersistenceManager($this);
273 }
274
275 /**
276 * Clears the in-memory state of the persistence.
277 *
278 * Managed instances become detached, any fetches will
279 * return data directly from the persistence "backend".
280 *
281 * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException
282 * @return void
283 */
284 public function clearState() {
285 $this->newObjects = array();
286 $this->addedObjects = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
287 $this->removedObjects = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
288 $this->changedObjects = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
289 $this->persistenceSession->destroy();
290 }
291
292 /**
293 * Checks if the given object has ever been persisted.
294 *
295 * @param object $object The object to check
296 * @return boolean TRUE if the object is new, FALSE if the object exists in the persistence session
297 * @api
298 */
299 public function isNewObject($object) {
300 return ($this->persistenceSession->hasObject($object) === FALSE);
301 }
302
303 /**
304 * Registers an object which has been created or cloned during this request.
305 *
306 * A "new" object does not necessarily
307 * have to be known by any repository or be persisted in the end.
308 *
309 * Objects registered with this method must be known to the getObjectByIdentifier()
310 * method.
311 *
312 * @param object $object The new object to register
313 * @return void
314 */
315 public function registerNewObject($object) {
316 $identifier = $this->getIdentifierByObject($object);
317 $this->newObjects[$identifier] = $object;
318 }
319
320 /**
321 * Converts the given object into an array containing the identity of the domain object.
322 *
323 * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException
324 * @param object $object The object to be converted
325 * @api
326 */
327 public function convertObjectToIdentityArray($object) {
328 throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException(__METHOD__);
329 }
330
331 /**
332 * Recursively iterates through the given array and turns objects
333 * into arrays containing the identity of the domain object.
334 *
335 * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException
336 * @param array $array The array to be iterated over
337 * @api
338 * @see convertObjectToIdentityArray()
339 */
340 public function convertObjectsToIdentityArrays(array $array) {
341 throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException(__METHOD__);
342 }
343
344 /**
345 * Tear down the persistence
346 *
347 * This method is called in functional tests to reset the storage between tests.
348 * The implementation is optional and depends on the underlying persistence backend.
349 *
350 * @return void
351 */
352 public function tearDown() {
353 if (method_exists($this->backend, 'tearDown')) {
354 $this->backend->tearDown();
355 }
356 }
357
358 }
359
360 ?>