[TASK] Namespace classes
[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 * @package Extbase
33 * @subpackage Persistence
34 * @version $Id$
35 * @api
36 */
37 class PersistenceManager implements \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface, \TYPO3\CMS\Core\SingletonInterface {
38
39 /**
40 * @var \TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface
41 */
42 protected $backend;
43
44 /**
45 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Session
46 */
47 protected $session;
48
49 /**
50 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
51 */
52 protected $objectManager;
53
54 /**
55 * This is an array of registered repository class names.
56 *
57 * @var array
58 */
59 protected $repositoryClassNames = array();
60
61 /**
62 * Injects the Persistence Backend
63 *
64 * @param \TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface $backend The persistence backend
65 * @return void
66 */
67 public function injectBackend(\TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface $backend) {
68 $this->backend = $backend;
69 }
70
71 /**
72 * Injects the Persistence Session
73 *
74 * @param \TYPO3\CMS\Extbase\Persistence\Generic\Session $session The persistence session
75 * @return void
76 */
77 public function injectSession(\TYPO3\CMS\Extbase\Persistence\Generic\Session $session) {
78 $this->session = $session;
79 }
80
81 /**
82 * Injects the object manager
83 *
84 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
85 * @return void
86 */
87 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager) {
88 $this->objectManager = $objectManager;
89 }
90
91 /**
92 * Returns the current persistence session
93 *
94 * @return \TYPO3\CMS\Extbase\Persistence\Generic\Session
95 * @deprecated since Extbase 1.4.0, will be removed in Extbase 6.0
96 */
97 public function getSession() {
98 return $this->session;
99 }
100
101 /**
102 * Returns the persistence backend
103 *
104 * @return \TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface
105 * @deprecated since Extbase 1.4.0, will be removed in Extbase 6.0
106 */
107 public function getBackend() {
108 return $this->backend;
109 }
110
111 /**
112 * Registers a repository
113 *
114 * @param string $className The class name of the repository to be reigistered
115 * @return void
116 */
117 public function registerRepositoryClassName($className) {
118 $this->repositoryClassNames[] = $className;
119 }
120
121 /**
122 * Returns the number of records matching the query.
123 *
124 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
125 * @return integer
126 * @api
127 */
128 public function getObjectCountByQuery(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query) {
129 return $this->backend->getObjectCountByQuery($query);
130 }
131
132 /**
133 * Returns the object data matching the $query.
134 *
135 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
136 * @return array
137 * @api
138 */
139 public function getObjectDataByQuery(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query) {
140 return $this->backend->getObjectDataByQuery($query);
141 }
142
143 /**
144 * Returns the (internal) identifier for the object, if it is known to the
145 * backend. Otherwise NULL is returned.
146 *
147 * Note: this returns an identifier even if the object has not been
148 * persisted in case of AOP-managed entities. Use isNewObject() if you need
149 * to distinguish those cases.
150 *
151 * @param object $object
152 * @return mixed The identifier for the object if it is known, or NULL
153 * @api
154 */
155 public function getIdentifierByObject($object) {
156 return $this->backend->getIdentifierByObject($object);
157 }
158
159 /**
160 * Returns the object with the (internal) identifier, if it is known to the
161 * backend. Otherwise NULL is returned.
162 *
163 * @param mixed $identifier
164 * @param string $objectType
165 * @return object The object for the identifier if it is known, or NULL
166 * @api
167 */
168 public function getObjectByIdentifier($identifier, $objectType) {
169 return $this->backend->getObjectByIdentifier($identifier, $objectType);
170 }
171
172 /**
173 * Commits new objects and changes to objects in the current persistence
174 * session into the backend
175 *
176 * @return void
177 * @api
178 */
179 public function persistAll() {
180 $aggregateRootObjects = new \TYPO3\CMS\Extbase\Persistence\Generic\ObjectStorage();
181 $removedObjects = new \TYPO3\CMS\Extbase\Persistence\Generic\ObjectStorage();
182 // fetch and inspect objects from all known repositories
183 foreach ($this->repositoryClassNames as $repositoryClassName) {
184 $repository = $this->objectManager->get($repositoryClassName);
185 $aggregateRootObjects->addAll($repository->getAddedObjects());
186 $removedObjects->addAll($repository->getRemovedObjects());
187 }
188 foreach ($this->session->getReconstitutedObjects() as $reconstitutedObject) {
189 if (class_exists(str_replace('_Model_', '_Repository_', get_class($reconstitutedObject)) . 'Repository')) {
190 $aggregateRootObjects->attach($reconstitutedObject);
191 }
192 }
193 // hand in only aggregate roots, leaving handling of subobjects to
194 // the underlying storage layer
195 $this->backend->setAggregateRootObjects($aggregateRootObjects);
196 $this->backend->setDeletedObjects($removedObjects);
197 $this->backend->commit();
198 // this needs to unregister more than just those, as at least some of
199 // the subobjects are supposed to go away as well...
200 // OTOH those do no harm, changes to the unused ones should not happen,
201 // so all they do is eat some memory.
202 foreach ($removedObjects as $removedObject) {
203 $this->session->unregisterReconstitutedObject($removedObject);
204 }
205 }
206
207 }
208
209
210 ?>