1 | <?php |
||
9 | class Meta implements Contracts\Meta |
||
10 | { |
||
11 | protected $manager; |
||
12 | protected $property = []; |
||
13 | protected $indexes = []; |
||
14 | protected $types = []; |
||
15 | |||
16 | 62 | public function __construct(Contracts\Manager $manager) |
|
17 | { |
||
18 | 62 | $this->manager = $manager; |
|
19 | |||
20 | 62 | $client = $manager->getClient(); |
|
21 | 62 | foreach ($client->getSpace('property')->select([], 'space')->getData() as $property) { |
|
22 | 62 | list($id, $spaceId, $line, $name, $type) = $property; |
|
|
|||
23 | 62 | if (!array_key_exists($spaceId, $this->property)) { |
|
24 | 62 | $this->property[$spaceId] = []; |
|
25 | 62 | $this->types[$spaceId] = []; |
|
26 | 62 | } |
|
27 | 62 | $this->property[$spaceId][$line] = $name; |
|
28 | 62 | $this->types[$spaceId][$name] = $type; |
|
29 | 62 | } |
|
30 | 62 | foreach ($client->getSpace('_vindex')->select([], 'primary')->getData() as $index) { |
|
31 | 62 | list($spaceId, $num, $name, $type, $params, $properties) = $index; |
|
32 | 62 | if (!array_key_exists($spaceId, $this->property)) { |
|
33 | // tarantool space index |
||
34 | 62 | continue; |
|
35 | } |
||
36 | 62 | if (!isset($this->indexes[$spaceId])) { |
|
37 | 62 | $this->indexes[$spaceId] = []; |
|
38 | 62 | } |
|
39 | 62 | $this->indexes[$spaceId][$num] = []; |
|
40 | 62 | foreach ($properties as $row) { |
|
41 | 62 | list($part, $type) = $row; |
|
42 | 62 | $this->indexes[$spaceId][$num][] = $this->property[$spaceId][$part]; |
|
43 | 62 | } |
|
44 | 62 | } |
|
45 | 62 | foreach ($this->property as $spaceId => $collection) { |
|
46 | 62 | ksort($collection); |
|
47 | 62 | $this->property[$spaceId] = $collection; |
|
48 | 62 | } |
|
49 | 62 | } |
|
50 | |||
51 | /** |
||
52 | * @return Type |
||
53 | */ |
||
54 | 62 | public function get($type) |
|
55 | { |
||
56 | 62 | if (!array_key_exists($type, $this->types)) { |
|
57 | 62 | $spaceId = $this->manager->getSchema()->getSpaceId($type); |
|
58 | 62 | if (!$spaceId) { |
|
59 | 1 | throw new LogicException("Type $type not exists"); |
|
60 | } |
||
61 | |||
62 | 62 | $this->types[$type] = new Type( |
|
63 | 62 | $this->manager, $type, |
|
64 | 62 | $this->property[$spaceId], |
|
65 | 62 | $this->types[$spaceId], |
|
66 | 62 | $this->indexes[$spaceId] |
|
67 | 62 | ); |
|
68 | 62 | } |
|
69 | |||
70 | 62 | return $this->types[$type]; |
|
71 | } |
||
72 | |||
73 | 4 | public function has($type) |
|
88 | |||
89 | 4 | public function remove($type) |
|
90 | { |
||
91 | 4 | $other = $this->manager->get('property')->find(['type' => $type]); |
|
92 | 4 | if (count($other)) { |
|
93 | 1 | $name = $this->manager->getSchema()->getSpaceName($other[0]->space); |
|
94 | 1 | throw new Exception("Space $name references ".$type); |
|
95 | } |
||
96 | 3 | $instance = $this->get($type); |
|
97 | 3 | $rows = $instance->getSpace()->select([])->getData(); |
|
98 | 3 | if (count($rows)) { |
|
99 | 1 | throw new Exception("Can't remove non-empty space $type"); |
|
100 | } |
||
101 | |||
102 | 2 | $indexes = array_reverse(array_keys($instance->getIndexes())); |
|
103 | 2 | foreach ($indexes as $index) { |
|
104 | 2 | $instance->dropIndex($index); |
|
105 | 2 | } |
|
106 | |||
107 | 2 | foreach (array_reverse($instance->getProperties()) as $property) { |
|
108 | 2 | $instance->removeProperty($property); |
|
109 | 2 | } |
|
110 | |||
111 | 2 | $sq = $this->manager->get('sequence')->findOne(['space' => $instance->getSpaceId()]); |
|
112 | 2 | if ($sq) { |
|
113 | 1 | $this->manager->remove($sq); |
|
114 | 1 | $this->manager->get('sequence')->flushCache(); |
|
115 | 1 | } |
|
116 | |||
117 | 2 | $this->manager->getSchema()->dropSpace($type); |
|
118 | 2 | unset($this->types[$type]); |
|
119 | |||
120 | 2 | $this->manager->forgetRepository($type); |
|
121 | 2 | } |
|
122 | |||
123 | /** |
||
124 | * @return Type |
||
125 | */ |
||
126 | 62 | public function create($type, array $fields = null) |
|
127 | { |
||
128 | 62 | if ($this->manager->getSchema()->hasSpace($type)) { |
|
129 | 1 | throw new LogicException("Type $type exists"); |
|
130 | } |
||
131 | |||
132 | 62 | $this->manager->getSchema()->createSpace($type); |
|
133 | |||
134 | 62 | $instance = new Type($this->manager, $type, [], [], []); |
|
135 | |||
136 | 62 | $instance->addProperty('id'); |
|
137 | 62 | $instance->addIndex('id'); |
|
138 | |||
139 | 62 | if ($fields) { |
|
140 | 62 | foreach ($fields as $index => $field) { |
|
141 | 62 | if ($field instanceof Contracts\Type) { |
|
142 | 5 | if (!is_numeric($index)) { |
|
143 | 1 | $instance->reference($field, $index); |
|
144 | 1 | } else { |
|
145 | 4 | $instance->reference($field); |
|
146 | } |
||
147 | 5 | } else { |
|
148 | 62 | $instance->addProperty($field); |
|
149 | } |
||
150 | 62 | } |
|
151 | 62 | } |
|
152 | 62 | $this->types[$type] = $instance; |
|
153 | |||
154 | 62 | return $instance; |
|
155 | } |
||
156 | |||
157 | 1 | public function setConvention(Contracts\Convention $convention) |
|
163 | |||
164 | 62 | public function getConvention() |
|
165 | { |
||
166 | 62 | if (!isset($this->convention)) { |
|
167 | 62 | $this->convention = new Convention(); |
|
172 | } |
||
173 |
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.