|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BeyondCode\ErdGenerator; |
|
4
|
|
|
|
|
5
|
|
|
use ReflectionClass; |
|
6
|
|
|
use Illuminate\Console\Command; |
|
7
|
|
|
use phpDocumentor\GraphViz\Graph; |
|
8
|
|
|
use Illuminate\Support\Collection; |
|
9
|
|
|
use BeyondCode\ErdGenerator\Model as GraphModel; |
|
10
|
|
|
|
|
11
|
|
|
class GenerateDiagramCommand extends Command |
|
12
|
|
|
{ |
|
13
|
|
|
const FORMAT_TEXT = 'text'; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* The console command name. |
|
17
|
|
|
* |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $signature = 'generate:erd {filename=graph.png} {--format=png}'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* The console command description. |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $description = 'Generate ER diagram.'; |
|
28
|
|
|
|
|
29
|
|
|
/** @var ModelFinder */ |
|
30
|
|
|
protected $modelFinder; |
|
31
|
|
|
|
|
32
|
|
|
/** @var RelationFinder */ |
|
33
|
|
|
protected $relationFinder; |
|
34
|
|
|
|
|
35
|
|
|
/** @var Graph */ |
|
36
|
|
|
protected $graph; |
|
37
|
|
|
|
|
38
|
|
|
/** @var GraphBuilder */ |
|
39
|
|
|
protected $graphBuilder; |
|
40
|
|
|
|
|
41
|
|
|
public function __construct(ModelFinder $modelFinder, RelationFinder $relationFinder, GraphBuilder $graphBuilder) |
|
42
|
|
|
{ |
|
43
|
|
|
parent::__construct(); |
|
44
|
|
|
|
|
45
|
|
|
$this->relationFinder = $relationFinder; |
|
46
|
|
|
$this->modelFinder = $modelFinder; |
|
47
|
|
|
$this->graphBuilder = $graphBuilder; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function handle() |
|
51
|
|
|
{ |
|
52
|
|
|
$models = $this->getModelsThatShouldBeInspected(); |
|
53
|
|
|
|
|
54
|
|
|
$this->info("Found {$models->count()} models."); |
|
55
|
|
|
$this->info("Inspecing model relations."); |
|
56
|
|
|
|
|
57
|
|
|
$bar = $this->output->createProgressBar($models->count()); |
|
58
|
|
|
|
|
59
|
|
|
$models->transform(function ($model) use ($bar) { |
|
60
|
|
|
$bar->advance(); |
|
61
|
|
|
return new GraphModel( |
|
62
|
|
|
$model, |
|
63
|
|
|
(new ReflectionClass($model))->getShortName(), |
|
64
|
|
|
$this->relationFinder->getModelRelations($model) |
|
65
|
|
|
); |
|
66
|
|
|
}); |
|
67
|
|
|
|
|
68
|
|
|
$graph = $this->graphBuilder->buildGraph($models); |
|
69
|
|
|
|
|
70
|
|
|
if ($this->option('format') === self::FORMAT_TEXT) { |
|
71
|
|
|
$this->info($graph->__toString()); |
|
72
|
|
|
return; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$graph->export($this->option('format'), $this->argument('filename')); |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
$this->info(PHP_EOL); |
|
78
|
|
|
$this->info('Wrote diagram to '.$this->argument('filename')); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
protected function getModelsThatShouldBeInspected(): Collection |
|
82
|
|
|
{ |
|
83
|
|
|
$directories = config('erd-generator.directories'); |
|
84
|
|
|
|
|
85
|
|
|
$modelsFromDirectories = $this->getAllModelsFromEachDirectory($directories); |
|
86
|
|
|
|
|
87
|
|
|
return $modelsFromDirectories; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
protected function getAllModelsFromEachDirectory(array $directories): Collection |
|
91
|
|
|
{ |
|
92
|
|
|
return collect($directories) |
|
93
|
|
|
->map(function ($directory) { |
|
94
|
|
|
return $this->modelFinder->getModelsInDirectory($directory)->all(); |
|
95
|
|
|
}) |
|
96
|
|
|
->flatten(); |
|
97
|
|
|
} |
|
98
|
|
|
} |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.