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