421bef1d9514ad38376a10ef5acec36a19c761b9
[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
17 use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
18
19 /**
20 * The persistence session - acts as a Unit of Work for Extbase persistence framework.
21 * @internal only to be used within Extbase, not part of TYPO3 Core API.
22 */
23 class Session implements \TYPO3\CMS\Core\SingletonInterface
24 {
25 /**
26 * Reconstituted objects
27 *
28 * @var ObjectStorage
29 */
30 protected $reconstitutedEntities;
31
32 /**
33 * @var ObjectStorage
34 */
35 protected $objectMap;
36
37 /**
38 * @var array
39 */
40 protected $identifierMap = [];
41
42 /**
43 * Constructs a new Session
44 */
45 public function __construct()
46 {
47 $this->reconstitutedEntities = new ObjectStorage();
48 $this->objectMap = new ObjectStorage();
49 }
50
51 /**
52 * Registers data for a reconstituted object.
53 *
54 * $entityData format is described in
55 * "Documentation/PersistenceFramework object data format.txt"
56 *
57 * @param object $entity
58 */
59 public function registerReconstitutedEntity($entity)
60 {
61 $this->reconstitutedEntities->attach($entity);
62 }
63
64 /**
65 * Replace a reconstituted object, leaves the clean data unchanged.
66 *
67 * @param object $oldEntity
68 * @param object $newEntity
69 */
70 public function replaceReconstitutedEntity($oldEntity, $newEntity)
71 {
72 $this->reconstitutedEntities->detach($oldEntity);
73 $this->reconstitutedEntities->attach($newEntity);
74 }
75
76 /**
77 * Unregisters data for a reconstituted object
78 *
79 * @param object $entity
80 */
81 public function unregisterReconstitutedEntity($entity)
82 {
83 if ($this->reconstitutedEntities->contains($entity)) {
84 $this->reconstitutedEntities->detach($entity);
85 }
86 }
87
88 /**
89 * Returns all objects which have been registered as reconstituted
90 *
91 * @return ObjectStorage All reconstituted objects
92 */
93 public function getReconstitutedEntities()
94 {
95 return $this->reconstitutedEntities;
96 }
97
98 /**
99 * Tells whether the given object is a reconstituted entity.
100 *
101 * @param object $entity
102 * @return bool
103 */
104 public function isReconstitutedEntity($entity)
105 {
106 return $this->reconstitutedEntities->contains($entity);
107 }
108
109 // @todo implement the is dirty checking behaviour of the Flow persistence session here
110
111 /**
112 * Checks whether the given object is known to the identity map
113 *
114 * @param object $object
115 * @return bool
116 */
117 public function hasObject($object)
118 {
119 return $this->objectMap->contains($object);
120 }
121
122 /**
123 * Checks whether the given identifier is known to the identity map
124 *
125 * @param string $identifier
126 * @param string $className
127 * @return bool
128 */
129 public function hasIdentifier($identifier, $className)
130 {
131 return isset($this->identifierMap[strtolower($className)][$identifier]);
132 }
133
134 /**
135 * Returns the object for the given identifier
136 *
137 * @param string $identifier
138 * @param string $className
139 * @return object
140 */
141 public function getObjectByIdentifier($identifier, $className)
142 {
143 return $this->identifierMap[strtolower($className)][$identifier];
144 }
145
146 /**
147 * Returns the identifier for the given object from
148 * the session, if the object was registered.
149 *
150 *
151 * @param object $object
152 * @return string
153 */
154 public function getIdentifierByObject($object)
155 {
156 if ($this->hasObject($object)) {
157 return $this->objectMap[$object];
158 }
159 return null;
160 }
161
162 /**
163 * Register an identifier for an object
164 *
165 * @param object $object
166 * @param string $identifier
167 */
168 public function registerObject($object, $identifier)
169 {
170 $this->objectMap[$object] = $identifier;
171 $this->identifierMap[strtolower(get_class($object))][$identifier] = $object;
172 }
173
174 /**
175 * Unregister an object
176 *
177 * @param object $object
178 */
179 public function unregisterObject($object)
180 {
181 unset($this->identifierMap[strtolower(get_class($object))][$this->objectMap[$object]]);
182 $this->objectMap->detach($object);
183 }
184
185 /**
186 * Destroy the state of the persistence session and reset
187 * all internal data.
188 */
189 public function destroy()
190 {
191 $this->identifierMap = [];
192 $this->objectMap = new ObjectStorage();
193 $this->reconstitutedEntities = new ObjectStorage();
194 }
195 }