|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tarantool\Mapper\Schema; |
|
4
|
|
|
|
|
5
|
|
|
use Tarantool\Mapper\Contracts; |
|
6
|
|
|
use LogicException; |
|
7
|
|
|
|
|
8
|
|
|
class Meta implements Contracts\Meta |
|
9
|
|
|
{ |
|
10
|
|
|
protected $manager; |
|
11
|
|
|
protected $property = []; |
|
12
|
|
|
protected $indexes = []; |
|
13
|
|
|
protected $types = []; |
|
14
|
|
|
|
|
15
|
26 |
|
public function __construct(Contracts\Manager $manager) |
|
16
|
|
|
{ |
|
17
|
26 |
|
$this->manager = $manager; |
|
18
|
|
|
|
|
19
|
26 |
|
$client = $manager->getClient(); |
|
20
|
26 |
|
foreach ($client->getSpace('property')->select([], 'space')->getData() as $property) { |
|
21
|
26 |
|
list($id, $spaceId, $line, $name, $type) = $property; |
|
|
|
|
|
|
22
|
26 |
|
if (!array_key_exists($spaceId, $this->property)) { |
|
23
|
26 |
|
$this->property[$spaceId] = []; |
|
24
|
26 |
|
$this->types[$spaceId] = []; |
|
25
|
|
|
} |
|
26
|
26 |
|
$this->property[$spaceId][$line] = $name; |
|
27
|
26 |
|
$this->types[$spaceId][$name] = $type; |
|
28
|
|
|
} |
|
29
|
26 |
|
foreach ($client->getSpace('_vindex')->select([], 'primary')->getData() as $index) { |
|
30
|
26 |
|
list($spaceId, $num, $name, $type, $params, $properties) = $index; |
|
|
|
|
|
|
31
|
26 |
|
if (!array_key_exists($spaceId, $this->property)) { |
|
32
|
|
|
// tarantool space index |
|
33
|
26 |
|
continue; |
|
34
|
|
|
} |
|
35
|
26 |
|
if (!isset($this->indexes[$spaceId])) { |
|
36
|
26 |
|
$this->indexes[$spaceId] = []; |
|
37
|
|
|
} |
|
38
|
26 |
|
$this->indexes[$spaceId][$name] = []; |
|
39
|
26 |
|
foreach ($properties as $row) { |
|
40
|
26 |
|
list($part, $type) = $row; |
|
|
|
|
|
|
41
|
26 |
|
$this->indexes[$spaceId][$name][] = $this->property[$spaceId][$part]; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
26 |
|
foreach ($this->property as $spaceId => $collection) { |
|
45
|
26 |
|
ksort($collection); |
|
46
|
26 |
|
$this->property[$spaceId] = $collection; |
|
47
|
|
|
} |
|
48
|
26 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return Type |
|
52
|
|
|
*/ |
|
53
|
26 |
|
public function get($type) |
|
54
|
|
|
{ |
|
55
|
26 |
|
if (!array_key_exists($type, $this->types)) { |
|
56
|
26 |
|
$spaceId = $this->manager->getSchema()->getSpaceId($type); |
|
57
|
26 |
|
if (!$spaceId) { |
|
58
|
1 |
|
throw new LogicException("Type $type not exists"); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
26 |
|
$this->types[$type] = new Type( |
|
62
|
26 |
|
$this->manager, $type, |
|
63
|
26 |
|
$this->property[$spaceId], |
|
64
|
26 |
|
$this->types[$spaceId], |
|
65
|
26 |
|
$this->indexes[$spaceId] |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
26 |
|
return $this->types[$type]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return Type |
|
74
|
|
|
*/ |
|
75
|
26 |
|
public function create($type, array $fields = null) |
|
76
|
|
|
{ |
|
77
|
26 |
|
if ($this->manager->getSchema()->hasSpace($type)) { |
|
78
|
1 |
|
throw new LogicException("Type $type exists"); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
26 |
|
$this->manager->getSchema()->createSpace($type); |
|
82
|
|
|
|
|
83
|
26 |
|
$instance = new Type($this->manager, $type, [], [], []); |
|
84
|
|
|
|
|
85
|
26 |
|
$instance->addProperty('id'); |
|
86
|
26 |
|
$instance->addIndex('id'); |
|
87
|
|
|
|
|
88
|
26 |
|
if ($fields) { |
|
89
|
26 |
|
foreach ($fields as $index => $field) { |
|
90
|
26 |
|
if ($field instanceof Contracts\Type) { |
|
91
|
2 |
|
if (!is_numeric($index)) { |
|
92
|
1 |
|
$instance->reference($field, $index); |
|
93
|
|
|
} else { |
|
94
|
2 |
|
$instance->reference($field); |
|
95
|
|
|
} |
|
96
|
|
|
} else { |
|
97
|
26 |
|
$instance->addProperty($field); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
26 |
|
return $this->types[$type] = $instance; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
1 |
|
public function setConvention(Contracts\Convention $convention) |
|
106
|
|
|
{ |
|
107
|
1 |
|
$this->convention = $convention; |
|
|
|
|
|
|
108
|
|
|
|
|
109
|
1 |
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
26 |
|
public function getConvention() |
|
113
|
|
|
{ |
|
114
|
26 |
|
if (!isset($this->convention)) { |
|
115
|
26 |
|
$this->convention = new Convention(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
26 |
|
return $this->convention; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
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
$aand$care used. There was no need to assign$b.Instead, the list call could have been.