1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\Yii\Cycle\Command; |
4
|
|
|
|
5
|
|
|
use Cycle\ORM\Relation; |
6
|
|
|
use Cycle\ORM\Schema; |
7
|
|
|
use Cycle\ORM\SchemaInterface; |
8
|
|
|
use Spiral\Database\DatabaseManager; |
9
|
|
|
use Spiral\Migrations\Config\MigrationConfig; |
10
|
|
|
use Spiral\Migrations\Migrator; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
use Yiisoft\Yii\Console\ExitCode; |
14
|
|
|
|
15
|
|
|
final class SchemaCommand extends BaseMigrationCommand |
16
|
|
|
{ |
17
|
|
|
protected static $defaultName = 'cycle/schema'; |
18
|
|
|
|
19
|
|
|
private SchemaInterface $schema; |
20
|
|
|
private const STR_RELATION = [ |
21
|
|
|
Relation::HAS_ONE => 'has one', |
22
|
|
|
Relation::HAS_MANY => 'has many', |
23
|
|
|
Relation::BELONGS_TO => 'belongs to', |
24
|
|
|
Relation::REFERS_TO => 'refers to', |
25
|
|
|
Relation::MANY_TO_MANY => 'many to many', |
26
|
|
|
Relation::BELONGS_TO_MORPHED => 'belongs to morphed', |
27
|
|
|
Relation::MORPHED_HAS_ONE => 'morphed has one', |
28
|
|
|
Relation::MORPHED_HAS_MANY => 'morphed has many', |
29
|
|
|
]; |
30
|
|
|
private const STR_PREFETCH_MODE = [ |
31
|
|
|
Relation::LOAD_PROMISE => 'promise', |
32
|
|
|
Relation::LOAD_EAGER => 'eager', |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
public function __construct( |
36
|
|
|
DatabaseManager $dbal, |
37
|
|
|
MigrationConfig $conf, |
38
|
|
|
Migrator $migrator, |
39
|
|
|
SchemaInterface $schema |
40
|
|
|
) { |
41
|
|
|
parent::__construct($dbal, $conf, $migrator); |
42
|
|
|
$this->schema = $schema; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function configure(): void |
46
|
|
|
{ |
47
|
|
|
$this->setDescription('Shown current schema'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
51
|
|
|
{ |
52
|
|
|
foreach ($this->schema->getRoles() as $role) { |
53
|
|
|
$output->write("<fg=magenta>[{$role}</>"); |
54
|
|
|
$alias = $this->schema->resolveAlias($role); |
55
|
|
|
// alias |
56
|
|
|
if ($alias !== null && $alias !== $role) { |
57
|
|
|
$output->write("=><fg=magenta>{$alias}</>"); |
58
|
|
|
} |
59
|
|
|
// table |
60
|
|
|
$output->write("<fg=magenta>]</>"); |
61
|
|
|
|
62
|
|
|
// database |
63
|
|
|
$database = $this->schema->define($role, Schema::DATABASE); |
64
|
|
|
$table = $this->schema->define($role, Schema::TABLE); |
65
|
|
|
if ($database !== null) { |
66
|
|
|
$output->write(" :: <fg=green>{$database}</>.<fg=green>{$table}</>"); |
67
|
|
|
} |
68
|
|
|
$output->writeln(''); |
69
|
|
|
|
70
|
|
|
// Entity |
71
|
|
|
$entity = $this->schema->define($role, Schema::ENTITY); |
72
|
|
|
$output->write(' Entity : '); |
73
|
|
|
$output->writeln($entity === null ? 'no entity' : "<fg=blue>{$entity}</>"); |
74
|
|
|
// Mapper |
75
|
|
|
$mapper = $this->schema->define($role, Schema::MAPPER); |
76
|
|
|
$output->write(' Mapper : '); |
77
|
|
|
$output->writeln($mapper === null ? 'no mapper' : "<fg=blue>{$mapper}</>"); |
78
|
|
|
// Constrain |
79
|
|
|
$constrain = $this->schema->define($role, Schema::CONSTRAIN); |
80
|
|
|
$output->write(' Constrain : '); |
81
|
|
|
$output->writeln($constrain === null ? 'no constrain' : "<fg=blue>{$constrain}</>"); |
82
|
|
|
// Repository |
83
|
|
|
$repository = $this->schema->define($role, Schema::REPOSITORY); |
84
|
|
|
$output->write(' Repository : '); |
85
|
|
|
$output->writeln($repository === null ? 'no repository' : "<fg=blue>{$repository}</>"); |
86
|
|
|
// PK |
87
|
|
|
$pk = $this->schema->define($role, Schema::PRIMARY_KEY); |
88
|
|
|
$output->write(' Primary key: '); |
89
|
|
|
$output->writeln($pk === null ? 'no primary key' : "<fg=green>{$pk}</>"); |
90
|
|
|
// Fields |
91
|
|
|
$columns = $this->schema->define($role, Schema::COLUMNS); |
92
|
|
|
$output->writeln(" Fields :"); |
93
|
|
|
$output->writeln(" (<fg=cyan>property</> -> <fg=green>db.field</> -> <fg=blue>typecast</>)"); |
94
|
|
|
$types = $this->schema->define($role, Schema::TYPECAST); |
95
|
|
|
foreach ($columns as $property => $field) { |
96
|
|
|
$typecast = $types[$property] ?? $types[$field] ?? null; |
97
|
|
|
$output->write(" <fg=cyan>{$property}</> -> <fg=green>{$field}</>"); |
98
|
|
|
if ($typecast !== null) { |
99
|
|
|
$output->write(" -> <fg=blue>{$typecast}</>"); |
100
|
|
|
} |
101
|
|
|
$output->writeln(''); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// Relations |
105
|
|
|
$relations = $this->schema->define($role, Schema::RELATIONS); |
106
|
|
|
if (count($relations) > 0) { |
107
|
|
|
$output->writeln(' Relations :'); |
108
|
|
|
foreach ($relations as $field => $relation) { |
109
|
|
|
$type = self::STR_RELATION[$relation[Relation::TYPE] ?? ''] ?? '?'; |
110
|
|
|
$target = $relation[Relation::TARGET] ?? '?'; |
111
|
|
|
$loading = self::STR_PREFETCH_MODE[$relation[Relation::LOAD] ?? ''] ?? '?'; |
112
|
|
|
$relSchema = $relation[Relation::SCHEMA]; |
113
|
|
|
$innerKey = $relSchema[Relation::INNER_KEY] ?? '?'; |
114
|
|
|
$outerKey = $relSchema[Relation::OUTER_KEY] ?? '?'; |
115
|
|
|
$where = $relSchema[Relation::WHERE] ?? []; |
116
|
|
|
$cascade = $relSchema[Relation::CASCADE] ?? null; |
117
|
|
|
$cascadeStr = $cascade ? 'cascaded' : 'not cascaded'; |
118
|
|
|
$nullable = $relSchema[Relation::NULLABLE] ?? null; |
119
|
|
|
$nullableStr = $nullable ? 'nullable' : ($nullable === false ? 'not null' : 'n/a'); |
120
|
|
|
$morphKey = $relSchema[Relation::MORPH_KEY] ?? null; |
121
|
|
|
// Many-To-Many relation(s) options |
122
|
|
|
$mmInnerKey = $relSchema[Relation::THROUGH_INNER_KEY] ?? '?'; |
123
|
|
|
$mmOuterKey = $relSchema[Relation::THROUGH_OUTER_KEY] ?? '?'; |
124
|
|
|
$mmEntity = $relSchema[Relation::THROUGH_ENTITY] ?? null; |
125
|
|
|
$mmWhere = $relSchema[Relation::THROUGH_WHERE] ?? []; |
126
|
|
|
// print |
127
|
|
|
$output->write( |
128
|
|
|
" <fg=magenta>{$role}</>-><fg=cyan>{$field}</> " |
129
|
|
|
. "{$type} <fg=magenta>{$target}</> {$loading} load" |
130
|
|
|
); |
131
|
|
|
if ($morphKey !== null) { |
132
|
|
|
$output->writeln(" Morphed key: <fg=green>{$morphKey}</>"); |
133
|
|
|
} |
134
|
|
|
$output->writeln(" <fg=yellow>{$cascadeStr}</>"); |
135
|
|
|
$output->write(" {$nullableStr} <fg=green>{$table}</>.<fg=green>{$innerKey}</> <="); |
136
|
|
|
if ($mmEntity !== null) { |
137
|
|
|
$output->write(" <fg=magenta>{$mmEntity}</>.<fg=green>{$mmInnerKey}</>"); |
138
|
|
|
$output->write("|"); |
139
|
|
|
$output->write("<fg=magenta>{$mmEntity}</>.<fg=green>{$mmOuterKey}</> "); |
140
|
|
|
} |
141
|
|
|
$output->writeln("=> <fg=magenta>{$target}</>.<fg=green>{$outerKey}</> "); |
142
|
|
|
if (count($where)) { |
143
|
|
|
$output->write(" Where:"); |
144
|
|
|
$output->writeln(str_replace(["\r\n", "\n"], "\n ", "\n" . print_r($where, 1))); |
145
|
|
|
} |
146
|
|
|
if (count($mmWhere)) { |
147
|
|
|
$output->write(" Through where:"); |
148
|
|
|
$output->writeln(str_replace(["\r\n", "\n"], "\n ", "\n" . print_r($mmWhere, 1))); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} else { |
152
|
|
|
$output->writeln(' No relations'); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
return ExitCode::OK; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|