|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tarantool\Mapper\Schema; |
|
4
|
|
|
|
|
5
|
|
|
use Tarantool\Mapper\Contracts; |
|
6
|
|
|
use Exception; |
|
7
|
|
|
use LogicException; |
|
8
|
|
|
|
|
9
|
|
|
class Meta implements Contracts\Meta |
|
10
|
|
|
{ |
|
11
|
|
|
protected $manager; |
|
12
|
|
|
protected $property = []; |
|
13
|
|
|
protected $indexes = []; |
|
14
|
|
|
protected $types = []; |
|
15
|
|
|
protected $instances = []; |
|
16
|
|
|
|
|
17
|
63 |
|
public function __construct(Contracts\Manager $manager, array $data = null) |
|
18
|
|
|
{ |
|
19
|
63 |
|
$this->manager = $manager; |
|
20
|
|
|
|
|
21
|
63 |
|
if($data) { |
|
22
|
1 |
|
$this->property = $data['property']; |
|
23
|
1 |
|
$this->indexes = $data['indexes']; |
|
24
|
1 |
|
$this->types = $data['types']; |
|
25
|
|
|
|
|
26
|
|
|
} else { |
|
27
|
63 |
|
$this->collectData(); |
|
28
|
|
|
} |
|
29
|
63 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return Type |
|
33
|
|
|
*/ |
|
34
|
63 |
|
public function get($type) |
|
35
|
|
|
{ |
|
36
|
63 |
|
if (!array_key_exists($type, $this->instances)) { |
|
37
|
63 |
|
$spaceId = $this->manager->getSchema()->getSpaceId($type); |
|
38
|
63 |
|
if (!$spaceId) { |
|
39
|
1 |
|
throw new LogicException("Type $type not exists"); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
63 |
|
$this->instances[$type] = new Type( |
|
43
|
63 |
|
$this->manager, $type, |
|
44
|
63 |
|
$this->property[$spaceId], |
|
45
|
63 |
|
$this->types[$spaceId], |
|
46
|
63 |
|
$this->indexes[$spaceId] |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
63 |
|
return $this->instances[$type]; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
4 |
|
public function has($type) |
|
54
|
|
|
{ |
|
55
|
|
|
// was created |
|
56
|
4 |
|
if (array_key_exists($type, $this->instances)) { |
|
57
|
2 |
|
return true; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
// can be created |
|
61
|
4 |
|
$spaceId = $this->manager->getSchema()->getSpaceId($type); |
|
62
|
4 |
|
if (array_key_exists($spaceId, $this->property)) { |
|
63
|
1 |
|
return true; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
4 |
|
return false; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
4 |
|
public function remove($type) |
|
70
|
|
|
{ |
|
71
|
4 |
|
$other = $this->manager->get('property')->find(['type' => $type]); |
|
|
|
|
|
|
72
|
4 |
|
if (count($other)) { |
|
73
|
1 |
|
$name = $this->manager->getSchema()->getSpaceName($other[0]->space); |
|
74
|
1 |
|
throw new Exception("Space $name references ".$type); |
|
75
|
|
|
} |
|
76
|
3 |
|
$instance = $this->get($type); |
|
77
|
3 |
|
$rows = $instance->getSpace()->select([])->getData(); |
|
78
|
3 |
|
if (count($rows)) { |
|
79
|
1 |
|
throw new Exception("Can't remove non-empty space $type"); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
2 |
|
$indexes = array_reverse(array_keys($instance->getIndexes())); |
|
83
|
2 |
|
foreach ($indexes as $index) { |
|
84
|
2 |
|
$instance->dropIndex($index); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
2 |
|
foreach (array_reverse($instance->getProperties()) as $property) { |
|
88
|
2 |
|
$instance->removeProperty($property); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
2 |
|
$sq = $this->manager->get('sequence')->findOne(['space' => $instance->getSpaceId()]); |
|
|
|
|
|
|
92
|
2 |
|
if ($sq) { |
|
93
|
1 |
|
$this->manager->remove($sq); |
|
94
|
1 |
|
$this->manager->get('sequence')->flushCache(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
2 |
|
$this->manager->getSchema()->dropSpace($type); |
|
98
|
2 |
|
unset($this->instances[$type]); |
|
99
|
|
|
|
|
100
|
2 |
|
$this->manager->forgetRepository($type); |
|
101
|
2 |
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return Type |
|
105
|
|
|
*/ |
|
106
|
63 |
|
public function create($type, array $fields = null) |
|
107
|
|
|
{ |
|
108
|
63 |
|
if ($this->manager->getSchema()->hasSpace($type)) { |
|
109
|
1 |
|
throw new LogicException("Type $type exists"); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
63 |
|
$this->manager->getSchema()->createSpace($type); |
|
113
|
|
|
|
|
114
|
63 |
|
$instance = new Type($this->manager, $type, [], [], []); |
|
115
|
|
|
|
|
116
|
63 |
|
$instance->addProperty('id'); |
|
117
|
63 |
|
$instance->addIndex('id'); |
|
118
|
|
|
|
|
119
|
63 |
|
if ($fields) { |
|
120
|
63 |
|
foreach ($fields as $index => $field) { |
|
121
|
63 |
|
if ($field instanceof Contracts\Type) { |
|
122
|
5 |
|
if (!is_numeric($index)) { |
|
123
|
1 |
|
$instance->reference($field, $index); |
|
124
|
|
|
} else { |
|
125
|
5 |
|
$instance->reference($field); |
|
126
|
|
|
} |
|
127
|
|
|
} else { |
|
128
|
63 |
|
$instance->addProperty($field); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
63 |
|
$this->instances[$type] = $instance; |
|
133
|
|
|
|
|
134
|
63 |
|
return $instance; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
1 |
|
public function setConvention(Contracts\Convention $convention) |
|
138
|
|
|
{ |
|
139
|
1 |
|
$this->convention = $convention; |
|
|
|
|
|
|
140
|
|
|
|
|
141
|
1 |
|
return $this; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
63 |
|
public function getConvention() |
|
145
|
|
|
{ |
|
146
|
63 |
|
if (!isset($this->convention)) { |
|
147
|
63 |
|
$this->convention = new Convention(); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
63 |
|
return $this->convention; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
63 |
|
private function collectData() |
|
154
|
|
|
{ |
|
155
|
63 |
|
$client = $this->manager->getClient(); |
|
156
|
63 |
|
foreach ($client->getSpace('property')->select([], 'space')->getData() as $property) { |
|
157
|
63 |
|
list($id, $spaceId, $line, $name, $type) = $property; |
|
|
|
|
|
|
158
|
63 |
|
if (!array_key_exists($spaceId, $this->property)) { |
|
159
|
63 |
|
$this->property[$spaceId] = []; |
|
160
|
63 |
|
$this->types[$spaceId] = []; |
|
161
|
|
|
} |
|
162
|
63 |
|
$this->property[$spaceId][$line] = $name; |
|
163
|
63 |
|
$this->types[$spaceId][$name] = $type; |
|
164
|
|
|
} |
|
165
|
63 |
|
foreach ($client->getSpace('_vindex')->select([], 'primary')->getData() as $index) { |
|
166
|
63 |
|
list($spaceId, $num, $name, $type, $params, $properties) = $index; |
|
|
|
|
|
|
167
|
63 |
|
if (!array_key_exists($spaceId, $this->property)) { |
|
168
|
|
|
// tarantool space index |
|
169
|
63 |
|
continue; |
|
170
|
|
|
} |
|
171
|
63 |
|
if (!isset($this->indexes[$spaceId])) { |
|
172
|
63 |
|
$this->indexes[$spaceId] = []; |
|
173
|
|
|
} |
|
174
|
63 |
|
$this->indexes[$spaceId][$num] = []; |
|
175
|
63 |
|
foreach ($properties as $row) { |
|
176
|
63 |
|
list($part, $type) = $row; |
|
|
|
|
|
|
177
|
63 |
|
$this->indexes[$spaceId][$num][] = $this->property[$spaceId][$part]; |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
63 |
|
foreach ($this->property as $spaceId => $collection) { |
|
181
|
63 |
|
ksort($collection); |
|
182
|
63 |
|
$this->property[$spaceId] = $collection; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
63 |
|
} |
|
186
|
|
|
|
|
187
|
1 |
|
public function toArray() |
|
188
|
|
|
{ |
|
189
|
1 |
|
$this->collectData(); |
|
190
|
|
|
|
|
191
|
|
|
return [ |
|
192
|
1 |
|
'property' => $this->property, |
|
193
|
1 |
|
'indexes' => $this->indexes, |
|
194
|
1 |
|
'types' => $this->types, |
|
195
|
|
|
]; |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: