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
|
27 |
|
public function __construct(Contracts\Manager $manager) |
16
|
|
|
{ |
17
|
27 |
|
$this->manager = $manager; |
18
|
|
|
|
19
|
27 |
|
$client = $manager->getClient(); |
20
|
27 |
|
foreach ($client->getSpace('property')->select([], 'space')->getData() as $property) { |
21
|
27 |
|
list($id, $spaceId, $line, $name, $type) = $property; |
|
|
|
|
22
|
27 |
|
if (!array_key_exists($spaceId, $this->property)) { |
23
|
27 |
|
$this->property[$spaceId] = []; |
24
|
27 |
|
$this->types[$spaceId] = []; |
25
|
|
|
} |
26
|
27 |
|
$this->property[$spaceId][$line] = $name; |
27
|
27 |
|
$this->types[$spaceId][$name] = $type; |
28
|
|
|
} |
29
|
27 |
|
foreach ($client->getSpace('_vindex')->select([], 'primary')->getData() as $index) { |
30
|
27 |
|
list($spaceId, $num, $name, $type, $params, $properties) = $index; |
|
|
|
|
31
|
27 |
|
if (!array_key_exists($spaceId, $this->property)) { |
32
|
|
|
// tarantool space index |
33
|
27 |
|
continue; |
34
|
|
|
} |
35
|
27 |
|
if (!isset($this->indexes[$spaceId])) { |
36
|
27 |
|
$this->indexes[$spaceId] = []; |
37
|
|
|
} |
38
|
27 |
|
$this->indexes[$spaceId][$num] = []; |
39
|
27 |
|
foreach ($properties as $row) { |
40
|
27 |
|
list($part, $type) = $row; |
|
|
|
|
41
|
27 |
|
$this->indexes[$spaceId][$num][] = $this->property[$spaceId][$part]; |
42
|
|
|
} |
43
|
|
|
} |
44
|
27 |
|
foreach ($this->property as $spaceId => $collection) { |
45
|
27 |
|
ksort($collection); |
46
|
27 |
|
$this->property[$spaceId] = $collection; |
47
|
|
|
} |
48
|
27 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return Type |
52
|
|
|
*/ |
53
|
27 |
|
public function get($type) |
54
|
|
|
{ |
55
|
27 |
|
if (!array_key_exists($type, $this->types)) { |
56
|
27 |
|
$spaceId = $this->manager->getSchema()->getSpaceId($type); |
57
|
27 |
|
if (!$spaceId) { |
58
|
1 |
|
throw new LogicException("Type $type not exists"); |
59
|
|
|
} |
60
|
|
|
|
61
|
27 |
|
$this->types[$type] = new Type( |
62
|
27 |
|
$this->manager, $type, |
63
|
27 |
|
$this->property[$spaceId], |
64
|
27 |
|
$this->types[$spaceId], |
65
|
27 |
|
$this->indexes[$spaceId] |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
27 |
|
return $this->types[$type]; |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
public function has($type) |
73
|
|
|
{ |
74
|
|
|
// was created |
75
|
2 |
|
if (array_key_exists($type, $this->types)) { |
76
|
1 |
|
return true; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// can be created |
80
|
2 |
|
$spaceId = $this->manager->getSchema()->getSpaceId($type); |
81
|
2 |
|
if (array_key_exists($spaceId, $this->property)) { |
82
|
1 |
|
return true; |
83
|
|
|
} |
84
|
|
|
|
85
|
2 |
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return Type |
90
|
|
|
*/ |
91
|
27 |
|
public function create($type, array $fields = null) |
92
|
|
|
{ |
93
|
27 |
|
if ($this->manager->getSchema()->hasSpace($type)) { |
94
|
1 |
|
throw new LogicException("Type $type exists"); |
95
|
|
|
} |
96
|
|
|
|
97
|
27 |
|
$this->manager->getSchema()->createSpace($type); |
98
|
|
|
|
99
|
27 |
|
$instance = new Type($this->manager, $type, [], [], []); |
100
|
|
|
|
101
|
27 |
|
$instance->addProperty('id'); |
102
|
27 |
|
$instance->addIndex('id'); |
103
|
|
|
|
104
|
27 |
|
if ($fields) { |
105
|
27 |
|
foreach ($fields as $index => $field) { |
106
|
27 |
|
if ($field instanceof Contracts\Type) { |
107
|
2 |
|
if (!is_numeric($index)) { |
108
|
1 |
|
$instance->reference($field, $index); |
109
|
|
|
} else { |
110
|
2 |
|
$instance->reference($field); |
111
|
|
|
} |
112
|
|
|
} else { |
113
|
27 |
|
$instance->addProperty($field); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
27 |
|
return $this->types[$type] = $instance; |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
public function setConvention(Contracts\Convention $convention) |
122
|
|
|
{ |
123
|
1 |
|
$this->convention = $convention; |
|
|
|
|
124
|
|
|
|
125
|
1 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
27 |
|
public function getConvention() |
129
|
|
|
{ |
130
|
27 |
|
if (!isset($this->convention)) { |
131
|
27 |
|
$this->convention = new Convention(); |
132
|
|
|
} |
133
|
|
|
|
134
|
27 |
|
return $this->convention; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
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.