|
1 | 1 | <?php
|
2 | 2 |
|
3 |
| -namespace App\Console\Commands\Db; |
| 3 | +namespace Bekwoh\LaravelDbDoc\Commands; |
4 | 4 |
|
5 | 5 | use Bekwoh\LaravelDbDoc\Processor;
|
6 | 6 | use Illuminate\Console\Command;
|
7 |
| -use Illuminate\Support\Str; |
8 | 7 |
|
9 | 8 | class LaravelDbDocCommand extends Command
|
10 | 9 | {
|
11 |
| - public $database_connection; |
12 |
| - |
13 |
| - public $format; |
14 |
| - |
15 |
| - public $connection; |
16 |
| - |
17 |
| - public $schema; |
18 |
| - |
19 |
| - public $tables; |
20 |
| - |
21 |
| - public $collections = []; |
22 |
| - |
23 |
| - /** |
24 |
| - * Output Path. |
25 |
| - * |
26 |
| - * @var string |
27 |
| - */ |
28 |
| - public $output_path; |
29 |
| - |
30 | 10 | /**
|
31 | 11 | * The name and signature of the console command.
|
32 | 12 | *
|
@@ -56,139 +36,4 @@ public function handle()
|
56 | 36 | ->process()
|
57 | 37 | ->render();
|
58 | 38 | }
|
59 |
| - |
60 |
| - private function generateDataStructure() |
61 |
| - { |
62 |
| - $tables = $this->tables; |
63 |
| - $schema = $this->schema; |
64 |
| - |
65 |
| - $this->collections = []; |
66 |
| - foreach ($tables as $table) { |
67 |
| - $columns = $schema->listTableColumns($table); |
68 |
| - $foreignKeys = collect($schema->listTableForeignKeys($table))->keyBy(function ($foreignColumn) { |
69 |
| - return $foreignColumn->getLocalColumns()[0]; |
70 |
| - }); |
71 |
| - $this->info('Table: '.$table); |
72 |
| - foreach ($columns as $column) { |
73 |
| - $columnName = $column->getName(); |
74 |
| - $columnType = $column->getType()->getName(); |
75 |
| - if (isset($foreignKeys[$columnName])) { |
76 |
| - $foreignColumn = $foreignKeys[$columnName]; |
77 |
| - $foreignTable = $foreignColumn->getForeignTableName(); |
78 |
| - $columnType = 'FK -> '.$foreignTable; |
79 |
| - } |
80 |
| - $length = $column->getLength(); |
81 |
| - |
82 |
| - $details['column'] = $columnName; |
83 |
| - $details['type'] = $columnType.$this->determineUnsigned($column); |
84 |
| - $details['length'] = $length != 0 ? $length : null; |
85 |
| - $details['default'] = $this->getDefaultValue($column); |
86 |
| - $details['nullable'] = $this->getExpression(true === ! $column->getNotNull()); |
87 |
| - $details['comment'] = $column->getComment(); |
88 |
| - $this->collections[$table][] = $details; |
89 |
| - } |
90 |
| - } |
91 |
| - } |
92 |
| - |
93 |
| - private function generateDocument() |
94 |
| - { |
95 |
| - switch ($this->format) { |
96 |
| - case 'json': |
97 |
| - $rendered = $this->render_json_content(); |
98 |
| - |
99 |
| - break; |
100 |
| - |
101 |
| - default: |
102 |
| - $rendered = $this->render_markdown_content(); |
103 |
| - |
104 |
| - break; |
105 |
| - } |
106 |
| - $filename = $rendered['filename']; |
107 |
| - $output = $rendered['output']; |
108 |
| - $path = $this->output_path.DIRECTORY_SEPARATOR.$filename; |
109 |
| - if (file_exists($path)) { |
110 |
| - unlink($path); |
111 |
| - } |
112 |
| - file_put_contents($path, $output); |
113 |
| - } |
114 |
| - |
115 |
| - private function getStub() |
116 |
| - { |
117 |
| - return file_get_contents(base_path('stubs/db/doc.schema.stub')); |
118 |
| - } |
119 |
| - |
120 |
| - private function determineUnsigned($column) |
121 |
| - { |
122 |
| - return (true === $column->getUnsigned()) ? '(unsigned)' : ''; |
123 |
| - } |
124 |
| - |
125 |
| - private function getDefaultValue($column) |
126 |
| - { |
127 |
| - if ('boolean' == $column->getType()->getName()) { |
128 |
| - return $column->getDefault() ? 'true' : 'false'; |
129 |
| - } |
130 |
| - |
131 |
| - return $column->getDefault(); |
132 |
| - } |
133 |
| - |
134 |
| - private function getExpression($status) |
135 |
| - { |
136 |
| - if ($this->option('emoji')) { |
137 |
| - return $status ? "\u{2705}" : "\u{274C}"; |
138 |
| - } |
139 |
| - |
140 |
| - return $status ? 'Yes' : 'No'; |
141 |
| - } |
142 |
| - |
143 |
| - private function render_json_content() |
144 |
| - { |
145 |
| - $collections = $this->collections; |
146 |
| - |
147 |
| - return [ |
148 |
| - 'output' => json_encode($collections), |
149 |
| - 'filename' => config('app.name').' Database Schema.json', |
150 |
| - ]; |
151 |
| - } |
152 |
| - |
153 |
| - private function render_markdown_content() |
154 |
| - { |
155 |
| - $collections = $this->collections; |
156 |
| - $output = []; |
157 |
| - foreach ($collections as $table => $properties) { |
158 |
| - $table = preg_replace('/[^A-Za-z0-9]/', ' ', $table); |
159 |
| - $output[] = '### '.Str::title($table).PHP_EOL.PHP_EOL; |
160 |
| - $output[] = '| Column | Type | Length | Default | Nullable | Comment |'.PHP_EOL; |
161 |
| - $output[] = '|--------|------|--------|---------|----------|---------|'.PHP_EOL; |
162 |
| - foreach ($properties as $key => $value) { |
163 |
| - $fields = []; |
164 |
| - foreach ($value as $k => $v) { |
165 |
| - $fields[] = "{$v}"; |
166 |
| - } |
167 |
| - $output[] = '| '.implode(' | ', $fields).' |'.PHP_EOL; |
168 |
| - } |
169 |
| - $output[] = PHP_EOL; |
170 |
| - } |
171 |
| - |
172 |
| - $schema = implode('', $output); |
173 |
| - $stub = $this->getStub(); |
174 |
| - $database_config = config('database.connections.'.$this->database_connection); |
175 |
| - $host = isset($database_config['host']) ? $database_config['host'] : null; |
176 |
| - $port = isset($database_config['port']) ? $database_config['port'] : null; |
177 |
| - $output = str_replace([ |
178 |
| - 'APP_NAME', |
179 |
| - 'DB_CONNECTION', 'DB_HOST', 'DB_PORT', 'DB_DATABASE', |
180 |
| - 'SCHEMA_CONTENT', |
181 |
| - ], [ |
182 |
| - config('app.name'), |
183 |
| - $this->database_connection, $host, $port, $database_config['database'], |
184 |
| - $schema, |
185 |
| - ], $stub); |
186 |
| - |
187 |
| - $filename = config('app.name').' Database Schema.md'; |
188 |
| - |
189 |
| - return [ |
190 |
| - 'output' => $output, |
191 |
| - 'filename' => $filename, |
192 |
| - ]; |
193 |
| - } |
194 | 39 | }
|
0 commit comments