-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCompiler.php
243 lines (210 loc) · 6.74 KB
/
Compiler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
/**
* Cycle ORM Schema Builder.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/
declare(strict_types=1);
namespace Cycle\Schema;
use Cycle\ORM\Mapper\Mapper;
use Cycle\ORM\Schema;
use Cycle\ORM\Select\Repository;
use Cycle\ORM\Select\Source;
use Cycle\Schema\Definition\Comparator\FieldComparator;
use Cycle\Schema\Definition\Entity;
use Cycle\Schema\Definition\Field;
use Spiral\Database\Exception\CompilerException;
final class Compiler
{
/** @var array */
private $result = [];
/** @var array */
private $defaults = [
Schema::MAPPER => Mapper::class,
Schema::REPOSITORY => Repository::class,
Schema::SOURCE => Source::class,
Schema::CONSTRAIN => null,
];
/** @var \Doctrine\Inflector\Inflector */
private $inflector;
public function __construct()
{
$this->inflector = (new \Doctrine\Inflector\Rules\English\InflectorFactory())->build();
}
/**
* Compile the registry schema.
*
* @param Registry $registry
* @param GeneratorInterface[] $generators
* @param array $defaults
* @return array
*
*/
public function compile(Registry $registry, array $generators = [], array $defaults = []): array
{
$this->defaults = $defaults + $this->defaults;
foreach ($generators as $generator) {
if (!$generator instanceof GeneratorInterface) {
throw new CompilerException(sprintf(
'Invalid generator `%s`',
is_object($generator) ? get_class($generator) : gettype($generator)
));
}
$registry = $generator->run($registry);
}
foreach ($registry->getIterator() as $entity) {
if ($this->getPrimary($entity) === null) {
// incomplete entity, skip
continue;
}
$this->compute($registry, $entity);
}
return $this->result;
}
/**
* Get compiled schema result.
*
* @return array
*/
public function getSchema(): array
{
return $this->result;
}
/**
* Compile entity and relation definitions into packed ORM schema.
*
* @param Registry $registry
* @param Entity $entity
*/
protected function compute(Registry $registry, Entity $entity): void
{
$schema = [
Schema::ENTITY => $entity->getClass(),
Schema::SOURCE => $entity->getSource() ?? $this->defaults[Schema::SOURCE],
Schema::MAPPER => $entity->getMapper() ?? $this->defaults[Schema::MAPPER],
Schema::REPOSITORY => $entity->getRepository() ?? $this->defaults[Schema::REPOSITORY],
Schema::CONSTRAIN => $entity->getConstrain() ?? $this->defaults[Schema::CONSTRAIN],
Schema::SCHEMA => $entity->getSchema(),
Schema::PRIMARY_KEY => $this->getPrimary($entity),
Schema::COLUMNS => $this->renderColumns($entity),
Schema::FIND_BY_KEYS => $this->renderReferences($entity),
Schema::TYPECAST => $this->renderTypecast($entity),
Schema::RELATIONS => $this->renderRelations($registry, $entity)
];
if ($registry->hasTable($entity)) {
$schema[Schema::DATABASE] = $registry->getDatabase($entity);
$schema[Schema::TABLE] = $registry->getTable($entity);
}
// table inheritance
foreach ($registry->getChildren($entity) as $child) {
$this->result[$child->getClass()] = [Schema::ROLE => $entity->getRole()];
$schema[Schema::CHILDREN][$this->childAlias($child)] = $child->getClass();
}
ksort($schema);
$this->result[$entity->getRole()] = $schema;
}
/**
* @param Entity $entity
* @return array
*/
protected function renderColumns(Entity $entity): array
{
// Check field duplicates
/** @var Field[][] $fieldGroups */
$fieldGroups = [];
// Collect and group fields by column name
foreach ($entity->getFields() as $name => $field) {
$fieldGroups[$field->getColumn()][$name] = $field;
}
foreach ($fieldGroups as $fieldName => $fields) {
// We need duplicates only
if (count($fields) === 1) {
continue;
}
// Compare
$comparator = new FieldComparator();
foreach ($fields as $name => $field) {
$comparator->addField($name, $field);
}
try {
$comparator->compare();
} catch (\Throwable $e) {
throw new Exception\CompilerException(
sprintf("Error compiling the `%s` role.\n\n%s", $entity->getRole(), $e->getMessage()),
$e->getCode()
);
}
}
$schema = [];
foreach ($entity->getFields() as $name => $field) {
$schema[$name] = $field->getColumn();
}
return $schema;
}
/**
* @param Entity $entity
* @return array
*/
protected function renderTypecast(Entity $entity): array
{
$schema = [];
foreach ($entity->getFields() as $name => $field) {
if ($field->hasTypecast()) {
$schema[$name] = $field->getTypecast();
}
}
return $schema;
}
/**
* @param Entity $entity
* @return array
*/
protected function renderReferences(Entity $entity): array
{
$schema = [$this->getPrimary($entity)];
foreach ($entity->getFields() as $name => $field) {
if ($field->isReferenced()) {
$schema[] = $name;
}
}
return array_unique($schema);
}
/**
* @param Registry $registry
* @param Entity $entity
* @return array
*/
protected function renderRelations(Registry $registry, Entity $entity): array
{
$result = [];
foreach ($registry->getRelations($entity) as $name => $relation) {
$result[$name] = $relation->packSchema();
}
return $result;
}
/**
* @param Entity $entity
* @return string|null
*/
protected function getPrimary(Entity $entity): ?string
{
foreach ($entity->getFields() as $name => $field) {
if ($field->isPrimary()) {
return $name;
}
}
return null;
}
/**
* Return the unique alias for the child entity.
*
* @param Entity $entity
* @return string
*/
protected function childAlias(Entity $entity): string
{
$r = new \ReflectionClass($entity->getClass());
return $this->inflector->classify($r->getShortName());
}
}