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