[!!!][~API] Extbase (Persistence): $query->execute() now returns a plain array of...
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / Manager.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
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 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * The Extbase Persistence Manager
30 *
31 * @package Extbase
32 * @subpackage Persistence
33 * @version $Id$
34 * @api
35 */
36 class Tx_Extbase_Persistence_Manager implements Tx_Extbase_Persistence_ManagerInterface, t3lib_Singleton {
37
38 /**
39 * @var Tx_Extbase_Persistence_BackendInterface
40 */
41 protected $backend;
42
43 /**
44 * @var Tx_Extbase_Persistence_Session
45 */
46 protected $session;
47
48 /**
49 * @var Tx_Extbase_Object_ManagerInterface
50 */
51 protected $objectManager;
52
53 /**
54 * This is an array of registered repository class names.
55 *
56 * @var array
57 */
58 protected $repositoryClassNames = array();
59
60 /**
61 * Injects the Persistence Backend
62 *
63 * @param Tx_Extbase_Persistence_BackendInterface $backend The persistence backend
64 * @return void
65 */
66 public function injectBackend(Tx_Extbase_Persistence_BackendInterface $backend) {
67 $this->backend = $backend;
68 }
69
70 /**
71 *
72 * Injects the Persistence Session
73 *
74 * @param Tx_Extbase_Persistence_Session $session The persistence session
75 * @return void
76 */
77 public function injectSession(Tx_Extbase_Persistence_Session $session) {
78 $this->session = $session;
79 }
80
81 /**
82 * Injects the object manager
83 *
84 * @param Tx_Extbase_Object_ManagerInterface $objectManager
85 * @return void
86 */
87 public function injectObjectManager(Tx_Extbase_Object_ManagerInterface $objectManager) {
88 $this->objectManager = $objectManager;
89 }
90
91 /**
92 * Returns the current persistence session
93 *
94 * @return Tx_Extbase_Persistence_Session
95 */
96 public function getSession() {
97 return $this->session;
98 }
99
100 /**
101 * Returns the persistence backend
102 *
103 * @return Tx_Extbase_Persistence_BackendInterface
104 */
105 public function getBackend() {
106 return $this->backend;
107 }
108
109 /**
110 * Registers a repository
111 *
112 * @param string $className The class name of the repository to be reigistered
113 * @return void
114 */
115 public function registerRepositoryClassName($className) {
116 $this->repositoryClassNames[] = $className;
117 }
118
119 /**
120 * Returns all repository class names
121 *
122 * @return array An array holding the registered repository class names
123 */
124 public function getRepositoryClassNames() {
125 return $this->repositoryClassNames;
126 }
127
128 /**
129 * Returns the number of records matching the query.
130 *
131 * @param Tx_Extbase_Persistence_QueryInterface $query
132 * @return integer
133 * @api
134 */
135 public function getObjectCountByQuery(Tx_Extbase_Persistence_QueryInterface $query) {
136 return $this->backend->getObjectCountByQuery($query);
137 }
138
139 /**
140 * Returns the object data matching the $query.
141 *
142 * @param Tx_Extbase_Persistence_QueryInterface $query
143 * @return array
144 * @api
145 */
146 public function getObjectDataByQuery(Tx_Extbase_Persistence_QueryInterface $query) {
147 return $this->backend->getObjectDataByQuery($query);
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 Tx_Extbase_Persistence_ObjectStorage();
159 $removedObjects = new Tx_Extbase_Persistence_ObjectStorage();
160
161 // fetch and inspect objects from all known repositories
162 $repositoryClassNames = $this->getRepositoryClassNames();
163 foreach ($repositoryClassNames as $repositoryClassName) {
164 $repository = $this->objectManager->getObject($repositoryClassName);
165 $aggregateRootObjects->addAll($repository->getAddedObjects());
166 $removedObjects->addAll($repository->getRemovedObjects());
167 }
168
169 foreach ($this->session->getReconstitutedObjects() as $reconstitutedObject) {
170 if (class_exists(str_replace('_Model_', '_Repository_', get_class($reconstitutedObject)) . 'Repository')) {
171 $aggregateRootObjects->attach($reconstitutedObject);
172 }
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
181 // this needs to unregister more than just those, as at least some of
182 // the subobjects are supposed to go away as well...
183 // OTOH those do no harm, changes to the unused ones should not happen,
184 // so all they do is eat some memory.
185 foreach($removedObjects as $removedObject) {
186 $this->session->unregisterReconstitutedObject($removedObject);
187 }
188 }
189
190 }
191 ?>