[TASK] Turn todos into @todo to find them easier
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / Generic / Session.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Persistence\Generic;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
17
18 /**
19 * The persistence session - acts as a Unit of Work for Extbase persistence framework.
20 */
21 class Session implements \TYPO3\CMS\Core\SingletonInterface {
22
23 /**
24 * Reconstituted objects
25 *
26 * @var ObjectStorage
27 */
28 protected $reconstitutedEntities;
29
30 /**
31 * Reconstituted entity data (effectively their clean state)
32 * Currently unused in Extbase
33 * @todo make use of it in Extbase
34 *
35 * @var array
36 */
37 protected $reconstitutedEntitiesData = array();
38
39 /**
40 * @var ObjectStorage
41 */
42 protected $objectMap;
43
44 /**
45 * @var array
46 */
47 protected $identifierMap = array();
48
49 /**
50 * @var \TYPO3\CMS\Extbase\Reflection\ReflectionService
51 * @inject
52 */
53 protected $reflectionService;
54
55 /**
56 * Constructs a new Session
57 */
58 public function __construct() {
59 $this->reconstitutedEntities = new ObjectStorage();
60 $this->objectMap = new ObjectStorage();
61 }
62
63 /**
64 * Registers data for a reconstituted object.
65 *
66 * $entityData format is described in
67 * "Documentation/PersistenceFramework object data format.txt"
68 *
69 * @param object $entity
70 * @param array $entityData
71 * @return void
72 */
73 public function registerReconstitutedEntity($entity, array $entityData = array()) {
74 $this->reconstitutedEntities->attach($entity);
75 $this->reconstitutedEntitiesData[$entityData['identifier']] = $entityData;
76 }
77
78 /**
79 * Replace a reconstituted object, leaves the clean data unchanged.
80 *
81 * @param object $oldEntity
82 * @param object $newEntity
83 * @return void
84 */
85 public function replaceReconstitutedEntity($oldEntity, $newEntity) {
86 $this->reconstitutedEntities->detach($oldEntity);
87 $this->reconstitutedEntities->attach($newEntity);
88 }
89
90 /**
91 * Unregisters data for a reconstituted object
92 *
93 * @param object $entity
94 * @return void
95 */
96 public function unregisterReconstitutedEntity($entity) {
97 if ($this->reconstitutedEntities->contains($entity)) {
98 $this->reconstitutedEntities->detach($entity);
99 unset($this->reconstitutedEntitiesData[$this->getIdentifierByObject($entity)]);
100 }
101 }
102
103 /**
104 * Returns all objects which have been registered as reconstituted
105 *
106 * @return ObjectStorage All reconstituted objects
107 */
108 public function getReconstitutedEntities() {
109 return $this->reconstitutedEntities;
110 }
111
112 /**
113 * Tells whether the given object is a reconstituted entity.
114 *
115 * @param object $entity
116 * @return bool
117 */
118 public function isReconstitutedEntity($entity) {
119 return $this->reconstitutedEntities->contains($entity);
120 }
121
122 // @todo implement the is dirty checking behaviour of the Flow persistence session here
123
124 /**
125 * Checks whether the given object is known to the identity map
126 *
127 * @param object $object
128 * @return bool
129 * @api
130 */
131 public function hasObject($object) {
132 return $this->objectMap->contains($object);
133 }
134
135 /**
136 * Checks whether the given identifier is known to the identity map
137 *
138 * @param string $identifier
139 * @param string $className
140 * @return bool
141 */
142 public function hasIdentifier($identifier, $className) {
143 return isset($this->identifierMap[strtolower($className)][$identifier]);
144 }
145
146 /**
147 * Returns the object for the given identifier
148 *
149 * @param string $identifier
150 * @param string $className
151 * @return object
152 * @api
153 */
154 public function getObjectByIdentifier($identifier, $className) {
155 return $this->identifierMap[strtolower($className)][$identifier];
156 }
157
158 /**
159 * Returns the identifier for the given object from
160 * the session, if the object was registered.
161 *
162 *
163 * @param object $object
164 * @return string
165 * @api
166 */
167 public function getIdentifierByObject($object) {
168 if ($this->hasObject($object)) {
169 return $this->objectMap[$object];
170 }
171 return NULL;
172 }
173
174 /**
175 * Register an identifier for an object
176 *
177 * @param object $object
178 * @param string $identifier
179 * @api
180 */
181 public function registerObject($object, $identifier) {
182 $this->objectMap[$object] = $identifier;
183 $this->identifierMap[strtolower(get_class($object))][$identifier] = $object;
184 }
185
186 /**
187 * Unregister an object
188 *
189 * @param object $object
190 * @return void
191 */
192 public function unregisterObject($object) {
193 unset($this->identifierMap[strtolower(get_class($object))][$this->objectMap[$object]]);
194 $this->objectMap->detach($object);
195 }
196
197 /**
198 * Destroy the state of the persistence session and reset
199 * all internal data.
200 *
201 * @return void
202 */
203 public function destroy() {
204 $this->identifierMap = array();
205 $this->objectMap = new ObjectStorage();
206 $this->reconstitutedEntities = new ObjectStorage();
207 $this->reconstitutedEntitiesData = array();
208 }
209
210 }