* renamed TcaMapper to ObjectRelationalMapper
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / TX_EXTMVC_Persistence_Session.php
1 <?php
2 declare(ENCODING = 'utf-8');
3
4 /* *
5 * This script belongs to the FLOW3 framework. *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License as published by the *
9 * Free Software Foundation, either version 3 of the License, or (at your *
10 * option) any later version. *
11 * *
12 * This script is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15 * General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with the script. *
19 * If not, see http://www.gnu.org/licenses/lgpl.html *
20 * *
21 * The TYPO3 project - inspiring people to share! *
22 * */
23
24 require_once(PATH_t3lib . 'interfaces/interface.t3lib_singleton.php');
25 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/DomainObject/TX_EXTMVC_DomainObject_AbstractDomainObject.php');
26 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Persistence/TX_EXTMVC_Persistence_ObjectStorage.php');
27
28 /**
29 * The persistence session - acts as a Unit of Work for EXCMVC's persistence framework.
30 *
31 * @version $Id:$
32 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
33 */
34 class TX_EXTMVC_Persistence_Session implements t3lib_singleton {
35 // TODO Implement against SessionInterface
36
37 /**
38 * Objects added to the repository but not yet persisted in the persistence backend
39 *
40 * @var TX_EXTMVC_Persistence_ObjectStorage
41 */
42 protected $addedObjects;
43
44 /**
45 * Objects removed but not yet persisted in the persistence backend
46 *
47 * @var TX_EXTMVC_Persistence_ObjectStorage
48 */
49 protected $removedObjects;
50
51 /**
52 * @var TX_EXTMVC_Persistence_ObjectStorage
53 */
54 protected $reconstitutedObjects;
55
56 /**
57 * @var array
58 */
59 protected $aggregateRootClassNames = array();
60
61 /**
62 * Constructs a new Session
63 *
64 * @author Jochen Rau <jochen.rau@typoplanet.de>
65 */
66 public function __construct() {
67 $this->addedObjects = new TX_EXTMVC_Persistence_ObjectStorage();
68 $this->removedObjects = new TX_EXTMVC_Persistence_ObjectStorage();
69 $this->reconstitutedObjects = new TX_EXTMVC_Persistence_ObjectStorage();
70 }
71
72 /**
73 * Registers an added object
74 *
75 * @param TX_EXTMVC_DomainObject_AbstractDomainObject $object
76 * @return void
77 * @author Jochen Rau <jochen.rau@typoplanet.de>
78 */
79 public function registerAddedObject(TX_EXTMVC_DomainObject_AbstractDomainObject $object) {
80 if ($this->reconstitutedObjects->contains($object)) throw new InvalidArgumentException('The object was registered as reconstituted and can therefore not be registered as added.');
81 $this->removedObjects->detach($object);
82 $this->addedObjects->attach($object);
83 }
84
85 /**
86 * Unregisters an added object
87 *
88 * @param TX_EXTMVC_DomainObject_AbstractDomainObject $object
89 * @return void
90 * @author Jochen Rau <jochen.rau@typoplanet.de>
91 */
92 public function unregisterAddedObject(TX_EXTMVC_DomainObject_AbstractDomainObject $object) {
93 $this->addedObjects->detach($object);
94 }
95
96 /**
97 * Returns all objects which have been registered as added objects
98 *
99 * @param string $objectClassName The class name of objects to be returned
100 * @return TX_EXTMVC_Persistence_ObjectStorage All added objects
101 * @author Jochen Rau <jochen.rau@typoplanet.de>
102 */
103 public function getAddedObjects($objectClassName = NULL) {
104 $addedObjects = array();
105 foreach ($this->addedObjects as $object) {
106 if ($objectClassName != NULL && !($object instanceof $objectClassName)) continue;
107 $addedObjects[] = $object;
108 }
109 return $addedObjects;
110 }
111
112 /**
113 * Returns TRUE if the given object is registered as added
114 *
115 * @param TX_EXTMVC_DomainObject_AbstractDomainObject $object
116 * @return bool TRUE if the given object is registered as added; otherwise FALSE
117 * @author Jochen Rau <jochen.rau@typoplanet.de>
118 */
119 public function isAddedObject(TX_EXTMVC_DomainObject_AbstractDomainObject $object) {
120 return $this->addedObjects->contains($object);
121 }
122
123 /**
124 * Registers a removed object
125 *
126 * @param TX_EXTMVC_DomainObject_AbstractDomainObject $object
127 * @return void
128 * @author Jochen Rau <jochen.rau@typoplanet.de>
129 */
130 public function registerRemovedObject(TX_EXTMVC_DomainObject_AbstractDomainObject $object) {
131 if ($this->addedObjects->contains($object)) {
132 $this->addedObjects->detach($object);
133 } else {
134 $this->removedObjects->attach($object);
135 }
136 }
137
138 /**
139 * Unregisters a removed object
140 *
141 * @param TX_EXTMVC_DomainObject_AbstractDomainObject $object
142 * @return void
143 * @author Jochen Rau <jochen.rau@typoplanet.de>
144 */
145 public function unregisterRemovedObject(TX_EXTMVC_DomainObject_AbstractDomainObject $object) {
146 $this->removedObjects->detach($object);
147 }
148
149 /**
150 * Returns all objects which have been registered as removed objects
151 *
152 * @param string $objectClassName The class name of objects to be returned
153 * @return TX_EXTMVC_Persistence_ObjectStorage All removed objects
154 * @author Jochen Rau <jochen.rau@typoplanet.de>
155 */
156 public function getRemovedObjects($objectClassName = NULL) {
157 $removedObjects = array();
158 foreach ($this->removedObjects as $object) {
159 if ($objectClassName != NULL && !($object instanceof $objectClassName)) continue;
160 $removedObjects[] = $object;
161 }
162 return $removedObjects;
163 }
164
165 /**
166 * Returns TRUE if the given object is registered as removed
167 *
168 * @param TX_EXTMVC_DomainObject_AbstractDomainObject $object
169 * @return bool TRUE if the given object is registered as removed; otherwise FALSE
170 * @author Jochen Rau <jochen.rau@typoplanet.de>
171 */
172 public function isRemovedObject(TX_EXTMVC_DomainObject_AbstractDomainObject $object) {
173 return $this->removedObjects->contains($object);
174 }
175
176 /**
177 * Registers a reconstituted object
178 *
179 * @param object $object
180 * @return TX_EXTMVC_DomainObject_AbstractDomainObject
181 * @author Jochen Rau <jochen.rau@typoplanet.de>
182 */
183 public function registerReconstitutedObject(TX_EXTMVC_DomainObject_AbstractDomainObject $object) {
184 if ($this->addedObjects->contains($object)) throw new InvalidArgumentException('The object was registered as added and can therefore not be registered as reconstituted.');
185 $this->reconstitutedObjects->attach($object);
186 $object->_memorizeCleanState();
187 }
188
189 /**
190 * Unregisters a reconstituted object
191 *
192 * @param TX_EXTMVC_DomainObject_AbstractDomainObject $object
193 * @return void
194 * @author Jochen Rau <jochen.rau@typoplanet.de>
195 */
196 public function unregisterReconstitutedObject(TX_EXTMVC_DomainObject_AbstractDomainObject $object) {
197 $this->reconstitutedObjects->detach($object);
198 }
199
200 /**
201 * Returns all objects which have been registered as reconstituted objects
202 *
203 * @param string $objectClassName The class name of objects to be returned
204 * @return TX_EXTMVC_Persistence_ObjectStorage All reconstituted objects
205 * @author Jochen Rau <jochen.rau@typoplanet.de>
206 */
207 public function getReconstitutedObjects($objectClassName = NULL) {
208 $reconstitutedObjects = array();
209 foreach ($this->reconstitutedObjects as $object) {
210 if ($objectClassName != NULL && !($object instanceof $objectClassName)) continue;
211 $reconstitutedObjects[] = $object;
212 }
213 return $reconstitutedObjects;
214 }
215
216 /**
217 * Returns TRUE if the given object is registered as reconstituted
218 *
219 * @param TX_EXTMVC_DomainObject_AbstractDomainObject $object
220 * @return bool TRUE if the given object is registered as reconstituted; otherwise FALSE
221 * @author Jochen Rau <jochen.rau@typoplanet.de>
222 */
223 public function isReconstitutedObject(TX_EXTMVC_DomainObject_AbstractDomainObject $object) {
224 return $this->reconstitutedObjects->contains($object);
225 }
226
227 /**
228 * Returns all objects marked as dirty (changed after reconstitution)
229 *
230 * @param string $objectClassName The class name of objects to be returned
231 * @return array An array of dirty objects
232 * @author Jochen Rau <jochen.rau@typoplanet.de>
233 */
234 public function getDirtyObjects($objectClassName = NULL) {
235 $dirtyObjects = array();
236 foreach ($this->reconstitutedObjects as $object) {
237 if ($objectClassName != NULL && !($object instanceof $objectClassName)) continue;
238 if ($object->_isDirty()) {
239 $dirtyObjects[] = $object;
240 }
241 }
242 return $dirtyObjects;
243 }
244
245 /**
246 * Returns TRUE if the given object is dirty
247 *
248 * @param TX_EXTMVC_DomainObject_AbstractDomainObject $object
249 * @return bool TRUE if the given object is dirty; otherwise FALSE
250 * @author Jochen Rau <jochen.rau@typoplanet.de>
251 */
252 public function isDirtyObject(TX_EXTMVC_DomainObject_AbstractDomainObject $object) {
253 return $object->_isDirty();
254 }
255
256 /**
257 * Unregisters an object from all states
258 *
259 * @param TX_EXTMVC_DomainObject_AbstractDomainObject $object
260 * @return void
261 * @author Jochen Rau <jochen.rau@typoplanet.de>
262 */
263 public function unregisterObject(TX_EXTMVC_DomainObject_AbstractDomainObject $object) {
264 $this->unregisterAddedObject($object);
265 $this->unregisterRemovedObject($object);
266 $this->unregisterReconstitutedObject($object);
267 }
268
269 /**
270 * Clears all ObjectStorages
271 *
272 * @return void
273 * @author Jochen Rau <jochen.rau@typoplanet.de>
274 */
275 public function clear() {
276 $this->addedObjects->removeAll();
277 $this->removedObjects->removeAll();
278 $this->reconstitutedObjects->removeAll();
279 $this->aggregateRootClassNames = array();
280 }
281
282 /**
283 * Registers an aggregate root
284 *
285 * @param string $className The class to be registered
286 * @return void
287 * @author Jochen Rau <jochen.rau@typoplanet.de>
288 */
289 public function registerAggregateRootClassName($className) {
290 $this->aggregateRootClassNames[] = $className;
291 }
292
293 /**
294 * Returns all aggregate root classes
295 *
296 * @return array An array holding the registered aggregate root classes
297 * @author Jochen Rau <jochen.rau@typoplanet.de>
298 */
299 public function getAggregateRootClassNames() {
300 return $this->aggregateRootClassNames;
301 }
302
303 /**
304 * Commits the current persistence session.
305 *
306 * @return void
307 * @author Karsten Dambekalns <karsten@typo3.org>
308 */
309 public function commit() {
310 $dataMapper = t3lib_div::makeInstance('TX_EXTMVC_Persistence_Mapper_ObjectRelationalMapper'); // singleton;
311 $dataMapper->persistAll();
312 }
313
314 }
315 ?>