[TASK] Replace inject methods with @inject
[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 * @inject
64 */
65 protected $reflectionService;
66
67 /**
68 * Constructs a new Session
69 */
70 public function __construct() {
71 $this->reconstitutedEntities = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
72 $this->objectMap = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
73 }
74
75 /**
76 * Registers data for a reconstituted object.
77 *
78 * $entityData format is described in
79 * "Documentation/PersistenceFramework object data format.txt"
80 *
81 * @param object $entity
82 * @param array $entityData
83 * @return void
84 */
85 public function registerReconstitutedEntity($entity, array $entityData = array()) {
86 $this->reconstitutedEntities->attach($entity);
87 $this->reconstitutedEntitiesData[$entityData['identifier']] = $entityData;
88 }
89
90 /**
91 * Replace a reconstituted object, leaves the clean data unchanged.
92 *
93 * @param object $oldEntity
94 * @param object $newEntity
95 * @return void
96 */
97 public function replaceReconstitutedEntity($oldEntity, $newEntity) {
98 $this->reconstitutedEntities->detach($oldEntity);
99 $this->reconstitutedEntities->attach($newEntity);
100 }
101
102 /**
103 * Unregisters data for a reconstituted object
104 *
105 * @param object $entity
106 * @return void
107 */
108 public function unregisterReconstitutedEntity($entity) {
109 if ($this->reconstitutedEntities->contains($entity)) {
110 $this->reconstitutedEntities->detach($entity);
111 unset($this->reconstitutedEntitiesData[$this->getIdentifierByObject($entity)]);
112 }
113 }
114
115 /**
116 * Returns all objects which have been registered as reconstituted
117 *
118 * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage All reconstituted objects
119 */
120 public function getReconstitutedEntities() {
121 return $this->reconstitutedEntities;
122 }
123
124 /**
125 * Tells whether the given object is a reconstituted entity.
126 *
127 * @param object $entity
128 * @return boolean
129 */
130 public function isReconstitutedEntity($entity) {
131 return $this->reconstitutedEntities->contains($entity);
132 }
133
134 // TODO implement the is dirty checking behaviour of the Flow persistence session here
135
136 /**
137 * Checks whether the given object is known to the identity map
138 *
139 * @param object $object
140 * @return boolean
141 * @api
142 */
143 public function hasObject($object) {
144 return $this->objectMap->contains($object);
145 }
146
147 /**
148 * Checks whether the given identifier is known to the identity map
149 *
150 * @param string $identifier
151 * @param string $className
152 * @return boolean
153 */
154 public function hasIdentifier($identifier, $className) {
155 return isset($this->identifierMap[strtolower($className)][$identifier]);
156 }
157
158 /**
159 * Returns the object for the given identifier
160 *
161 * @param string $identifier
162 * @param string $className
163 * @return object
164 * @api
165 */
166 public function getObjectByIdentifier($identifier, $className) {
167 return $this->identifierMap[strtolower($className)][$identifier];
168 }
169
170 /**
171 * Returns the identifier for the given object from
172 * the session, if the object was registered.
173 *
174 *
175 * @param object $object
176 * @return string
177 * @api
178 */
179 public function getIdentifierByObject($object) {
180 if ($this->hasObject($object)) {
181 return $this->objectMap[$object];
182 }
183 return NULL;
184 }
185
186 /**
187 * Register an identifier for an object
188 *
189 * @param object $object
190 * @param string $identifier
191 * @api
192 */
193 public function registerObject($object, $identifier) {
194 $this->objectMap[$object] = $identifier;
195 $this->identifierMap[strtolower(get_class($object))][$identifier] = $object;
196 }
197
198 /**
199 * Unregister an object
200 *
201 * @param string $object
202 * @return void
203 */
204 public function unregisterObject($object) {
205 unset($this->identifierMap[strtolower(get_class($object))][$this->objectMap[$object]]);
206 $this->objectMap->detach($object);
207 }
208
209 /**
210 * Destroy the state of the persistence session and reset
211 * all internal data.
212 *
213 * @return void
214 */
215 public function destroy() {
216 $this->identifierMap = array();
217 $this->objectMap = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
218 $this->reconstitutedEntities = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
219 $this->reconstitutedEntitiesData = array();
220 }
221
222 }
223
224 ?>