|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Igni\Storage; |
|
4
|
|
|
|
|
5
|
|
|
use Igni\Storage\Exception\UnitOfWorkException; |
|
6
|
|
|
use SplObjectStorage; |
|
7
|
|
|
|
|
8
|
|
|
class Storage implements UnitOfWork, RepositoryContainer |
|
9
|
|
|
{ |
|
10
|
|
|
private const STATE_NEW = 1; |
|
11
|
|
|
private const STATE_MANAGED = 2; |
|
12
|
|
|
private const STATE_REMOVED = 3; |
|
13
|
|
|
private const STATE_DETACHED = 4; |
|
14
|
|
|
private const ACTION_CREATE = 'create'; |
|
15
|
|
|
private const ACTION_REMOVE = 'remove'; |
|
16
|
|
|
private const ACTION_UPDATE = 'update'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Contains calculated states for entities. |
|
20
|
|
|
* @var array<string, int> |
|
21
|
|
|
*/ |
|
22
|
|
|
private $states = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Contains grouped by class name entities that should be saved. |
|
26
|
|
|
* @var SplObjectStorage[] |
|
27
|
|
|
*/ |
|
28
|
|
|
private $create = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Contains grouped by class name entities that should be removed. |
|
32
|
|
|
* @var SplObjectStorage[] |
|
33
|
|
|
*/ |
|
34
|
|
|
private $remove = []; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Contains grouped by class name entities that should be removed. |
|
38
|
|
|
* @var SplObjectStorage[] |
|
39
|
|
|
*/ |
|
40
|
|
|
private $update = []; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var Manager |
|
44
|
|
|
*/ |
|
45
|
|
|
private $entityManager; |
|
46
|
|
|
|
|
47
|
25 |
|
public function __construct(Manager $manager = null) |
|
48
|
|
|
{ |
|
49
|
25 |
|
$this->entityManager = $manager ?? new Manager(); |
|
50
|
25 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function getRepository(string $entity): Repository |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->entityManager->getRepository($entity); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function hasRepository(string $entity): bool |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->entityManager->hasRepository($entity); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function addRepository(Repository ...$repositories): void |
|
63
|
|
|
{ |
|
64
|
|
|
$this->entityManager->addRepository(...$repositories); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function getEntityManager(): Manager |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->entityManager; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
4 |
|
public function get(string $entity, $id): Entity |
|
73
|
|
|
{ |
|
74
|
4 |
|
$entity = $this->entityManager->get($entity, $id); |
|
75
|
4 |
|
$this->states[spl_object_hash($entity)] = self::STATE_MANAGED; |
|
76
|
|
|
|
|
77
|
4 |
|
return $entity; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param Entity[] ...$entities |
|
82
|
|
|
*/ |
|
83
|
2 |
|
public function persist(Entity ...$entities): void |
|
84
|
|
|
{ |
|
85
|
2 |
|
foreach ($entities as $entity) { |
|
86
|
2 |
|
$this->persistOne($entity); |
|
87
|
|
|
} |
|
88
|
2 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param Entity[] ...$entities |
|
92
|
|
|
*/ |
|
93
|
2 |
|
public function remove(Entity ...$entities): void |
|
94
|
|
|
{ |
|
95
|
2 |
|
foreach ($entities as $entity) { |
|
96
|
2 |
|
$this->removeOne($entity); |
|
97
|
|
|
} |
|
98
|
2 |
|
} |
|
99
|
|
|
|
|
100
|
2 |
|
public function commit(): void |
|
101
|
|
|
{ |
|
102
|
2 |
|
$this->commitAction(self::ACTION_CREATE); |
|
103
|
2 |
|
$this->commitAction(self::ACTION_UPDATE); |
|
104
|
2 |
|
$this->commitAction(self::ACTION_REMOVE); |
|
105
|
2 |
|
} |
|
106
|
|
|
|
|
107
|
2 |
|
private function commitAction(string $action): void |
|
108
|
|
|
{ |
|
109
|
2 |
|
foreach ($this->{$action} as $namespace => $entities) { |
|
110
|
2 |
|
foreach ($entities as $entity) { |
|
111
|
2 |
|
call_user_func([$this->entityManager->getRepository($namespace), $action], $entity); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
2 |
|
} |
|
115
|
|
|
|
|
116
|
1 |
|
public function rollback(): void |
|
117
|
|
|
{ |
|
118
|
1 |
|
$this->create = []; |
|
119
|
1 |
|
$this->update = []; |
|
120
|
1 |
|
$this->remove = []; |
|
121
|
1 |
|
} |
|
122
|
|
|
|
|
123
|
2 |
|
private function persistOne(Entity $entity): void |
|
124
|
|
|
{ |
|
125
|
2 |
|
$namespace = get_class($entity); |
|
126
|
|
|
|
|
127
|
2 |
|
switch ($this->getState($entity)) { |
|
128
|
2 |
|
case self::STATE_MANAGED: |
|
129
|
2 |
|
if (!isset($this->update[$namespace])) { |
|
130
|
2 |
|
$this->update[$namespace] = new SplObjectStorage(); |
|
131
|
|
|
} |
|
132
|
2 |
|
$this->update[$namespace]->attach($entity); |
|
133
|
2 |
|
break; |
|
134
|
|
|
case self::STATE_NEW: |
|
135
|
|
|
if (!isset($this->create[$namespace])) { |
|
136
|
|
|
$this->create[$namespace] = new SplObjectStorage(); |
|
137
|
|
|
} |
|
138
|
|
|
$this->create[$namespace]->attach($entity); |
|
139
|
|
|
break; |
|
140
|
|
|
case self::STATE_REMOVED: |
|
141
|
|
|
case self::STATE_DETACHED: |
|
142
|
|
|
default: |
|
143
|
|
|
throw UnitOfWorkException::forPersistingEntityInInvalidState($entity); |
|
144
|
|
|
} |
|
145
|
2 |
|
} |
|
146
|
|
|
|
|
147
|
2 |
|
public function getState(Entity $entity): int |
|
148
|
|
|
{ |
|
149
|
2 |
|
$oid = spl_object_hash($entity); |
|
150
|
2 |
|
if (isset($this->states[$oid])) { |
|
151
|
2 |
|
return $this->states[$oid]; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$namespace = get_class($entity); |
|
155
|
|
|
|
|
156
|
|
|
if (isset($this->remove[$namespace]) && $this->remove[$namespace]->contains($entity)) { |
|
157
|
|
|
return $this->states[$oid] = self::STATE_REMOVED; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
if ($this->entityManager->contains($entity)) { |
|
161
|
|
|
return $this->states[$oid] = self::STATE_MANAGED; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
try { |
|
165
|
|
|
$this->entityManager->getRepository($namespace)->get($entity->getId()); |
|
166
|
|
|
return $this->states[$oid] = self::STATE_DETACHED; |
|
167
|
|
|
} catch (\Exception $exception) { |
|
168
|
|
|
return $this->states[$oid] = self::STATE_NEW; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
2 |
|
private function removeOne(Entity $entity): void |
|
173
|
|
|
{ |
|
174
|
2 |
|
$namespace = get_class($entity); |
|
175
|
2 |
|
$oid = spl_object_hash($entity); |
|
176
|
|
|
|
|
177
|
2 |
|
if (isset($this->states[$oid]) && $this->states[$oid] === self::STATE_MANAGED) { |
|
178
|
2 |
|
$this->states[$oid] = self::STATE_REMOVED; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
2 |
|
if (!isset($this->remove[$namespace])) { |
|
182
|
2 |
|
$this->remove[$namespace] = new SplObjectStorage(); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
2 |
|
if (isset($this->create[$namespace])) { |
|
186
|
|
|
$this->create[$namespace]->detach($entity); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
2 |
|
if (isset($this->update[$namespace])) { |
|
190
|
1 |
|
$this->update[$namespace]->detach($entity); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
2 |
|
$this->remove[$namespace]->attach($entity); |
|
194
|
2 |
|
} |
|
195
|
|
|
|
|
196
|
|
|
public function attach(Entity ...$entities): void |
|
197
|
|
|
{ |
|
198
|
|
|
foreach ($entities as $entity) { |
|
199
|
|
|
$this->entityManager->attach($entity); |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
public function contains(Entity $entity): bool |
|
204
|
|
|
{ |
|
205
|
|
|
$contains = $this->entityManager->contains($entity); |
|
206
|
|
|
$oid = spl_object_hash($entity); |
|
207
|
|
|
$this->states[$oid] = self::STATE_MANAGED; |
|
208
|
|
|
|
|
209
|
|
|
return $contains; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
public function detach(Entity ...$entities): void |
|
213
|
|
|
{ |
|
214
|
|
|
foreach ($entities as $entity) { |
|
215
|
|
|
$this->detachOne($entity); |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
private function detachOne(Entity $entity): void |
|
220
|
|
|
{ |
|
221
|
|
|
$this->states[spl_object_hash($entity)] = self::STATE_DETACHED; |
|
222
|
|
|
$namespace = get_class($entity); |
|
223
|
|
|
|
|
224
|
|
|
if (isset($this->update[$namespace])) { |
|
225
|
|
|
$this->update[$namespace]->detach($entity); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
if (isset($this->create[$namespace])) { |
|
229
|
|
|
$this->create[$namespace]->detach($entity); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
if (isset($this->remove[$namespace])) { |
|
233
|
|
|
$this->remove[$namespace]->detach($entity); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
$this->entityManager->detach($entity); |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
|