01661de12807db9d50ae729f9884ee5509b58d61
[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 // TODO Implement against SessionInterface
35 // SK: Why should we need to implement this against an interface?
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 * Objects which were reconstituted
53 *
54 * @var TX_EXTMVC_Persistence_ObjectStorage
55 */
56 protected $reconstitutedObjects;
57
58 // SK: Add comment here please :-) As I do not know what this means or when it is set
59 /**
60 * @var array
61 */
62 protected $aggregateRootClassNames = array();
63
64 /**
65 * Constructs a new Session
66 *
67 */
68 public function __construct() {
69 $this->addedObjects = new TX_EXTMVC_Persistence_ObjectStorage();
70 $this->removedObjects = new TX_EXTMVC_Persistence_ObjectStorage();
71 $this->reconstitutedObjects = new TX_EXTMVC_Persistence_ObjectStorage();
72 }
73
74 /**
75 * Registers an added object
76 *
77 * @param TX_EXTMVC_DomainObject_AbstractDomainObject $object
78 * @return void
79 */
80 // SK: When is this method called?
81 public function registerAddedObject(TX_EXTMVC_DomainObject_AbstractDomainObject $object) {
82 if ($this->reconstitutedObjects->contains($object)) throw new InvalidArgumentException('The object was registered as reconstituted and can therefore not be registered as added.');
83 $this->removedObjects->detach($object);
84 $this->addedObjects->attach($object);
85 }
86
87 /**
88 * Unregisters an added object
89 *
90 * @param TX_EXTMVC_DomainObject_AbstractDomainObject $object
91 * @return void
92 */
93 // SK: When is this method called?
94 public function unregisterAddedObject(TX_EXTMVC_DomainObject_AbstractDomainObject $object) {
95 $this->addedObjects->detach($object);
96 }
97
98 /**
99 * Returns all objects which have been registered as added objects
100 *
101 * @param string $objectClassName The class name of objects to be returned
102 * @return TX_EXTMVC_Persistence_ObjectStorage All added objects
103 */
104 // SK: Wrong PHPDoc. Returns a normal array, and no ObjectStorage
105 public function getAddedObjects($objectClassName = NULL) {
106 $addedObjects = array();
107 foreach ($this->addedObjects as $object) {
108 if ($objectClassName != NULL && !($object instanceof $objectClassName)) continue;
109 $addedObjects[] = $object;
110 }
111 return $addedObjects;
112 }
113
114 /**
115 * Returns TRUE if the given object is registered as added
116 *
117 * @param TX_EXTMVC_DomainObject_AbstractDomainObject $object
118 * @return bool TRUE if the given object is registered as added; otherwise FALSE
119 */
120 public function isAddedObject(TX_EXTMVC_DomainObject_AbstractDomainObject $object) {
121 return $this->addedObjects->contains($object);
122 }
123
124 /**
125 * Registers a removed object
126 *
127 * @param TX_EXTMVC_DomainObject_AbstractDomainObject $object
128 * @return void
129 */
130 // SK: when is this called?
131 public function registerRemovedObject(TX_EXTMVC_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_EXTMVC_DomainObject_AbstractDomainObject $object
143 * @return void
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 */
155 // SK: Wrong PHPDoc. Returns a normal array, and no ObjectStorage
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 TX_EXTMVC_Persistence_ObjectStorage All reconstituted objects
202 */
203 // SK: Wrong PHPDoc. Returns a normal array, and no ObjectStorage
204 public function getReconstitutedObjects($objectClassName = NULL) {
205 $reconstitutedObjects = array();
206 foreach ($this->reconstitutedObjects as $object) {
207 if ($objectClassName != NULL && !($object instanceof $objectClassName)) continue;
208 $reconstitutedObjects[] = $object;
209 }
210 return $reconstitutedObjects;
211 }
212
213 /**
214 * Returns TRUE if the given object is registered as reconstituted
215 *
216 * @param TX_EXTMVC_DomainObject_AbstractDomainObject $object
217 * @return bool TRUE if the given object is registered as reconstituted; otherwise FALSE
218 */
219 public function isReconstitutedObject(TX_EXTMVC_DomainObject_AbstractDomainObject $object) {
220 return $this->reconstitutedObjects->contains($object);
221 }
222
223 /**
224 * Returns all objects marked as dirty (changed after reconstitution)
225 *
226 * @param string $objectClassName The class name of objects to be returned
227 * @return array An array of dirty objects
228 */
229 public function getDirtyObjects($objectClassName = NULL) {
230 $dirtyObjects = array();
231 foreach ($this->reconstitutedObjects as $object) {
232 if ($objectClassName != NULL && !($object instanceof $objectClassName)) continue;
233 if ($object->_isDirty()) {
234 $dirtyObjects[] = $object;
235 }
236 }
237 return $dirtyObjects;
238 }
239
240 /**
241 * Returns TRUE if the given object is dirty
242 *
243 * @param TX_EXTMVC_DomainObject_AbstractDomainObject $object
244 * @return bool TRUE if the given object is dirty; otherwise FALSE
245 */
246 public function isDirtyObject(TX_EXTMVC_DomainObject_AbstractDomainObject $object) {
247 return $object->_isDirty();
248 }
249
250 /**
251 * Unregisters an object from all states
252 *
253 * @param TX_EXTMVC_DomainObject_AbstractDomainObject $object
254 * @return void
255 */
256 public function unregisterObject(TX_EXTMVC_DomainObject_AbstractDomainObject $object) {
257 $this->unregisterAddedObject($object);
258 $this->unregisterRemovedObject($object);
259 $this->unregisterReconstitutedObject($object);
260 }
261
262 /**
263 * Clears all ObjectStorages
264 *
265 * @return void
266 */
267 public function clear() {
268 $this->addedObjects->removeAll();
269 $this->removedObjects->removeAll();
270 $this->reconstitutedObjects->removeAll();
271 $this->aggregateRootClassNames = array();
272 }
273
274 /**
275 * Registers an aggregate root
276 *
277 * @param string $className The class to be registered
278 * @return void
279 */
280 public function registerAggregateRootClassName($className) {
281 $this->aggregateRootClassNames[] = $className;
282 }
283
284 /**
285 * Returns all aggregate root classes
286 *
287 * @return array An array holding the registered aggregate root classes
288 */
289 public function getAggregateRootClassNames() {
290 return $this->aggregateRootClassNames;
291 }
292
293 /**
294 * Commits the current persistence session.
295 *
296 * @return void
297 */
298 public function commit() {
299 $dataMapper = t3lib_div::makeInstance('TX_EXTMVC_Persistence_Mapper_ObjectRelationalMapper'); // singleton;
300 $dataMapper->persistAll();
301 }
302
303 }
304 ?>