43e4467c33d2780b22840b4a781cd5455796b34d
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / Generic / Session.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Persistence\Generic;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2013 Extbase Team (http://forge.typo3.org/projects/typo3v4-mvc)
8 * Extbase is a backport of TYPO3 Flow. All credits go to the TYPO3 Flow team.
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the textfile GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30 /**
31 * The persistence session - acts as a Unit of Work for Extbase persistence framework.
32 */
33 class Session implements \TYPO3\CMS\Core\SingletonInterface {
34
35 /**
36 * Reconstituted objects
37 *
38 * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage
39 */
40 protected $reconstitutedEntities;
41
42 /**
43 * Reconstituted entity data (effectively their clean state)
44 * Currently unused in Extbase
45 * TODO make use of it in Extbase
46 *
47 * @var array
48 */
49 protected $reconstitutedEntitiesData = array();
50
51 /**
52 * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage
53 */
54 protected $objectMap;
55
56 /**
57 * @var array
58 */
59 protected $identifierMap = array();
60
61 /**
62 * @var \TYPO3\CMS\Extbase\Reflection\ReflectionService
63 */
64 protected $reflectionService;
65
66 /**
67 * Constructs a new Session
68 */
69 public function __construct() {
70 $this->reconstitutedEntities = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
71 $this->objectMap = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
72 }
73
74 /**
75 * Injects a Reflection Service instance
76 *
77 * @param \TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService
78 * @return void
79 */
80 public function injectReflectionService(\TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService) {
81 $this->reflectionService = $reflectionService;
82 }
83
84 /**
85 * Registers data for a reconstituted object.
86 *
87 * $entityData format is described in
88 * "Documentation/PersistenceFramework object data format.txt"
89 *
90 * @param object $entity
91 * @param array $entityData
92 * @return void
93 */
94 public function registerReconstitutedEntity($entity, array $entityData = array()) {
95 $this->reconstitutedEntities->attach($entity);
96 $this->reconstitutedEntitiesData[$entityData['identifier']] = $entityData;
97 }
98
99 /**
100 * Replace a reconstituted object, leaves the clean data unchanged.
101 *
102 * @param object $oldEntity
103 * @param object $newEntity
104 * @return void
105 */
106 public function replaceReconstitutedEntity($oldEntity, $newEntity) {
107 $this->reconstitutedEntities->detach($oldEntity);
108 $this->reconstitutedEntities->attach($newEntity);
109 }
110
111 /**
112 * Unregisters data for a reconstituted object
113 *
114 * @param object $entity
115 * @return void
116 */
117 public function unregisterReconstitutedEntity($entity) {
118 if ($this->reconstitutedEntities->contains($entity)) {
119 $this->reconstitutedEntities->detach($entity);
120 unset($this->reconstitutedEntitiesData[$this->getIdentifierByObject($entity)]);
121 }
122 }
123
124 /**
125 * Returns all objects which have been registered as reconstituted
126 *
127 * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage All reconstituted objects
128 */
129 public function getReconstitutedEntities() {
130 return $this->reconstitutedEntities;
131 }
132
133 /**
134 * Tells whether the given object is a reconstituted entity.
135 *
136 * @param object $entity
137 * @return boolean
138 */
139 public function isReconstitutedEntity($entity) {
140 return $this->reconstitutedEntities->contains($entity);
141 }
142
143 // TODO implement the is dirty checking behaviour of the Flow persistence session here
144
145 /**
146 * Checks whether the given object is known to the identity map
147 *
148 * @param object $object
149 * @return boolean
150 * @api
151 */
152 public function hasObject($object) {
153 return $this->objectMap->contains($object);
154 }
155
156 /**
157 * Checks whether the given identifier is known to the identity map
158 *
159 * @param string $identifier
160 * @param string $className
161 * @return boolean
162 */
163 public function hasIdentifier($identifier, $className) {
164 return isset($this->identifierMap[strtolower($className)][$identifier]);
165 }
166
167 /**
168 * Returns the object for the given identifier
169 *
170 * @param string $identifier
171 * @param string $className
172 * @return object
173 * @api
174 */
175 public function getObjectByIdentifier($identifier, $className) {
176 return $this->identifierMap[strtolower($className)][$identifier];
177 }
178
179 /**
180 * Returns the identifier for the given object from
181 * the session, if the object was registered.
182 *
183 *
184 * @param object $object
185 * @return string
186 * @api
187 */
188 public function getIdentifierByObject($object) {
189 if ($this->hasObject($object)) {
190 return $this->objectMap[$object];
191 }
192 return NULL;
193 }
194
195 /**
196 * Register an identifier for an object
197 *
198 * @param object $object
199 * @param string $identifier
200 * @api
201 */
202 public function registerObject($object, $identifier) {
203 $this->objectMap[$object] = $identifier;
204 $this->identifierMap[strtolower(get_class($object))][$identifier] = $object;
205 }
206
207 /**
208 * Unregister an object
209 *
210 * @param string $object
211 * @return void
212 */
213 public function unregisterObject($object) {
214 unset($this->identifierMap[strtolower(get_class($object))][$this->objectMap[$object]]);
215 $this->objectMap->detach($object);
216 }
217
218 /**
219 * Destroy the state of the persistence session and reset
220 * all internal data.
221 *
222 * @return void
223 */
224 public function destroy() {
225 $this->identifierMap = array();
226 $this->objectMap = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
227 $this->reconstitutedEntities = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
228 $this->reconstitutedEntitiesData = array();
229 }
230
231 }
232
233 ?>