1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Spiral Framework. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Anton Titov (Wolfy-J) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Cycle\Schema; |
11
|
|
|
|
12
|
|
|
use Cycle\Schema\Definition\Entity; |
13
|
|
|
use Cycle\Schema\Exception\BuilderException; |
14
|
|
|
use Spiral\Database\DatabaseManager; |
15
|
|
|
use Spiral\Database\Exception\DBALException; |
16
|
|
|
|
17
|
|
|
class Builder |
18
|
|
|
{ |
19
|
|
|
/** @var DatabaseManager */ |
20
|
|
|
private $dbal; |
21
|
|
|
|
22
|
|
|
/** @var Entity[] */ |
23
|
|
|
private $entities = []; |
24
|
|
|
|
25
|
|
|
/** @var \SplObjectStorage */ |
26
|
|
|
private $tables; |
27
|
|
|
|
28
|
|
|
/** @var \SplObjectStorage */ |
29
|
|
|
private $children; |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
private $relations; |
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param DatabaseManager $dbal |
36
|
|
|
*/ |
37
|
|
|
public function __construct(DatabaseManager $dbal) |
38
|
|
|
{ |
39
|
|
|
$this->dbal = $dbal; |
40
|
|
|
$this->tables = new \SplObjectStorage(); |
41
|
|
|
$this->children = new \SplObjectStorage(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param Entity $entity |
46
|
|
|
*/ |
47
|
|
|
public function register(Entity $entity) |
48
|
|
|
{ |
49
|
|
|
$this->entities[] = $entity; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $role |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
public function hasRole(string $role): bool |
57
|
|
|
{ |
58
|
|
|
foreach ($this->entities as $entity) { |
59
|
|
|
if ($entity->getRole() == $role) { |
60
|
|
|
return true; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param Entity $entity |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
public function hasEntity(Entity $entity): bool |
72
|
|
|
{ |
73
|
|
|
return array_search($entity, $this->entities, true) !== false; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get entity by it's role. |
78
|
|
|
* |
79
|
|
|
* @param string $role |
80
|
|
|
* @return Entity |
81
|
|
|
* |
82
|
|
|
* @throws BuilderException |
83
|
|
|
*/ |
84
|
|
|
public function getEntity(string $role): Entity |
85
|
|
|
{ |
86
|
|
|
foreach ($this->entities as $entity) { |
87
|
|
|
if ($entity->getRole() == $role || $entity->getClass() === $role) { |
88
|
|
|
return $entity; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
throw new BuilderException("Undefined entity `{$role}`"); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Assign child entity to parent entity. |
97
|
|
|
* |
98
|
|
|
* @param Entity $parent |
99
|
|
|
* @param Entity $child |
100
|
|
|
* |
101
|
|
|
* @throws BuilderException |
102
|
|
|
*/ |
103
|
|
|
public function registerChild(Entity $parent, Entity $child) |
104
|
|
|
{ |
105
|
|
|
if (!$this->hasEntity($parent)) { |
106
|
|
|
throw new BuilderException("Undefined entity `{$parent->getRole()}`"); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (!$this->children->contains($parent)) { |
110
|
|
|
$this->children[$parent] = []; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$this->children[$parent][] = $child; |
114
|
|
|
|
115
|
|
|
// merge parent and child schema |
116
|
|
|
$parent->merge($child); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get all assigned children entities. |
121
|
|
|
* |
122
|
|
|
* @param Entity $entity |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
public function getChildren(Entity $entity): array |
126
|
|
|
{ |
127
|
|
|
if (!$this->hasEntity($entity)) { |
128
|
|
|
throw new BuilderException("Undefined entity `{$entity->getRole()}`"); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if (!$this->children->contains($entity)) { |
132
|
|
|
return []; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $this->children[$entity]; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Associate entity with table. |
140
|
|
|
* |
141
|
|
|
* @param Entity $entity |
142
|
|
|
* @param string|null $database |
143
|
|
|
* @param string $table |
144
|
|
|
* |
145
|
|
|
* @throws BuilderException |
146
|
|
|
* @throws DBALException |
147
|
|
|
*/ |
148
|
|
|
public function linkTable(Entity $entity, ?string $database, string $table) |
149
|
|
|
{ |
150
|
|
|
if (!$this->hasEntity($entity)) { |
151
|
|
|
throw new BuilderException("Undefined entity `{$entity->getRole()}`"); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$this->tables[$entity] = $this->dbal->database($database)->table($table)->getSchema(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function getEntities() |
158
|
|
|
{ |
159
|
|
|
|
160
|
|
|
} |
161
|
|
|
} |