|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Igni\Storage; |
|
4
|
|
|
|
|
5
|
|
|
use Cache\Adapter\PHPArray\ArrayCachePool; |
|
6
|
|
|
use Igni\Storage\Exception\HydratorException; |
|
7
|
|
|
use Igni\Storage\Exception\RepositoryException; |
|
8
|
|
|
use Igni\Storage\Hydration\HydratorAutoGenerate; |
|
9
|
|
|
use Igni\Storage\Hydration\HydratorFactory; |
|
10
|
|
|
use Igni\Storage\Hydration\ObjectHydrator; |
|
11
|
|
|
use Igni\Storage\Mapping\IdentityMap; |
|
12
|
|
|
use Igni\Storage\Mapping\MetaData\EntityMetaData; |
|
13
|
|
|
use Igni\Storage\Mapping\MetaData\MetaDataFactory; |
|
14
|
|
|
use Igni\Storage\Mapping\MetaData\Strategy\AnnotationMetaDataFactory; |
|
15
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
16
|
|
|
|
|
17
|
|
|
class Manager implements IdentityMap, RepositoryContainer, MetaDataFactory |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var Entity[] */ |
|
20
|
|
|
private $registry = []; |
|
21
|
|
|
|
|
22
|
|
|
/** @var Repository[] */ |
|
23
|
|
|
private $repositories = []; |
|
24
|
|
|
|
|
25
|
|
|
/** @var HydratorFactory */ |
|
26
|
|
|
private $hydratorFactory; |
|
27
|
|
|
|
|
28
|
|
|
/** @var ObjectHydrator[] */ |
|
29
|
|
|
private $hydrators = []; |
|
30
|
|
|
|
|
31
|
|
|
/** @var MetaDataFactory */ |
|
32
|
|
|
private $metaDataFactory; |
|
33
|
|
|
|
|
34
|
|
|
/** @var string */ |
|
35
|
|
|
private $hydratorDir; |
|
36
|
|
|
|
|
37
|
|
|
/** @var string */ |
|
38
|
|
|
private $hydratorNamespace; |
|
39
|
|
|
|
|
40
|
|
|
/** @var CacheInterface */ |
|
41
|
|
|
private $cache; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* EntityManager constructor. |
|
45
|
|
|
* @param string|null $hydratorDir |
|
46
|
|
|
* @param string|null $hydratorNamespace |
|
47
|
|
|
* @param CacheInterface|null $cache |
|
48
|
|
|
* @param string|null $hydratorAutoGenerate |
|
49
|
|
|
* @param MetaDataFactory|null $metaDataFactory |
|
50
|
|
|
*/ |
|
51
|
25 |
|
public function __construct( |
|
52
|
|
|
string $hydratorDir = null, |
|
53
|
|
|
string $hydratorNamespace = null, |
|
54
|
|
|
CacheInterface $cache = null, |
|
55
|
|
|
string $hydratorAutoGenerate = null, |
|
56
|
|
|
MetaDataFactory $metaDataFactory = null |
|
57
|
|
|
) { |
|
58
|
25 |
|
if ($hydratorDir === null) { |
|
59
|
1 |
|
$hydratorDir = sys_get_temp_dir(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
25 |
|
if ($hydratorAutoGenerate === null) { |
|
63
|
25 |
|
$hydratorAutoGenerate = HydratorAutoGenerate::ALWAYS; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
25 |
|
if (!is_writable($hydratorDir)) { |
|
67
|
|
|
throw new HydratorException("Hydrators cannot be generated, directory ($hydratorDir) is not writable."); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
25 |
|
if ($metaDataFactory === null) { |
|
71
|
25 |
|
$metaDataFactory = new AnnotationMetaDataFactory(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
25 |
|
if ($cache === null) { |
|
75
|
25 |
|
$cache = new ArrayCachePool(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
25 |
|
$this->cache = $cache; |
|
79
|
25 |
|
$this->hydratorDir = $hydratorDir; |
|
80
|
25 |
|
$this->metaDataFactory = $metaDataFactory; |
|
81
|
25 |
|
$this->hydratorNamespace = $hydratorNamespace ?? ''; |
|
82
|
|
|
|
|
83
|
25 |
|
$this->hydratorFactory = new HydratorFactory($this, $hydratorAutoGenerate); |
|
84
|
25 |
|
} |
|
85
|
|
|
|
|
86
|
2 |
|
public function getHydratorDir(): string |
|
87
|
|
|
{ |
|
88
|
2 |
|
return $this->hydratorDir; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
14 |
|
public function getHydratorNamespace(): string |
|
92
|
|
|
{ |
|
93
|
14 |
|
return $this->hydratorNamespace; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Creates new entity in the storage. |
|
98
|
|
|
* |
|
99
|
|
|
* @param Entity $entity |
|
100
|
|
|
* @return Entity |
|
101
|
|
|
*/ |
|
102
|
|
|
public function create(Entity $entity): Entity |
|
103
|
|
|
{ |
|
104
|
|
|
$this->getRepository(get_class($entity))->create($entity); |
|
105
|
|
|
$this->attach($entity); |
|
106
|
|
|
|
|
107
|
|
|
return $entity; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Updated entity in the storage. |
|
112
|
|
|
* |
|
113
|
|
|
* @param Entity $entity |
|
114
|
|
|
* @return Entity |
|
115
|
|
|
*/ |
|
116
|
1 |
|
public function update(Entity $entity): Entity |
|
117
|
|
|
{ |
|
118
|
1 |
|
$this->getRepository(get_class($entity))->update($entity); |
|
119
|
|
|
|
|
120
|
1 |
|
return $entity; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Removes entity from the storage. |
|
125
|
|
|
* |
|
126
|
|
|
* @param Entity $entity |
|
127
|
|
|
* @return Entity |
|
128
|
|
|
*/ |
|
129
|
2 |
|
public function remove(Entity $entity): Entity |
|
130
|
|
|
{ |
|
131
|
2 |
|
$this->getRepository(get_class($entity))->remove($entity); |
|
132
|
2 |
|
$this->detach($entity); |
|
133
|
|
|
|
|
134
|
2 |
|
return $entity; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Retrieves entity by identifier from the storage. |
|
139
|
|
|
* |
|
140
|
|
|
* @param string $entity |
|
141
|
|
|
* @param $id |
|
142
|
|
|
* @return Entity |
|
143
|
|
|
*/ |
|
144
|
9 |
|
public function get(string $entity, $id): Entity |
|
145
|
|
|
{ |
|
146
|
9 |
|
$key = $this->getId($entity, $id); |
|
147
|
|
|
|
|
148
|
9 |
|
if ($this->has($entity, $id)) { |
|
149
|
1 |
|
return $this->registry[$key]; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
9 |
|
return $this->getRepository($entity)->get($id); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
9 |
|
public function getRepository(string $entity): Repository |
|
156
|
|
|
{ |
|
157
|
9 |
|
if ($this->hasRepository($entity)) { |
|
158
|
9 |
|
return $this->repositories[$entity]; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
throw RepositoryException::forNotRegisteredRepository($entity); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
9 |
|
public function hasRepository(string $entity): bool |
|
165
|
|
|
{ |
|
166
|
9 |
|
return isset($this->repositories[$entity]); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
14 |
|
public function addRepository(Repository ...$repositories): void |
|
170
|
|
|
{ |
|
171
|
14 |
|
foreach ($repositories as $repository) { |
|
172
|
14 |
|
$this->repositories[$repository->getEntityClass()] = $repository; |
|
173
|
|
|
} |
|
174
|
14 |
|
} |
|
175
|
|
|
|
|
176
|
9 |
|
public function attach(Entity $entity): Entity |
|
177
|
|
|
{ |
|
178
|
9 |
|
$key = $this->getId($entity); |
|
179
|
|
|
|
|
180
|
9 |
|
if (!isset($this->registry[$key])) { |
|
181
|
9 |
|
$this->registry[$key] = $entity; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
9 |
|
return $this->registry[$key]; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
2 |
|
public function detach(Entity $entity): Entity |
|
188
|
|
|
{ |
|
189
|
2 |
|
$key = $this->getId($entity); |
|
190
|
2 |
|
if (isset($this->registry[$key])) { |
|
191
|
2 |
|
unset($this->registry[$key]); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
2 |
|
return $entity; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* |
|
199
|
|
|
* @param string $class |
|
200
|
|
|
* @param $id |
|
201
|
|
|
* @return bool |
|
202
|
|
|
*/ |
|
203
|
9 |
|
public function has(string $class, $id): bool |
|
204
|
|
|
{ |
|
205
|
9 |
|
$key = $this->getId($class, $id); |
|
206
|
9 |
|
return isset($this->registry[$key]); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Checks if entity lives in the identity map. |
|
211
|
|
|
* |
|
212
|
|
|
* @param Entity $entity |
|
213
|
|
|
* @return bool |
|
214
|
|
|
*/ |
|
215
|
|
|
public function contains(Entity $entity): bool |
|
216
|
|
|
{ |
|
217
|
|
|
return in_array($entity, $this->registry, true); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Clears identity map. |
|
222
|
|
|
*/ |
|
223
|
1 |
|
public function clear(): void |
|
224
|
|
|
{ |
|
225
|
1 |
|
$this->registry = []; |
|
226
|
1 |
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Gets global id for entity which is used for |
|
230
|
|
|
* later storage in the identity map. |
|
231
|
|
|
* |
|
232
|
|
|
* @param $entity |
|
233
|
|
|
* @param null $id |
|
|
|
|
|
|
234
|
|
|
* @return string |
|
235
|
|
|
*/ |
|
236
|
9 |
|
private function getId($entity, $id = null): string |
|
237
|
|
|
{ |
|
238
|
9 |
|
if ($entity instanceof Entity) { |
|
239
|
9 |
|
return get_class($entity) . '@' . $entity->getId()->getValue(); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
return "${entity}@${id}"; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Creates instance of the entity class and hydrates it with passed data. |
|
247
|
|
|
* |
|
248
|
|
|
* @param string $entityClass |
|
249
|
|
|
* @param array $data |
|
250
|
|
|
* @return object |
|
251
|
|
|
*/ |
|
252
|
|
|
public function hydrate(string $entityClass, array $data) |
|
253
|
|
|
{ |
|
254
|
2 |
|
$hydrator = $this->getHydrator($entityClass); |
|
255
|
|
|
|
|
256
|
2 |
|
return $hydrator->hydrate($data); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Extracts data from passed entity and returns it. |
|
261
|
|
|
* |
|
262
|
|
|
* @param $entity |
|
263
|
|
|
* @return array |
|
264
|
|
|
*/ |
|
265
|
|
|
public function extract($entity): array |
|
266
|
|
|
{ |
|
267
|
|
|
$entityClass = get_class($entity); |
|
268
|
|
|
|
|
269
|
|
|
$hydrator = $this->getHydrator($entityClass); |
|
270
|
|
|
|
|
271
|
|
|
return $hydrator->extract($entity); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Returns hydrator for the given entity. |
|
276
|
|
|
* |
|
277
|
|
|
* @param string $entity |
|
278
|
|
|
* @return ObjectHydrator |
|
279
|
|
|
*/ |
|
280
|
|
|
public function getHydrator(string $entity): ObjectHydrator |
|
281
|
|
|
{ |
|
282
|
14 |
|
if (!isset($this->hydrators[$entity])) { |
|
283
|
14 |
|
$this->hydrators[$entity] = $this->hydratorFactory->get($entity); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
14 |
|
return $this->hydrators[$entity]; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* Returns entity's mapping metadata information. |
|
291
|
|
|
* |
|
292
|
|
|
* @param string|class $entity |
|
|
|
|
|
|
293
|
|
|
* @return EntityMetaData |
|
294
|
|
|
*/ |
|
295
|
|
|
public function getMetaData(string $entity): EntityMetaData |
|
296
|
|
|
{ |
|
297
|
14 |
|
$key = str_replace('\\', '.', $entity) . '.metadata'; |
|
298
|
|
|
|
|
299
|
14 |
|
if (!$this->cache->has($key)) { |
|
300
|
14 |
|
$metaData = $this->metaDataFactory->getMetaData($entity); |
|
301
|
14 |
|
$this->cache->set($key, $metaData); |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
14 |
|
return $this->cache->get($key); |
|
305
|
|
|
} |
|
306
|
|
|
} |
|
307
|
|
|
|