1b6a1e8fd9400db26b39023b8c377e46c0a36f32
[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 Extbase
31 * @subpackage Persistence
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_DataMapper.
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_DomainObjectInterface $object
82 * @return void
83 */
84 public function registerAddedObject(Tx_Extbase_DomainObject_DomainObjectInterface $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_DomainObjectInterface $object
94 * @return void
95 */
96 public function unregisterAddedObject(Tx_Extbase_DomainObject_DomainObjectInterface $object) {
97 $this->addedObjects->detach($object);
98 }
99
100 /**
101 * Returns all objects which have been registered as added objects
102 *
103 * @return array All added objects
104 */
105 public function getAddedObjects() {
106 return $this->addedObjects;
107 }
108
109 /**
110 * Returns TRUE if the given object is registered as added
111 *
112 * @param Tx_Extbase_DomainObject_DomainObjectInterface $object
113 * @return bool TRUE if the given object is registered as added; otherwise FALSE
114 */
115 public function isAddedObject(Tx_Extbase_DomainObject_DomainObjectInterface $object) {
116 return $this->addedObjects->contains($object);
117 }
118
119 /**
120 * Registers a removed object
121 *
122 * @param Tx_Extbase_DomainObject_DomainObjectInterface $object
123 * @return void
124 */
125 public function registerRemovedObject(Tx_Extbase_DomainObject_DomainObjectInterface $object) {
126 if ($this->addedObjects->contains($object)) {
127 $this->addedObjects->detach($object);
128 } else {
129 $this->removedObjects->attach($object);
130 }
131 }
132
133 /**
134 * Unregisters a removed object
135 *
136 * @param Tx_Extbase_DomainObject_DomainObjectInterface $object
137 * @return void
138 */
139 public function unregisterRemovedObject(Tx_Extbase_DomainObject_DomainObjectInterface $object) {
140 $this->removedObjects->detach($object);
141 }
142
143 /**
144 * Returns all objects which have been registered as removed objects
145 *
146 * @return array All removed objects
147 */
148 public function getRemovedObjects() {
149 return $this->removedObjects;
150 }
151
152 /**
153 * Returns TRUE if the given object is registered as removed
154 *
155 * @param Tx_Extbase_DomainObject_DomainObjectInterface $object
156 * @return bool TRUE if the given object is registered as removed; otherwise FALSE
157 */
158 public function isRemovedObject(Tx_Extbase_DomainObject_DomainObjectInterface $object) {
159 return $this->removedObjects->contains($object);
160 }
161
162 /**
163 * Registers all given objects as reconstituted
164 *
165 * @param array $objects
166 * @return void
167 */
168 public function registerReconstitutedObjects(array $objects) {
169 foreach ($objects as $object) {
170 $this->registerReconstitutedObject($object);
171 }
172 }
173
174 /**
175 * Registers a reconstituted object
176 *
177 * @param object $object
178 * @return void
179 */
180 public function registerReconstitutedObject(Tx_Extbase_DomainObject_DomainObjectInterface $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 }
184
185 /**
186 * Unregisters a reconstituted object
187 *
188 * @param Tx_Extbase_DomainObject_DomainObjectInterface $object
189 * @return void
190 */
191 public function unregisterReconstitutedObject(Tx_Extbase_DomainObject_DomainObjectInterface $object) {
192 $this->reconstitutedObjects->detach($object);
193 }
194
195 /**
196 * Returns all objects which have been registered as reconstituted objects
197 *
198 * @param string $objectClassName The class name of objects to be returned
199 * @return array All reconstituted objects
200 */
201 public function getReconstitutedObjects() {
202 return $this->reconstitutedObjects;
203 }
204
205 /**
206 * Returns TRUE if the given object is registered as reconstituted
207 *
208 * @param Tx_Extbase_DomainObject_DomainObjectInterface $object
209 * @return bool TRUE if the given object is registered as reconstituted; otherwise FALSE
210 */
211 public function isReconstitutedObject(Tx_Extbase_DomainObject_DomainObjectInterface $object) {
212 return $this->reconstitutedObjects->contains($object);
213 }
214
215 /**
216 * Returns all objects marked as dirty (changed after reconstitution)
217 *
218 * @return array An array of dirty objects
219 */
220 public function getDirtyObjects() {
221 $dirtyObjects = new Tx_Extbase_Persistence_ObjectStorage();
222 foreach ($this->reconstitutedObjects as $object) {
223 if ($object->_isDirty()) {
224 $dirtyObjects->attach($object);
225 }
226 }
227 return $dirtyObjects;
228 }
229
230 /**
231 * Returns TRUE if the given object is dirty
232 *
233 * @param Tx_Extbase_DomainObject_DomainObjectInterface $object
234 * @return bool TRUE if the given object is dirty; otherwise FALSE
235 */
236 public function isDirtyObject(Tx_Extbase_DomainObject_DomainObjectInterface $object) {
237 return $object->_isDirty();
238 }
239
240 /**
241 * Unregisters an object from all states
242 *
243 * @param Tx_Extbase_DomainObject_DomainObjectInterface $object
244 * @return void
245 */
246 public function unregisterObject(Tx_Extbase_DomainObject_DomainObjectInterface $object) {
247 $this->unregisterAddedObject($object);
248 $this->unregisterRemovedObject($object);
249 $this->unregisterReconstitutedObject($object);
250 }
251
252 /**
253 * Clears all ObjectStorages
254 *
255 * @return void
256 */
257 public function clear() {
258 $this->addedObjects = new Tx_Extbase_Persistence_ObjectStorage();
259 $this->removedObjects = new Tx_Extbase_Persistence_ObjectStorage();
260 $this->reconstitutedObjects = new Tx_Extbase_Persistence_ObjectStorage();
261 $this->aggregateRootClassNames = array();
262 }
263
264 /**
265 * Registers an aggregate root
266 *
267 * @param string $className The class to be registered
268 * @return void
269 */
270 public function registerAggregateRootClassName($className) {
271 $this->aggregateRootClassNames[] = $className;
272 }
273
274 /**
275 * Returns all aggregate root classes
276 *
277 * @return array An array holding the registered aggregate root classes
278 */
279 public function getAggregateRootClassNames() {
280 return $this->aggregateRootClassNames;
281 }
282
283 }
284 ?>