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