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
|
|
|
|
16
|
35 |
|
public function __construct(Contracts\Manager $manager) |
17
|
|
|
{ |
18
|
35 |
|
$this->manager = $manager; |
19
|
|
|
|
20
|
35 |
|
$client = $manager->getClient(); |
21
|
35 |
|
foreach ($client->getSpace('property')->select([], 'space')->getData() as $property) { |
22
|
35 |
|
list($id, $spaceId, $line, $name, $type) = $property; |
|
|
|
|
23
|
35 |
|
if (!array_key_exists($spaceId, $this->property)) { |
24
|
35 |
|
$this->property[$spaceId] = []; |
25
|
35 |
|
$this->types[$spaceId] = []; |
26
|
|
|
} |
27
|
35 |
|
$this->property[$spaceId][$line] = $name; |
28
|
35 |
|
$this->types[$spaceId][$name] = $type; |
29
|
|
|
} |
30
|
35 |
|
foreach ($client->getSpace('_vindex')->select([], 'primary')->getData() as $index) { |
31
|
35 |
|
list($spaceId, $num, $name, $type, $params, $properties) = $index; |
|
|
|
|
32
|
35 |
|
if (!array_key_exists($spaceId, $this->property)) { |
33
|
|
|
// tarantool space index |
34
|
35 |
|
continue; |
35
|
|
|
} |
36
|
35 |
|
if (!isset($this->indexes[$spaceId])) { |
37
|
35 |
|
$this->indexes[$spaceId] = []; |
38
|
|
|
} |
39
|
35 |
|
$this->indexes[$spaceId][$num] = []; |
40
|
35 |
|
foreach ($properties as $row) { |
41
|
35 |
|
list($part, $type) = $row; |
|
|
|
|
42
|
35 |
|
$this->indexes[$spaceId][$num][] = $this->property[$spaceId][$part]; |
43
|
|
|
} |
44
|
|
|
} |
45
|
35 |
|
foreach ($this->property as $spaceId => $collection) { |
46
|
35 |
|
ksort($collection); |
47
|
35 |
|
$this->property[$spaceId] = $collection; |
48
|
|
|
} |
49
|
35 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return Type |
53
|
|
|
*/ |
54
|
35 |
|
public function get($type) |
55
|
|
|
{ |
56
|
35 |
|
if (!array_key_exists($type, $this->types)) { |
57
|
35 |
|
$spaceId = $this->manager->getSchema()->getSpaceId($type); |
58
|
35 |
|
if (!$spaceId) { |
59
|
1 |
|
throw new LogicException("Type $type not exists"); |
60
|
|
|
} |
61
|
|
|
|
62
|
35 |
|
$this->types[$type] = new Type( |
63
|
35 |
|
$this->manager, $type, |
64
|
35 |
|
$this->property[$spaceId], |
65
|
35 |
|
$this->types[$spaceId], |
66
|
35 |
|
$this->indexes[$spaceId] |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
35 |
|
return $this->types[$type]; |
71
|
|
|
} |
72
|
|
|
|
73
|
3 |
|
public function has($type) |
74
|
|
|
{ |
75
|
|
|
// was created |
76
|
3 |
|
if (array_key_exists($type, $this->types)) { |
77
|
1 |
|
return true; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// can be created |
81
|
3 |
|
$spaceId = $this->manager->getSchema()->getSpaceId($type); |
82
|
3 |
|
if (array_key_exists($spaceId, $this->property)) { |
83
|
1 |
|
return true; |
84
|
|
|
} |
85
|
|
|
|
86
|
3 |
|
return false; |
87
|
|
|
} |
88
|
|
|
|
89
|
3 |
|
public function remove($type) |
90
|
|
|
{ |
91
|
3 |
|
$other = $this->manager->get('property')->find(['type' => $type]); |
|
|
|
|
92
|
3 |
|
if (count($other)) { |
93
|
1 |
|
$name = $this->manager->getSchema()->getSpaceName($other[0]->space); |
94
|
1 |
|
throw new Exception("Space $name references ".$type); |
95
|
|
|
} |
96
|
2 |
|
$instance = $this->get($type); |
97
|
2 |
|
$rows = $instance->getSpace()->select([])->getData(); |
98
|
2 |
|
if (count($rows)) { |
99
|
1 |
|
throw new Exception("Can't remove non-empty space $type"); |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
foreach (array_reverse($instance->getProperties()) as $property) { |
103
|
1 |
|
$instance->removeProperty($property); |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
foreach (array_keys($instance->getIndexes()) as $index) { |
107
|
1 |
|
$instance->dropIndex($index); |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
$sq = $this->manager->get('sequence')->findOne(['space' => $instance->getSpaceId()]); |
|
|
|
|
111
|
1 |
|
if ($sq) { |
112
|
1 |
|
$this->manager->remove($sq); |
113
|
1 |
|
$this->manager->get('sequence')->flushCache(); |
114
|
|
|
} |
115
|
|
|
|
116
|
1 |
|
$this->manager->getSchema()->dropSpace($type); |
117
|
1 |
|
unset($this->types[$type]); |
118
|
|
|
|
119
|
1 |
|
$this->manager->forgetRepository($type); |
120
|
1 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return Type |
124
|
|
|
*/ |
125
|
35 |
|
public function create($type, array $fields = null) |
126
|
|
|
{ |
127
|
35 |
|
if ($this->manager->getSchema()->hasSpace($type)) { |
128
|
1 |
|
throw new LogicException("Type $type exists"); |
129
|
|
|
} |
130
|
|
|
|
131
|
35 |
|
$this->manager->getSchema()->createSpace($type); |
132
|
|
|
|
133
|
35 |
|
$instance = new Type($this->manager, $type, [], [], []); |
134
|
|
|
|
135
|
35 |
|
$instance->addProperty('id'); |
136
|
35 |
|
$instance->addIndex('id'); |
137
|
|
|
|
138
|
35 |
|
if ($fields) { |
139
|
35 |
|
foreach ($fields as $index => $field) { |
140
|
35 |
|
if ($field instanceof Contracts\Type) { |
141
|
4 |
|
if (!is_numeric($index)) { |
142
|
1 |
|
$instance->reference($field, $index); |
143
|
|
|
} else { |
144
|
4 |
|
$instance->reference($field); |
145
|
|
|
} |
146
|
|
|
} else { |
147
|
35 |
|
$instance->addProperty($field); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
35 |
|
$this->types[$type] = $instance; |
152
|
|
|
|
153
|
35 |
|
return $instance; |
154
|
|
|
} |
155
|
|
|
|
156
|
1 |
|
public function setConvention(Contracts\Convention $convention) |
157
|
|
|
{ |
158
|
1 |
|
$this->convention = $convention; |
|
|
|
|
159
|
|
|
|
160
|
1 |
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
35 |
|
public function getConvention() |
164
|
|
|
{ |
165
|
35 |
|
if (!isset($this->convention)) { |
166
|
35 |
|
$this->convention = new Convention(); |
167
|
|
|
} |
168
|
|
|
|
169
|
35 |
|
return $this->convention; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
This checks looks for assignemnts to variables using the
list(...)
function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$a
and$c
are used. There was no need to assign$b
.Instead, the list call could have been.