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