|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tarantool\Mapper; |
|
4
|
|
|
|
|
5
|
|
|
use BadMethodCallException; |
|
6
|
|
|
use LogicException; |
|
7
|
|
|
|
|
8
|
|
|
class Repository implements Contracts\Repository |
|
9
|
|
|
{ |
|
10
|
|
|
private $type; |
|
11
|
|
|
private $entities = []; |
|
12
|
|
|
private $keyMap = []; |
|
13
|
|
|
private $findCache = []; |
|
14
|
|
|
private $original = []; |
|
15
|
|
|
|
|
16
|
|
|
private $magicMethodRules = [ |
|
17
|
|
|
'by' => false, |
|
18
|
|
|
'firstBy' => true, |
|
19
|
|
|
'oneBy' => true, |
|
20
|
|
|
]; |
|
21
|
|
|
|
|
22
|
53 |
|
public function __construct(Contracts\Type $type) |
|
23
|
|
|
{ |
|
24
|
53 |
|
$this->type = $type; |
|
25
|
53 |
|
} |
|
26
|
|
|
|
|
27
|
53 |
|
public function create($params = null) |
|
28
|
|
|
{ |
|
29
|
53 |
|
if ($params && !is_array($params)) { |
|
30
|
53 |
|
$params = [$params]; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
53 |
|
if (!is_array($params)) { |
|
34
|
1 |
|
$params = []; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
53 |
|
$data = []; |
|
38
|
53 |
|
foreach ($params as $k => $v) { |
|
39
|
53 |
|
if (is_numeric($k)) { |
|
40
|
53 |
|
if ($v instanceof Contracts\Entity) { |
|
41
|
4 |
|
$type = $this->type->getManager()->findRepository($v)->getType(); |
|
42
|
4 |
|
$k = $this->type->getReferenceProperty($type); |
|
43
|
|
|
} else { |
|
44
|
53 |
|
$primitive = []; |
|
45
|
53 |
|
foreach ($this->type->getProperties() as $property) { |
|
46
|
53 |
|
if (!$this->type->isReference($property)) { |
|
47
|
53 |
|
$primitive[] = $property; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
53 |
|
if (count($primitive) == 2) { |
|
51
|
53 |
|
$k = $primitive[1]; |
|
52
|
|
|
} else { |
|
53
|
1 |
|
throw new \Exception("Can't calculate key name"); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
53 |
|
if (!$this->type->hasProperty($k)) { |
|
58
|
1 |
|
throw new \Exception("Unknown property $k"); |
|
59
|
|
|
} |
|
60
|
53 |
|
$data[$k] = $this->type->encodeProperty($k, $v); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
53 |
|
$this->flushCache(); |
|
64
|
|
|
|
|
65
|
53 |
|
return $this->createInstance($data); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
53 |
|
private function createInstance($data) |
|
69
|
|
|
{ |
|
70
|
53 |
|
$class = $this->getType()->getEntityClass(); |
|
71
|
|
|
|
|
72
|
53 |
|
return $this->register(new $class($data)); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
53 |
|
public function __call($method, $arguments) |
|
76
|
|
|
{ |
|
77
|
53 |
|
foreach ($this->magicMethodRules as $prefix => $oneItem) { |
|
78
|
53 |
|
if (substr($method, 0, strlen($prefix)) == $prefix) { |
|
79
|
53 |
|
$tail = substr($method, strlen($prefix)); |
|
80
|
53 |
|
$fields = array_map('strtolower', explode('And', $tail)); |
|
81
|
|
|
|
|
82
|
53 |
|
return $this->find(array_combine($fields, $arguments), $oneItem); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
1 |
|
throw new BadMethodCallException("Method $method not found"); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
16 |
|
public function findOne($params) |
|
90
|
|
|
{ |
|
91
|
16 |
|
return $this->find($params, true); |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
53 |
|
public function find($params = [], $oneItem = false) |
|
95
|
|
|
{ |
|
96
|
53 |
|
$query = []; |
|
97
|
|
|
|
|
98
|
53 |
|
if (is_string($params) && 1 * $params == $params) { |
|
99
|
1 |
|
$params = 1 * $params; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
53 |
|
if (is_int($params)) { |
|
103
|
9 |
|
if (isset($this->keyMap[$params])) { |
|
104
|
3 |
|
return $this->entities[$this->keyMap[$params]]; |
|
105
|
|
|
} |
|
106
|
|
|
$query = [ |
|
107
|
6 |
|
'id' => $params, |
|
108
|
|
|
]; |
|
109
|
6 |
|
$oneItem = true; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
53 |
|
if ($params instanceof Contracts\Entity) { |
|
113
|
2 |
|
$params = [$params]; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
53 |
|
if (is_array($params)) { |
|
117
|
53 |
|
foreach ($params as $key => $value) { |
|
118
|
53 |
|
if (is_numeric($key) && $value instanceof Contracts\Entity) { |
|
119
|
2 |
|
$type = $this->type->getManager()->findRepository($value)->getType(); |
|
120
|
2 |
|
$key = $this->type->getReferenceProperty($type); |
|
121
|
|
|
} |
|
122
|
53 |
|
if ($this->type->hasProperty($key)) { |
|
123
|
53 |
|
$query[$key] = $this->type->encodeProperty($key, $value); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
53 |
|
$findKey = md5(json_encode($query).($oneItem ? 'x' : '')); |
|
129
|
53 |
|
if (array_key_exists($findKey, $this->findCache)) { |
|
130
|
53 |
|
return $this->findCache[$findKey]; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
53 |
|
$index = $this->type->findIndex(array_keys($query)); |
|
134
|
53 |
|
if (!is_numeric($index)) { |
|
135
|
2 |
|
throw new \Exception('No index found for '.json_encode(array_keys($query))); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
53 |
|
$values = count($query) ? $this->type->getIndexTuple($index, $query) : []; |
|
139
|
53 |
|
$data = $this->type->getSpace()->select($values, $index); |
|
140
|
|
|
|
|
141
|
53 |
|
$result = []; |
|
142
|
53 |
|
if (!empty($data->getData())) { |
|
143
|
53 |
|
foreach ($data->getData() as $tuple) { |
|
144
|
53 |
|
$data = $this->type->fromTuple($tuple); |
|
145
|
53 |
|
if (isset($data['id']) && array_key_exists($data['id'], $this->keyMap)) { |
|
146
|
53 |
|
$entity = $this->entities[$this->keyMap[$data['id']]]; |
|
147
|
53 |
|
$entity->update($data); |
|
148
|
|
|
} else { |
|
149
|
53 |
|
$entity = $this->createInstance($data); |
|
150
|
|
|
} |
|
151
|
53 |
|
if ($oneItem) { |
|
152
|
53 |
|
return $this->findCache[$findKey] = $entity; |
|
153
|
|
|
} |
|
154
|
13 |
|
$result[] = $entity; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
53 |
|
if (!$oneItem) { |
|
158
|
16 |
|
return $this->findCache[$findKey] = $result; |
|
|
|
|
|
|
159
|
|
|
} |
|
160
|
53 |
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @return Entity |
|
164
|
|
|
*/ |
|
165
|
53 |
|
public function knows(Contracts\Entity $entity) |
|
166
|
|
|
{ |
|
167
|
53 |
|
return in_array($entity, $this->entities); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
7 |
|
public function remove(Contracts\Entity $entity) |
|
171
|
|
|
{ |
|
172
|
7 |
|
unset($this->entities[$this->keyMap[$entity->id]]); |
|
173
|
7 |
|
unset($this->keyMap[$entity->id]); |
|
174
|
7 |
|
$this->flushCache(); |
|
175
|
|
|
|
|
176
|
7 |
|
$this->type->getSpace()->delete([$entity->id]); |
|
|
|
|
|
|
177
|
7 |
|
} |
|
178
|
|
|
|
|
179
|
1 |
|
public function removeAll() |
|
180
|
|
|
{ |
|
181
|
1 |
|
foreach ($this->find([]) as $entity) { |
|
|
|
|
|
|
182
|
1 |
|
$this->remove($entity); |
|
183
|
|
|
} |
|
184
|
1 |
|
$this->flushCache(); |
|
185
|
1 |
|
} |
|
186
|
|
|
|
|
187
|
53 |
|
public function flushCache() |
|
188
|
|
|
{ |
|
189
|
53 |
|
$this->findCache = []; |
|
190
|
53 |
|
} |
|
191
|
|
|
|
|
192
|
53 |
|
public function save(Contracts\Entity $entity) |
|
193
|
|
|
{ |
|
194
|
53 |
|
if (!$this->knows($entity)) { |
|
195
|
1 |
|
throw new LogicException('Entity is not related with this repository'); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
53 |
|
if (!$entity->getId()) { |
|
199
|
53 |
|
$this->generateId($entity); |
|
200
|
53 |
|
$tuple = $this->type->getCompleteTuple($entity->toArray()); |
|
201
|
53 |
|
$this->type->getSpace()->insert($tuple); |
|
202
|
|
|
} else { |
|
203
|
13 |
|
$array = $entity->toArray(false); |
|
204
|
13 |
|
$changes = []; |
|
205
|
13 |
|
$id = $entity->getId(); |
|
206
|
13 |
|
if (!array_key_exists($id, $this->original)) { |
|
207
|
|
|
$changes = $array; |
|
208
|
|
|
} else { |
|
209
|
13 |
|
foreach ($array as $k => $v) { |
|
210
|
13 |
|
if (!array_key_exists($k, $this->original[$id])) { |
|
211
|
2 |
|
$changes[$k] = $v; |
|
212
|
13 |
|
} elseif ($v !== $this->original[$id][$k]) { |
|
213
|
13 |
|
$changes[$k] = $v; |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
13 |
|
if (count($changes)) { |
|
218
|
13 |
|
$operations = []; |
|
219
|
13 |
|
foreach ($this->type->getTuple($changes) as $key => $value) { |
|
220
|
13 |
|
$operations[] = ['=', $key, $value]; |
|
221
|
|
|
} |
|
222
|
|
|
try { |
|
223
|
13 |
|
$this->type->getSpace()->update($id, $operations); |
|
224
|
1 |
|
} catch (\Exception $e) { |
|
225
|
1 |
|
$this->type->getSpace()->delete([$id]); |
|
226
|
1 |
|
$tuple = $this->type->getCompleteTuple($entity->toArray()); |
|
227
|
1 |
|
$this->type->getSpace()->insert($tuple); |
|
228
|
|
|
} |
|
229
|
13 |
|
$this->original[$id] = $entity->toArray(); |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
53 |
|
return $entity; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
53 |
|
private function register(Contracts\Entity $entity) |
|
237
|
|
|
{ |
|
238
|
53 |
|
if (!$this->knows($entity)) { |
|
239
|
53 |
|
$this->entities[] = $entity; |
|
240
|
|
|
} |
|
241
|
53 |
|
if ($entity->getId() && !array_key_exists($entity->getId(), $this->keyMap)) { |
|
242
|
53 |
|
$this->keyMap[$entity->getId()] = array_search($entity, $this->entities); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
53 |
|
if ($entity->getId()) { |
|
246
|
53 |
|
$this->original[$entity->getId()] = $entity->toArray(); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
53 |
|
return $entity; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
53 |
|
private function generateId(Contracts\Entity $entity) |
|
253
|
|
|
{ |
|
254
|
53 |
|
$manager = $this->type->getManager(); |
|
255
|
53 |
|
$name = $this->type->getName(); |
|
|
|
|
|
|
256
|
53 |
|
$spaceId = $this->type->getSpaceId(); |
|
257
|
|
|
|
|
258
|
53 |
|
$sequence = $manager->get('sequence')->oneBySpace($spaceId); |
|
259
|
53 |
|
if (!$sequence) { |
|
260
|
53 |
|
$sequence = $manager->get('sequence')->create([ |
|
261
|
53 |
|
'space' => $spaceId, |
|
262
|
53 |
|
'value' => 0, |
|
263
|
|
|
]); |
|
264
|
53 |
|
$manager->save($sequence); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
53 |
|
$nextValue = +$manager->getMeta() |
|
268
|
53 |
|
->get('sequence') |
|
269
|
53 |
|
->getSpace() |
|
270
|
53 |
|
->update($sequence->id, [['+', 2, 1]]) |
|
271
|
53 |
|
->getData()[0][2]; |
|
272
|
|
|
|
|
273
|
53 |
|
$entity->setId($nextValue); |
|
274
|
|
|
|
|
275
|
53 |
|
$this->register($entity); |
|
276
|
|
|
|
|
277
|
53 |
|
return $entity; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
53 |
|
public function getType() |
|
281
|
|
|
{ |
|
282
|
53 |
|
return $this->type; |
|
283
|
|
|
} |
|
284
|
|
|
} |
|
285
|
|
|
|