1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cycle\Schema\Renderer\MermaidRenderer; |
6
|
|
|
|
7
|
|
|
use Cycle\ORM\Relation as SchemaRelation; |
8
|
|
|
use Cycle\ORM\SchemaInterface; |
9
|
|
|
use Cycle\Schema\Renderer\MermaidRenderer\Entity\EntityRelation; |
10
|
|
|
use Cycle\Schema\Renderer\MermaidRenderer\Entity\EntityTable; |
11
|
|
|
use Cycle\Schema\Renderer\MermaidRenderer\Method\BelongsToMethod; |
12
|
|
|
use Cycle\Schema\Renderer\MermaidRenderer\Method\BelongsToMorphedMethod; |
13
|
|
|
use Cycle\Schema\Renderer\MermaidRenderer\Method\EmbeddedMethod; |
14
|
|
|
use Cycle\Schema\Renderer\MermaidRenderer\Method\HasManyMethod; |
15
|
|
|
use Cycle\Schema\Renderer\MermaidRenderer\Method\HasOneMethod; |
16
|
|
|
use Cycle\Schema\Renderer\MermaidRenderer\Method\ManyToManyMethod; |
17
|
|
|
use Cycle\Schema\Renderer\MermaidRenderer\Method\MorphedHasManyMethod; |
18
|
|
|
use Cycle\Schema\Renderer\MermaidRenderer\Method\MorphedHasOneMethod; |
19
|
|
|
use Cycle\Schema\Renderer\MermaidRenderer\Method\RefersToMethod; |
20
|
|
|
use Cycle\Schema\Renderer\MermaidRenderer\Relation\BelongsToMorphedRelation; |
21
|
|
|
use Cycle\Schema\Renderer\MermaidRenderer\Relation\BelongsToRelation; |
22
|
|
|
use Cycle\Schema\Renderer\MermaidRenderer\Relation\EmbeddedRelation; |
23
|
|
|
use Cycle\Schema\Renderer\MermaidRenderer\Relation\HasManyRelation; |
24
|
|
|
use Cycle\Schema\Renderer\MermaidRenderer\Relation\HasOneRelation; |
25
|
|
|
use Cycle\Schema\Renderer\MermaidRenderer\Relation\JoinedInheritanceRelation; |
26
|
|
|
use Cycle\Schema\Renderer\MermaidRenderer\Relation\ManyToManyRelation; |
27
|
|
|
use Cycle\Schema\Renderer\MermaidRenderer\Relation\MorphedHasManyRelation; |
28
|
|
|
use Cycle\Schema\Renderer\MermaidRenderer\Relation\MorphedHasOneRelation; |
29
|
|
|
use Cycle\Schema\Renderer\MermaidRenderer\Relation\RefersToRelation; |
30
|
|
|
use Cycle\Schema\Renderer\MermaidRenderer\Relation\SingleInheritanceRelation; |
31
|
|
|
use Cycle\Schema\Renderer\MermaidRenderer\Schema\ClassDiagram; |
32
|
|
|
use Cycle\Schema\Renderer\SchemaConstants; |
33
|
|
|
use Cycle\Schema\Renderer\SchemaRenderer; |
34
|
|
|
|
35
|
|
|
final class MermaidRenderer implements SchemaRenderer |
36
|
|
|
{ |
37
|
|
|
use StringFormatter; |
38
|
|
|
|
39
|
|
|
public function render(array $schema): string |
40
|
|
|
{ |
41
|
|
|
$class = new ClassDiagram(); |
42
|
|
|
$roleAliasCollection = new RoleAliasCollection($schema); |
43
|
|
|
|
44
|
|
|
$constants = (new SchemaConstants())->all(); |
45
|
|
|
|
46
|
|
|
foreach ($schema as $key => $value) { |
47
|
|
|
if (!isset($value[SchemaInterface::COLUMNS])) { |
48
|
|
|
continue; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$role = $roleAliasCollection->getAlias($key); |
52
|
|
|
|
53
|
|
|
$entityTable = new EntityTable($role); |
54
|
|
|
$entityRelation = new EntityRelation(); |
55
|
|
|
|
56
|
|
|
if (\strpos($key, ':') !== false) { |
57
|
|
|
$entityTable->addAnnotation( |
58
|
|
|
new Annotation($key) |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
foreach ($value[SchemaInterface::COLUMNS] as $column) { |
63
|
|
|
$entityTable->addColumn( |
64
|
|
|
new Column($value[SchemaInterface::TYPECAST][$column] ?? 'string', $column) |
|
|
|
|
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
foreach ($value[SchemaInterface::RELATIONS] ?? [] as $relationKey => $relation) { |
69
|
|
|
if (!isset($relation[SchemaRelation::TARGET], $relation[SchemaRelation::SCHEMA])) { |
70
|
|
|
continue; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$target = $roleAliasCollection->getAlias($relation[SchemaRelation::TARGET]); |
74
|
|
|
|
75
|
|
|
if (\class_exists($target)) { |
76
|
|
|
$target = \lcfirst($this->getClassShortName($target)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$isNullable = $relation[SchemaRelation::SCHEMA][SchemaRelation::NULLABLE] ?? false; |
80
|
|
|
|
81
|
|
|
switch ($relation[SchemaRelation::TYPE]) { |
82
|
|
|
case SchemaRelation::HAS_ONE: |
83
|
|
|
$entityTable->addMethod( |
84
|
|
|
new HasOneMethod($relationKey, $target) |
85
|
|
|
); |
86
|
|
|
$entityRelation->addRelation( |
87
|
|
|
new HasOneRelation($role, $target, $relationKey, $isNullable) |
88
|
|
|
); |
89
|
|
|
break; |
90
|
|
|
case SchemaRelation::HAS_MANY: |
91
|
|
|
$entityTable->addMethod( |
92
|
|
|
new HasManyMethod($relationKey, $target) |
93
|
|
|
); |
94
|
|
|
$entityRelation->addRelation( |
95
|
|
|
new HasManyRelation($role, $target, $relationKey, $isNullable) |
96
|
|
|
); |
97
|
|
|
break; |
98
|
|
|
case SchemaRelation::BELONGS_TO: |
99
|
|
|
$entityTable->addMethod( |
100
|
|
|
new BelongsToMethod($relationKey, $target) |
101
|
|
|
); |
102
|
|
|
$entityRelation->addRelation( |
103
|
|
|
new BelongsToRelation($role, $target, $relationKey, $isNullable) |
104
|
|
|
); |
105
|
|
|
break; |
106
|
|
|
case SchemaRelation::MANY_TO_MANY: |
107
|
|
|
$throughEntity = $relation[SchemaRelation::SCHEMA][SchemaRelation::THROUGH_ENTITY] ?? null; |
108
|
|
|
|
109
|
|
|
if ($throughEntity !== null) { |
110
|
|
|
if (\class_exists($throughEntity)) { |
111
|
|
|
$throughEntity = \lcfirst($this->getClassShortName($throughEntity)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$entityTable->addMethod( |
115
|
|
|
new ManyToManyMethod($relationKey, $target) |
116
|
|
|
); |
117
|
|
|
$entityRelation->addRelation( |
118
|
|
|
new ManyToManyRelation($role, $target, $relationKey, $isNullable) |
119
|
|
|
); |
120
|
|
|
$entityRelation->addRelation( |
121
|
|
|
new Relation($throughEntity, $role, "$role.$relationKey", '..>', $isNullable) |
122
|
|
|
); |
123
|
|
|
$entityRelation->addRelation( |
124
|
|
|
new Relation($throughEntity, $target, "$role.$relationKey", '..>', $isNullable) |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
break; |
128
|
|
|
case SchemaRelation::REFERS_TO: |
129
|
|
|
$entityTable->addMethod( |
130
|
|
|
new RefersToMethod($relationKey, $target) |
131
|
|
|
); |
132
|
|
|
$entityRelation->addRelation( |
133
|
|
|
new RefersToRelation($role, $target, $relationKey, $isNullable) |
134
|
|
|
); |
135
|
|
|
break; |
136
|
|
|
case SchemaRelation::MORPHED_HAS_MANY: |
137
|
|
|
$entityTable->addMethod( |
138
|
|
|
new MorphedHasManyMethod($relationKey, $target) |
139
|
|
|
); |
140
|
|
|
$entityRelation->addRelation( |
141
|
|
|
new MorphedHasManyRelation($role, $target, $relationKey, $isNullable) |
142
|
|
|
); |
143
|
|
|
break; |
144
|
|
|
case SchemaRelation::MORPHED_HAS_ONE: |
145
|
|
|
$entityTable->addMethod( |
146
|
|
|
new MorphedHasOneMethod($relationKey, $target) |
147
|
|
|
); |
148
|
|
|
$entityRelation->addRelation( |
149
|
|
|
new MorphedHasOneRelation($role, $target, $relationKey, $isNullable) |
150
|
|
|
); |
151
|
|
|
break; |
152
|
|
|
case SchemaRelation::BELONGS_TO_MORPHED: |
153
|
|
|
$entityTable->addMethod( |
154
|
|
|
new BelongsToMorphedMethod($relationKey, $target) |
155
|
|
|
); |
156
|
|
|
$entityRelation->addRelation( |
157
|
|
|
new BelongsToMorphedRelation($role, $target, $relationKey, $isNullable) |
158
|
|
|
); |
159
|
|
|
break; |
160
|
|
|
case SchemaRelation::EMBEDDED: |
161
|
|
|
$methodTarget = \explode('_', $target); |
162
|
|
|
$prop = isset($methodTarget[2]) ? $methodTarget[2] . '_prop' : $target; |
163
|
|
|
|
164
|
|
|
$entityTable->addMethod( |
165
|
|
|
new EmbeddedMethod($prop, $relation[SchemaRelation::TARGET]) |
166
|
|
|
); |
167
|
|
|
$entityRelation->addRelation( |
168
|
|
|
new EmbeddedRelation($role, $target, $prop, $isNullable) |
169
|
|
|
); |
170
|
|
|
break; |
171
|
|
|
default: |
172
|
|
|
throw new \ErrorException(\sprintf('Relation `%s` not found.', $relation[SchemaRelation::TYPE])); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
foreach ($value[SchemaInterface::CHILDREN] ?? [] as $children) { |
177
|
|
|
$entityRelation->addRelation( |
178
|
|
|
new SingleInheritanceRelation($role, $children, false) |
179
|
|
|
); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
if (isset($constants['PARENT'], $value[$constants['PARENT']])) { |
183
|
|
|
$entityRelation->addRelation( |
184
|
|
|
new JoinedInheritanceRelation($value[$constants['PARENT']], $role, false) |
185
|
|
|
); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
$class->addEntity($entityTable); |
189
|
|
|
$class->addEntity($entityRelation); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return (string)$class; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|