1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BeyondCode\ErdGenerator; |
4
|
|
|
|
5
|
|
|
use BeyondCode\ErdGenerator\Model as GraphModel; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use phpDocumentor\GraphViz\Graph; |
9
|
|
|
use ReflectionClass; |
10
|
|
|
|
11
|
|
|
class GenerateDiagramCommand extends Command |
12
|
|
|
{ |
13
|
|
|
const FORMAT_TEXT = 'text'; |
14
|
|
|
|
15
|
|
|
const DEFAULT_FILENAME = 'graph'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The console command name. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $signature = 'generate:erd {filename?} {--format=png} {--text-output : Output as text file instead of image}'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The console command description. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $description = 'Generate ER diagram.'; |
30
|
|
|
|
31
|
|
|
/** @var ModelFinder */ |
32
|
|
|
protected $modelFinder; |
33
|
|
|
|
34
|
|
|
/** @var RelationFinder */ |
35
|
|
|
protected $relationFinder; |
36
|
|
|
|
37
|
|
|
/** @var Graph */ |
38
|
|
|
protected $graph; |
39
|
|
|
|
40
|
|
|
/** @var GraphBuilder */ |
41
|
|
|
protected $graphBuilder; |
42
|
|
|
|
43
|
|
|
public function __construct(ModelFinder $modelFinder, RelationFinder $relationFinder, GraphBuilder $graphBuilder) |
44
|
|
|
{ |
45
|
|
|
parent::__construct(); |
46
|
|
|
|
47
|
|
|
$this->relationFinder = $relationFinder; |
48
|
|
|
$this->modelFinder = $modelFinder; |
49
|
|
|
$this->graphBuilder = $graphBuilder; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @throws \phpDocumentor\GraphViz\Exception |
54
|
|
|
*/ |
55
|
|
|
public function handle() |
56
|
|
|
{ |
57
|
|
|
$models = $this->getModelsThatShouldBeInspected(); |
58
|
|
|
|
59
|
|
|
$this->info("Found {$models->count()} models."); |
60
|
|
|
$this->info("Inspecting model relations."); |
61
|
|
|
|
62
|
|
|
$bar = $this->output->createProgressBar($models->count()); |
63
|
|
|
|
64
|
|
|
$models->transform(function ($model) use ($bar) { |
65
|
|
|
$bar->advance(); |
66
|
|
|
return new GraphModel( |
67
|
|
|
$model, |
68
|
|
|
(new ReflectionClass($model))->getShortName(), |
69
|
|
|
$this->relationFinder->getModelRelations($model) |
70
|
|
|
); |
71
|
|
|
}); |
72
|
|
|
|
73
|
|
|
// Check if output filename has .txt extension |
74
|
|
|
$outputFileName = $this->getOutputFileName(); |
75
|
|
|
if (pathinfo($outputFileName, PATHINFO_EXTENSION) === 'txt') { |
76
|
|
|
// Generate structured text output for .txt files |
77
|
|
|
$textOutput = $this->graphBuilder->generateStructuredTextRepresentation($models); |
78
|
|
|
file_put_contents($outputFileName, $textOutput); |
79
|
|
|
$this->info(PHP_EOL); |
80
|
|
|
$this->info('Wrote structured ER diagram to ' . $outputFileName); |
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$graph = $this->graphBuilder->buildGraph($models); |
85
|
|
|
|
86
|
|
|
if ($this->option('text-output') || $this->option('format') === self::FORMAT_TEXT) { |
87
|
|
|
$textOutput = $graph->__toString(); |
88
|
|
|
|
89
|
|
|
// If text-output option is set, write to file |
90
|
|
|
if ($this->option('text-output')) { |
91
|
|
|
$outputFileName = $this->getTextOutputFileName(); |
92
|
|
|
file_put_contents($outputFileName, $textOutput); |
93
|
|
|
$this->info(PHP_EOL); |
94
|
|
|
$this->info('Wrote text diagram to ' . $outputFileName); |
95
|
|
|
return; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// Otherwise just output to console |
99
|
|
|
$this->info($textOutput); |
100
|
|
|
return; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$graph->export($this->option('format'), $outputFileName); |
104
|
|
|
|
105
|
|
|
$this->info(PHP_EOL); |
106
|
|
|
$this->info('Wrote diagram to ' . $outputFileName); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
protected function getOutputFileName(): string |
110
|
|
|
{ |
111
|
|
|
return $this->argument('filename') ?: |
112
|
|
|
static::DEFAULT_FILENAME . '.' . $this->option('format'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
protected function getTextOutputFileName(): string |
116
|
|
|
{ |
117
|
|
|
return $this->argument('filename') ?: static::DEFAULT_FILENAME . '.txt'; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
protected function getModelsThatShouldBeInspected(): Collection |
121
|
|
|
{ |
122
|
|
|
$directories = config('erd-generator.directories'); |
123
|
|
|
|
124
|
|
|
$modelsFromDirectories = $this->getAllModelsFromEachDirectory($directories); |
125
|
|
|
|
126
|
|
|
return $modelsFromDirectories; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
protected function getAllModelsFromEachDirectory(array $directories): Collection |
130
|
|
|
{ |
131
|
|
|
return collect($directories) |
|
|
|
|
132
|
|
|
->map(function ($directory) { |
133
|
|
|
return $this->modelFinder->getModelsInDirectory($directory)->all(); |
134
|
|
|
}) |
135
|
|
|
->flatten(); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|