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}'; |
||
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 | $graph = $this->graphBuilder->buildGraph($models); |
||
74 | |||
75 | if ($this->option('format') === self::FORMAT_TEXT) { |
||
76 | $this->info($graph->__toString()); |
||
77 | return; |
||
78 | } |
||
79 | |||
80 | $graph->export($this->option('format'), $this->getOutputFileName()); |
||
81 | |||
82 | $this->info(PHP_EOL); |
||
83 | $this->info('Wrote diagram to ' . $this->getOutputFileName()); |
||
84 | } |
||
85 | |||
86 | protected function getOutputFileName(): string |
||
87 | { |
||
88 | return $this->argument('filename') ?: |
||
89 | static::DEFAULT_FILENAME . '.' . $this->option('format'); |
||
90 | } |
||
91 | |||
92 | protected function getModelsThatShouldBeInspected(): Collection |
||
93 | { |
||
94 | $directories = config('erd-generator.directories'); |
||
95 | |||
96 | $modelsFromDirectories = $this->getAllModelsFromEachDirectory($directories); |
||
97 | |||
98 | return $modelsFromDirectories; |
||
99 | } |
||
100 | |||
101 | protected function getAllModelsFromEachDirectory(array $directories): Collection |
||
102 | { |
||
103 | return collect($directories) |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
104 | ->map(function ($directory) { |
||
105 | return $this->modelFinder->getModelsInDirectory($directory)->all(); |
||
106 | }) |
||
107 | ->flatten(); |
||
108 | } |
||
109 | } |
||
110 |