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