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