|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tarantool\Mapper\Plugin; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
|
|
7
|
|
|
use phpDocumentor\Reflection\DocBlockFactory; |
|
8
|
|
|
use ReflectionClass; |
|
9
|
|
|
use ReflectionProperty; |
|
10
|
|
|
|
|
11
|
|
|
use Tarantool\Mapper\Entity; |
|
12
|
|
|
use Tarantool\Mapper\Plugin; |
|
13
|
|
|
use Tarantool\Mapper\Repository; |
|
14
|
|
|
|
|
15
|
|
|
class Annotation extends UserClasses |
|
16
|
|
|
{ |
|
17
|
|
|
private $entities = []; |
|
|
|
|
|
|
18
|
|
|
private $entityPostfix; |
|
19
|
|
|
|
|
20
|
|
|
private $repositories = []; |
|
|
|
|
|
|
21
|
|
|
private $repositoryPostifx; |
|
22
|
|
|
|
|
23
|
|
|
public function register($class) |
|
24
|
|
|
{ |
|
25
|
|
|
$isEntity = is_subclass_of($class, Entity::class); |
|
|
|
|
|
|
26
|
|
|
$isRepository = is_subclass_of($class, Repository::class); |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
if (!$isEntity && !$isRepository) { |
|
29
|
|
|
throw new Exception("Invalid registration"); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
if ($isEntity) { |
|
33
|
|
|
if ($class == Entity::class) { |
|
34
|
|
|
throw new Exception("Invalid entity registration"); |
|
35
|
|
|
} |
|
36
|
|
|
$this->entities[] = $class; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
if ($isRepository) { |
|
40
|
|
|
if ($class == Repository::class) { |
|
41
|
|
|
throw new Exception("Invalid repository registration"); |
|
42
|
|
|
} |
|
43
|
|
|
$this->repositories[] = $class; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$space = $this->getSpaceName($class); |
|
47
|
|
|
if ($this->mapper->getSchema()->hasSpace($space)) { |
|
48
|
|
|
if ($isEntity) { |
|
49
|
|
|
$this->mapEntity($space, $class); |
|
50
|
|
|
} else { |
|
51
|
|
|
$this->mapRepository($space, $class); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function migrate() |
|
58
|
|
|
{ |
|
59
|
|
|
$factory = DocBlockFactory::createInstance(); |
|
60
|
|
|
|
|
61
|
|
|
$schema = $this->mapper->getSchema(); |
|
62
|
|
|
|
|
63
|
|
|
foreach ($this->entities as $entity) { |
|
64
|
|
|
$spaceName = $this->getSpaceName($entity); |
|
65
|
|
|
$space = $schema->hasSpace($spaceName) ? $schema->getSpace($spaceName) : $schema->createSpace($spaceName); |
|
66
|
|
|
|
|
67
|
|
|
$this->mapEntity($spaceName, $entity); |
|
68
|
|
|
|
|
69
|
|
|
$class = new ReflectionClass($entity); |
|
70
|
|
|
|
|
71
|
|
|
foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $property) { |
|
72
|
|
|
$description = $factory->create($property->getDocComment()); |
|
73
|
|
|
$tags = $description->getTags('var'); |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
View Code Duplication |
if (!count($tags)) { |
|
|
|
|
|
|
76
|
|
|
throw new Exception("No var tag for ".$entity.'::'.$property->getName()); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
View Code Duplication |
if (count($tags) > 1) { |
|
|
|
|
|
|
80
|
|
|
throw new Exception("Invalid var tag for ".$entity.'::'.$property->getName()); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$property = $this->toUnderscore($property->getName()); |
|
84
|
|
|
$type = $this->getTarantoolType($tags[0]->getType()); |
|
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
if (!$space->hasProperty($property)) { |
|
87
|
|
|
$space->addProperty($property, $type); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
foreach ($this->repositories as $repository) { |
|
93
|
|
|
$spaceName = $this->getSpaceName($repository); |
|
94
|
|
|
|
|
95
|
|
|
if (!$schema->hasSpace($spaceName)) { |
|
96
|
|
|
throw new Exception("Repository with no entity definition"); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$this->mapRepository($spaceName, $repository); |
|
100
|
|
|
|
|
101
|
|
|
$space = $schema->getSpace($spaceName); |
|
102
|
|
|
$properties = $class->getDefaultProperties(); |
|
|
|
|
|
|
103
|
|
|
if (array_key_exists('indexes', $properties)) { |
|
104
|
|
|
foreach ($properties['indexes'] as $index) { |
|
|
|
|
|
|
105
|
|
|
if (!is_array($index)) { |
|
106
|
|
|
$index = (array) $index; |
|
107
|
|
|
} |
|
108
|
|
|
if (!array_key_exists('fields', $index)) { |
|
109
|
|
|
$index = ['fields' => $index]; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$index['if_not_exists'] = true; |
|
113
|
|
|
$space->addIndex($index); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
foreach ($schema->getSpaces() as $space) { |
|
119
|
|
|
if (!count($space->getIndexes())) { |
|
120
|
|
|
if (!$space->hasProperty('id')) { |
|
121
|
|
|
throw new Exception("No primary index on ". $space->getName()); |
|
122
|
|
|
} |
|
123
|
|
|
$space->addIndex(['id']); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $this; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function setEntityPostfix($postfix) |
|
131
|
|
|
{ |
|
132
|
|
|
$this->entityPostfix = $postfix; |
|
133
|
|
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function setRepositoryPostfix($postfix) |
|
137
|
|
|
{ |
|
138
|
|
|
$this->repositoryPostifx = $postfix; |
|
139
|
|
|
return $this; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public function getRepositoryMapping() |
|
143
|
|
|
{ |
|
144
|
|
|
$mapping = []; |
|
145
|
|
|
foreach ($this->repositories as $class) { |
|
146
|
|
|
$mapping[$this->getSpaceName($class)] = $class; |
|
147
|
|
|
} |
|
148
|
|
|
return $mapping; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function getEntityMapping() |
|
152
|
|
|
{ |
|
153
|
|
|
$mapping = []; |
|
154
|
|
|
foreach ($this->entities as $class) { |
|
155
|
|
|
$mapping[$this->getSpaceName($class)] = $class; |
|
156
|
|
|
} |
|
157
|
|
|
return $mapping; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
private $spaceNames = []; |
|
161
|
|
|
|
|
162
|
|
|
public function getSpaceName($class) |
|
163
|
|
|
{ |
|
164
|
|
|
if (!array_key_exists($class, $this->spaceNames)) { |
|
165
|
|
|
$reflection = new ReflectionClass($class); |
|
166
|
|
|
$className = $reflection->getShortName(); |
|
167
|
|
|
|
|
168
|
|
View Code Duplication |
if ($reflection->isSubclassOf(Repository::class)) { |
|
|
|
|
|
|
169
|
|
|
if ($this->repositoryPostifx) { |
|
170
|
|
|
$className = substr($className, 0, strlen($className) - strlen($this->repositoryPostifx)); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
View Code Duplication |
if ($reflection->isSubclassOf(Entity::class)) { |
|
|
|
|
|
|
175
|
|
|
if ($this->entityPostfix) { |
|
176
|
|
|
$className = substr($className, 0, strlen($className) - strlen($this->entityPostfix)); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
$this->spaceNames[$class] = $this->toUnderscore($className); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
return $this->spaceNames[$class]; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
private $underscores = []; |
|
187
|
|
|
|
|
188
|
|
|
private function toUnderscore($input) |
|
189
|
|
|
{ |
|
190
|
|
|
if (!array_key_exists($input, $this->underscores)) { |
|
191
|
|
|
preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches); |
|
192
|
|
|
$ret = $matches[0]; |
|
193
|
|
|
foreach ($ret as &$match) { |
|
194
|
|
|
$match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match); |
|
195
|
|
|
} |
|
196
|
|
|
$this->underscores[$input] = implode('_', $ret); |
|
197
|
|
|
} |
|
198
|
|
|
return $this->underscores[$input]; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
private $tarantoolTypes = []; |
|
202
|
|
|
|
|
203
|
|
|
private function getTarantoolType(string $type) |
|
204
|
|
|
{ |
|
205
|
|
|
if (array_key_exists($type, $this->tarantoolTypes)) { |
|
206
|
|
|
return $this->tarantoolTypes[$type]; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
if ($type[0] == '\\') { |
|
210
|
|
|
return $this->tarantoolTypes[$type] = 'unsigned'; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
switch ($type) { |
|
214
|
|
|
case 'int': |
|
215
|
|
|
return $this->tarantoolTypes[$type] = 'unsigned'; |
|
216
|
|
|
|
|
217
|
|
|
default: |
|
218
|
|
|
return $this->tarantoolTypes[$type] = 'str'; |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|