Passed
Push — master ( e47dd8...3ef27a )
by
unknown
13:30
created

Session::replaceReconstitutedEntity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace TYPO3\CMS\Extbase\Persistence\Generic;
17
18
use TYPO3\CMS\Core\SingletonInterface;
19
use TYPO3\CMS\Extbase\Object\Container\Container;
20
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
21
22
/**
23
 * The persistence session - acts as a Unit of Work for Extbase persistence framework.
24
 * @internal only to be used within Extbase, not part of TYPO3 Core API.
25
 */
26
class Session implements SingletonInterface
27
{
28
    /**
29
     * @var \TYPO3\CMS\Extbase\Object\Container\Container
30
     */
31
    protected $objectContainer;
32
33
    /**
34
     * Reconstituted objects
35
     *
36
     * @var ObjectStorage
37
     */
38
    protected $reconstitutedEntities;
39
40
    /**
41
     * @var ObjectStorage
42
     */
43
    protected $objectMap;
44
45
    /**
46
     * @var array
47
     */
48
    protected $identifierMap = [];
49
50
    /**
51
     * Constructs a new Session
52
     */
53
    public function __construct(Container $container)
54
    {
55
        $this->objectContainer = $container;
56
        $this->reconstitutedEntities = new ObjectStorage();
57
        $this->objectMap = new ObjectStorage();
58
    }
59
60
    /**
61
     * Registers data for a reconstituted object.
62
     *
63
     * $entityData format is described in
64
     * "Documentation/PersistenceFramework object data format.txt"
65
     *
66
     * @param object $entity
67
     */
68
    public function registerReconstitutedEntity($entity)
69
    {
70
        $this->reconstitutedEntities->attach($entity);
71
    }
72
73
    /**
74
     * Unregisters data for a reconstituted object
75
     *
76
     * @param object $entity
77
     */
78
    public function unregisterReconstitutedEntity($entity)
79
    {
80
        if ($this->reconstitutedEntities->contains($entity)) {
81
            $this->reconstitutedEntities->detach($entity);
82
        }
83
    }
84
85
    /**
86
     * Returns all objects which have been registered as reconstituted
87
     *
88
     * @return ObjectStorage All reconstituted objects
89
     */
90
    public function getReconstitutedEntities()
91
    {
92
        return $this->reconstitutedEntities;
93
    }
94
95
    // @todo implement the is dirty checking behaviour of the Flow persistence session here
96
97
    /**
98
     * Checks whether the given object is known to the identity map
99
     *
100
     * @param object $object
101
     * @return bool
102
     */
103
    public function hasObject($object)
104
    {
105
        return $this->objectMap->contains($object);
106
    }
107
108
    /**
109
     * Checks whether the given identifier is known to the identity map
110
     *
111
     * @param string $identifier
112
     * @param string $className
113
     * @return bool
114
     */
115
    public function hasIdentifier($identifier, $className)
116
    {
117
        return isset($this->identifierMap[$this->getClassIdentifier($className)][$identifier]);
118
    }
119
120
    /**
121
     * Returns the object for the given identifier
122
     *
123
     * @param string $identifier
124
     * @param string $className
125
     * @return object
126
     */
127
    public function getObjectByIdentifier($identifier, $className)
128
    {
129
        return $this->identifierMap[$this->getClassIdentifier($className)][$identifier];
130
    }
131
132
    /**
133
     * Returns the identifier for the given object from
134
     * the session, if the object was registered.
135
     *
136
     *
137
     * @param object $object
138
     * @return string
139
     */
140
    public function getIdentifierByObject($object)
141
    {
142
        if ($this->hasObject($object)) {
143
            return $this->objectMap[$object];
144
        }
145
        return null;
146
    }
147
148
    /**
149
     * Register an identifier for an object
150
     *
151
     * @param object $object
152
     * @param string $identifier
153
     */
154
    public function registerObject($object, $identifier)
155
    {
156
        $this->objectMap[$object] = $identifier;
157
        $this->identifierMap[$this->getClassIdentifier(get_class($object))][$identifier] = $object;
158
    }
159
160
    /**
161
     * Unregister an object
162
     *
163
     * @param object $object
164
     */
165
    public function unregisterObject($object)
166
    {
167
        unset($this->identifierMap[$this->getClassIdentifier(get_class($object))][$this->objectMap[$object]]);
168
        $this->objectMap->detach($object);
169
    }
170
171
    /**
172
     * Destroy the state of the persistence session and reset
173
     * all internal data.
174
     */
175
    public function destroy()
176
    {
177
        $this->identifierMap = [];
178
        $this->objectMap = new ObjectStorage();
179
        $this->reconstitutedEntities = new ObjectStorage();
180
    }
181
182
    /**
183
     * Objects are stored in the cache with their implementation class name
184
     * to allow reusing instances of different classes that point to the same implementation
185
     *
186
     * @param string $className
187
     * @return string a unique class identifier respecting configured implementation class names
188
     */
189
    protected function getClassIdentifier($className): string
190
    {
191
        return strtolower($this->objectContainer->getImplementationClassName($className));
192
    }
193
}
194