[TASK] Resync TYPO3Flow- and Extbase-Persistence
[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) 2009 Jochen Rau <jochen.rau@typoplanet.de>
8 * All rights reserved
9 *
10 * This class is a backport of the corresponding class of FLOW3.
11 * All credits go to the v5 team.
12 *
13 * This script is part of the TYPO3 project. The TYPO3 project is
14 * free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * The GNU General Public License can be found at
20 * http://www.gnu.org/copyleft/gpl.html.
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * The Extbase Persistence Manager
31 *
32 * @api
33 */
34 class PersistenceManager implements \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface, \TYPO3\CMS\Core\SingletonInterface {
35
36 /**
37 * @var \TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface
38 */
39 protected $backend;
40
41 /**
42 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Session
43 */
44 protected $session;
45
46 /**
47 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
48 */
49 protected $objectManager;
50
51 /**
52 * This is an array of registered repository class names.
53 *
54 * @var array
55 */
56 protected $repositoryClassNames = array();
57
58 /**
59 * Injects the Persistence Backend
60 *
61 * @param \TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface $backend The persistence backend
62 * @return void
63 */
64 public function injectBackend(\TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface $backend) {
65 $this->backend = $backend;
66 }
67
68 /**
69 * Injects the Persistence Session
70 *
71 * @param \TYPO3\CMS\Extbase\Persistence\Generic\Session $session The persistence session
72 * @return void
73 */
74 public function injectSession(\TYPO3\CMS\Extbase\Persistence\Generic\Session $session) {
75 $this->session = $session;
76 }
77
78 /**
79 * Injects the object manager
80 *
81 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
82 * @return void
83 */
84 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager) {
85 $this->objectManager = $objectManager;
86 }
87
88 /**
89 * Registers a repository
90 *
91 * @param string $className The class name of the repository to be reigistered
92 * @return void
93 */
94 public function registerRepositoryClassName($className) {
95 $this->repositoryClassNames[] = $className;
96 }
97
98 /**
99 * Returns the number of records matching the query.
100 *
101 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
102 * @return integer
103 * @api
104 */
105 public function getObjectCountByQuery(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query) {
106 return $this->backend->getObjectCountByQuery($query);
107 }
108
109 /**
110 * Returns the object data matching the $query.
111 *
112 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
113 * @return array
114 * @api
115 */
116 public function getObjectDataByQuery(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query) {
117 return $this->backend->getObjectDataByQuery($query);
118 }
119
120 /**
121 * Returns the (internal) identifier for the object, if it is known to the
122 * backend. Otherwise NULL is returned.
123 *
124 * Note: this returns an identifier even if the object has not been
125 * persisted in case of AOP-managed entities. Use isNewObject() if you need
126 * to distinguish those cases.
127 *
128 * @param object $object
129 * @return mixed The identifier for the object if it is known, or NULL
130 * @api
131 */
132 public function getIdentifierByObject($object) {
133 return $this->backend->getIdentifierByObject($object);
134 }
135
136 /**
137 * Returns the object with the (internal) identifier, if it is known to the
138 * backend. Otherwise NULL is returned.
139 *
140 * @param mixed $identifier
141 * @param string $objectType
142 * @param boolean $useLazyLoading Set to TRUE if you want to use lazy loading for this object
143 * @return object The object for the identifier if it is known, or NULL
144 * @api
145 */
146 public function getObjectByIdentifier($identifier, $objectType = NULL, $useLazyLoading = FALSE) {
147 return $this->backend->getObjectByIdentifier($identifier, $objectType);
148 }
149
150 /**
151 * Commits new objects and changes to objects in the current persistence
152 * session into the backend
153 *
154 * @return void
155 * @api
156 */
157 public function persistAll() {
158 $aggregateRootObjects = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
159 $removedObjects = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
160 // fetch and inspect objects from all known repositories
161 foreach ($this->repositoryClassNames as $repositoryClassName) {
162 $repository = $this->objectManager->get($repositoryClassName);
163 $aggregateRootObjects->addAll($repository->getAddedObjects());
164 $removedObjects->addAll($repository->getRemovedObjects());
165 }
166 foreach ($this->session->getReconstitutedObjects() as $reconstitutedObject) {
167 if (class_exists(str_replace('_Model_', '_Repository_', get_class($reconstitutedObject)) . 'Repository')) {
168 $aggregateRootObjects->attach($reconstitutedObject);
169 }
170 }
171 // hand in only aggregate roots, leaving handling of subobjects to
172 // the underlying storage layer
173 $this->backend->setAggregateRootObjects($aggregateRootObjects);
174 $this->backend->setDeletedObjects($removedObjects);
175 $this->backend->commit();
176 // this needs to unregister more than just those, as at least some of
177 // the subobjects are supposed to go away as well...
178 // OTOH those do no harm, changes to the unused ones should not happen,
179 // so all they do is eat some memory.
180 foreach ($removedObjects as $removedObject) {
181 $this->session->unregisterReconstitutedObject($removedObject);
182 }
183 }
184
185 /**
186 * Adds an object to the persistence.
187 *
188 * @param object $object The object to add
189 * @return void
190 * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException
191 * @api
192 */
193 public function add($object) {
194 throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException(__METHOD__);
195 }
196
197 /**
198 * Removes an object to the persistence.
199 *
200 * @param object $object The object to remove
201 * @return void
202 * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException
203 * @api
204 */
205 public function remove($object) {
206 throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException(__METHOD__);
207 }
208
209 /**
210 * Update an object in the persistence.
211 *
212 * @param object $object The modified object
213 * @return void
214 * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException
215 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
216 * @api
217 */
218 public function update($object) {
219 throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException(__METHOD__);
220 }
221
222 /**
223 * Injects the Extbase settings, called by Extbase.
224 *
225 * @param array $settings
226 * @return void
227 * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException
228 * @api
229 */
230 public function injectSettings(array $settings) {
231 throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException(__METHOD__);
232 }
233
234 /**
235 * Initializes the persistence manager, called by Extbase.
236 *
237 * @return void
238 * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException
239 * @api
240 */
241 public function initialize() {
242 throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException(__METHOD__);
243 }
244
245 /**
246 * Clears the in-memory state of the persistence.
247 *
248 * Managed instances become detached, any fetches will
249 * return data directly from the persistence "backend".
250 *
251 * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException
252 * @return void
253 */
254 public function clearState() {
255 throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException(__METHOD__);
256 }
257
258 /**
259 * Checks if the given object has ever been persisted.
260 *
261 * @param object $object The object to check
262 * @return boolean TRUE if the object is new, FALSE if the object exists in the repository
263 * @api
264 */
265 public function isNewObject($object) {
266 return $this->backend->isNewObject($object);
267 }
268
269 /**
270 * Converts the given object into an array containing the identity of the domain object.
271 *
272 * @param object $object The object to be converted
273 * @return array The identity array in the format array('__identity' => '...')
274 * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException
275 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException if the given object is not known to the Persistence Manager
276 * @api
277 */
278 public function convertObjectToIdentityArray($object) {
279 throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException(__METHOD__);
280 }
281
282 /**
283 * Recursively iterates through the given array and turns objects
284 * into arrays containing the identity of the domain object.
285 *
286 * @param array $array The array to be iterated over
287 * @return array The modified array without objects
288 * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException
289 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException if array contains objects that are not known to the Persistence Manager
290 * @api
291 * @see convertObjectToIdentityArray()
292 */
293 public function convertObjectsToIdentityArrays(array $array) {
294 throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException(__METHOD__);
295 }
296
297 /**
298 * Return a query object for the given type.
299 *
300 * @param string $type
301 * @return \TYPO3\CMS\Extbase\Persistence\QueryInterface
302 * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException
303 * @api
304 */
305 public function createQueryForType($type) {
306 throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException(__METHOD__);
307 }
308 }
309
310
311 ?>