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\ORM\Schema; |
13
|
|
|
use Cycle\Schema\Definition\Entity; |
14
|
|
|
use Cycle\Schema\Exception\BuilderException; |
15
|
|
|
|
16
|
|
|
final class Compiler implements ProcessorInterface |
17
|
|
|
{ |
18
|
|
|
/** @var array */ |
19
|
|
|
private $result = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Get compiled schema result. |
23
|
|
|
* |
24
|
|
|
* @return array |
25
|
|
|
*/ |
26
|
|
|
public function getResult(): array |
27
|
|
|
{ |
28
|
|
|
return $this->result; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Compile entity and relation definitions into packed ORM schema. |
33
|
|
|
* |
34
|
|
|
* @param Registry $registry |
35
|
|
|
* @param Entity $entity |
36
|
|
|
*/ |
37
|
|
|
public function compute(Registry $registry, Entity $entity) |
38
|
|
|
{ |
39
|
|
|
$schema = [ |
40
|
|
|
Schema::ENTITY => $entity->getClass(), |
41
|
|
|
Schema::SOURCE => $entity->getSource(), |
42
|
|
|
Schema::MAPPER => $entity->getMapper(), |
43
|
|
|
Schema::REPOSITORY => $entity->getRepository(), |
44
|
|
|
Schema::CONSTRAIN => $entity->getConstrain(), |
45
|
|
|
Schema::COLUMNS => $this->renderColumns($entity), |
46
|
|
|
Schema::FIND_BY_KEYS => $this->renderReferences($entity), |
47
|
|
|
Schema::TYPECAST => $this->renderTypecast($entity), |
48
|
|
|
Schema::RELATIONS => $this->renderRelations($registry, $entity) |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
if ($registry->hasTable($entity)) { |
52
|
|
|
$primaryKeys = $registry->getTableSchema($entity)->getPrimaryKeys(); |
53
|
|
|
if (count($primaryKeys) !== 1) { |
54
|
|
|
throw new BuilderException("Entity `{$entity->getRole()}` must define exactly one primary key"); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$schema[Schema::DATABASE] = $registry->getDatabase($entity); |
58
|
|
|
$schema[Schema::TABLE] = $registry->getTable($entity); |
59
|
|
|
$schema[Schema::PRIMARY_KEY] = current($primaryKeys); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// table inheritance |
63
|
|
|
foreach ($registry->getChildren($entity) as $child) { |
64
|
|
|
$this->result[$child->getClass()] = [Schema::ROLE => $entity->getRole()]; |
65
|
|
|
$schema[Schema::CHILDREN][] = $child->getClass(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
ksort($schema); |
69
|
|
|
$this->result[$entity->getRole()] = $schema; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param Entity $entity |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
protected function renderColumns(Entity $entity): array |
77
|
|
|
{ |
78
|
|
|
$schema = []; |
79
|
|
|
foreach ($entity->getFields() as $name => $field) { |
80
|
|
|
$schema[$name] = $field->getColumn(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $schema; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param Entity $entity |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
protected function renderTypecast(Entity $entity): array |
91
|
|
|
{ |
92
|
|
|
$schema = []; |
93
|
|
|
foreach ($entity->getFields() as $name => $field) { |
94
|
|
|
if ($field->hasTypecast()) { |
95
|
|
|
$schema[$name] = $field->getTypecast(); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $schema; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param Entity $entity |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
protected function renderReferences(Entity $entity): array |
107
|
|
|
{ |
108
|
|
|
$schema = []; |
109
|
|
|
foreach ($entity->getFields() as $name => $field) { |
110
|
|
|
if ($field->isReferenced()) { |
111
|
|
|
$schema[] = $name; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $schema; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param Registry $registry |
120
|
|
|
* @param Entity $entity |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
protected function renderRelations(Registry $registry, Entity $entity): array |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
return []; |
126
|
|
|
} |
127
|
|
|
} |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.