1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Console; |
4
|
|
|
|
5
|
|
|
use Bdf\Prime\Exception\PrimeException; |
6
|
|
|
use Bdf\Prime\Schema\RepositoryUpgrader; |
7
|
|
|
use Bdf\Prime\Schema\SchemaManagerInterface; |
8
|
|
|
use Bdf\Prime\Schema\Transformer\Doctrine\TableTransformer; |
9
|
|
|
use Bdf\Prime\Schema\Visitor\Graphviz; |
10
|
|
|
use Bdf\Prime\ServiceLocator; |
11
|
|
|
use Bdf\Util\Console\BdfStyle; |
12
|
|
|
use Bdf\Util\File\ClassFileLocator; |
13
|
|
|
use Doctrine\DBAL\Schema\Schema; |
14
|
|
|
use Symfony\Component\Console\Attribute\AsCommand; |
15
|
|
|
use Symfony\Component\Console\Command\Command; |
16
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
18
|
|
|
use Symfony\Component\Console\Input\InputOption; |
19
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* GraphCommand |
23
|
|
|
*/ |
24
|
|
|
#[AsCommand('prime:graph', 'Get the schema graphic from mappers')] |
25
|
|
|
class GraphCommand extends Command |
26
|
|
|
{ |
27
|
|
|
protected static $defaultName = 'prime:graph'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ServiceLocator |
31
|
|
|
*/ |
32
|
|
|
private $locator; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* GraphCommand constructor. |
36
|
|
|
* |
37
|
|
|
* @param ServiceLocator $locator |
38
|
|
|
*/ |
39
|
|
|
public function __construct(ServiceLocator $locator) |
40
|
|
|
{ |
41
|
|
|
$this->locator = $locator; |
42
|
|
|
|
43
|
|
|
parent::__construct(static::$defaultName); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
protected function configure() |
50
|
|
|
{ |
51
|
|
|
$this |
52
|
|
|
->setDescription('Get the schema graphic from mappers') |
53
|
|
|
->addOption('output', 'o', InputOption::VALUE_REQUIRED, 'Dot file to output graph') |
54
|
|
|
->addOption('fromdb', null, InputOption::VALUE_NONE, 'Generate graph from database') |
55
|
|
|
->addArgument('path', InputArgument::REQUIRED, 'The model path') |
56
|
|
|
; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
63
|
|
|
{ |
64
|
|
|
$io = new BdfStyle($input, $output); |
65
|
|
|
|
66
|
|
|
$schema = $io->option('fromdb') |
67
|
|
|
? $this->getSchemaFromDatabase() |
68
|
|
|
: $this->getSchemaFromModel($io, $io->argument('path')); |
69
|
|
|
|
70
|
|
|
$graph = new Graphviz(); |
71
|
|
|
$schema->visit($graph); |
|
|
|
|
72
|
|
|
|
73
|
|
|
if (!$io->option('output')) { |
74
|
|
|
$io->line($graph->getOutput()); |
75
|
|
|
} else { |
76
|
|
|
$io->line('Writting graph file <info>'.$io->option('output').'</info>'); |
77
|
|
|
$graph->write($io->option('output')); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return 0; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get the tables collection from model |
85
|
|
|
* |
86
|
|
|
* @param BdfStyle $io |
87
|
|
|
* @param string $path |
88
|
|
|
* |
89
|
|
|
* @return Schema |
90
|
|
|
* |
91
|
|
|
* @throws PrimeException |
92
|
|
|
*/ |
93
|
|
|
protected function getSchemaFromModel($io, $path) |
94
|
|
|
{ |
95
|
|
|
$tables = []; |
96
|
|
|
|
97
|
|
|
foreach ((new ClassFileLocator(realpath($path))) as $classInfo) { |
98
|
|
|
$className = $classInfo->getClass(); |
99
|
|
|
|
100
|
|
|
// walk on mapper only |
101
|
|
|
if (!$this->locator->mappers()->isMapper($className)) { |
102
|
|
|
$io->debug("{$className} is not a mapper class"); |
103
|
|
|
continue; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$mapper = $this->locator->mappers()->createMapper($this->locator, $className); |
107
|
|
|
/** @var RepositoryUpgrader $schemaManager */ |
108
|
|
|
$schemaManager = $mapper->repository()->schema(true); |
109
|
|
|
$platform = $mapper->repository()->connection()->platform(); |
110
|
|
|
|
111
|
|
|
$io->debug('Loading table info from '.$className); |
112
|
|
|
|
113
|
|
|
$table = $schemaManager->table(true); |
114
|
|
|
$tables[$table->name()] = (new TableTransformer($table, $platform))->toDoctrine(); |
115
|
|
|
|
116
|
|
|
if (($sequence = $schemaManager->sequence()) !== null) { |
117
|
|
|
$tables[$sequence->name()] = (new TableTransformer($sequence, $platform))->toDoctrine(); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return new Schema($tables); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get the tables collection from database |
126
|
|
|
* |
127
|
|
|
* @return Schema |
128
|
|
|
*/ |
129
|
|
|
protected function getSchemaFromDatabase() |
130
|
|
|
{ |
131
|
|
|
$manager = $this->locator->connection()->schema(); |
132
|
|
|
|
133
|
|
|
if (!$manager instanceof SchemaManagerInterface) { |
134
|
|
|
throw new \InvalidArgumentException('The default connection do not support schema loading'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// TODO parcourir les connections (database unique) et merger les tables ? |
138
|
|
|
return $manager->loadSchema(); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|