e5e65e609d1d301c2750c9878b6a50fb92726cbe
[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 * @return object The object for the identifier if it is known, or NULL
143 * @api
144 */
145 public function getObjectByIdentifier($identifier, $objectType) {
146 return $this->backend->getObjectByIdentifier($identifier, $objectType);
147 }
148
149 /**
150 * Commits new objects and changes to objects in the current persistence
151 * session into the backend
152 *
153 * @return void
154 * @api
155 */
156 public function persistAll() {
157 $aggregateRootObjects = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
158 $removedObjects = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
159 // fetch and inspect objects from all known repositories
160 foreach ($this->repositoryClassNames as $repositoryClassName) {
161 $repository = $this->objectManager->get($repositoryClassName);
162 $aggregateRootObjects->addAll($repository->getAddedObjects());
163 $removedObjects->addAll($repository->getRemovedObjects());
164 }
165 foreach ($this->session->getReconstitutedObjects() as $reconstitutedObject) {
166 if (class_exists(str_replace('_Model_', '_Repository_', get_class($reconstitutedObject)) . 'Repository')) {
167 $aggregateRootObjects->attach($reconstitutedObject);
168 }
169 }
170 // hand in only aggregate roots, leaving handling of subobjects to
171 // the underlying storage layer
172 $this->backend->setAggregateRootObjects($aggregateRootObjects);
173 $this->backend->setDeletedObjects($removedObjects);
174 $this->backend->commit();
175 // this needs to unregister more than just those, as at least some of
176 // the subobjects are supposed to go away as well...
177 // OTOH those do no harm, changes to the unused ones should not happen,
178 // so all they do is eat some memory.
179 foreach ($removedObjects as $removedObject) {
180 $this->session->unregisterReconstitutedObject($removedObject);
181 }
182 }
183
184 }
185
186
187 ?>