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