Skip to content

Commit 2bd3dca

Browse files
committed
Update Test
1 parent 58fc26e commit 2bd3dca

File tree

6 files changed

+21
-162
lines changed

6 files changed

+21
-162
lines changed

src/Commands/LaravelDbDocCommand.php

Lines changed: 1 addition & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,12 @@
11
<?php
22

3-
namespace App\Console\Commands\Db;
3+
namespace Bekwoh\LaravelDbDoc\Commands;
44

55
use Bekwoh\LaravelDbDoc\Processor;
66
use Illuminate\Console\Command;
7-
use Illuminate\Support\Str;
87

98
class LaravelDbDocCommand extends Command
109
{
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-
3010
/**
3111
* The name and signature of the console command.
3212
*
@@ -56,139 +36,4 @@ public function handle()
5636
->process()
5737
->render();
5838
}
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-
}
19439
}

src/Contracts/Presenter.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
interface Presenter
66
{
7+
public static function make(array $contents): self;
8+
79
public function getDisk();
810

911
public function getFilename();

src/Presentation/AbstractPresenter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ public function __construct(protected array $contents)
1414
{
1515
}
1616

17+
public static function make(array $contents): self
18+
{
19+
return new self($contents);
20+
}
21+
1722
public function getDisk()
1823
{
1924
return LaravelDbDoc::disk(strtolower(basename(__CLASS__)));

src/Processor.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
namespace Bekwoh\LaravelDbDoc;
44

5+
use Bekwoh\LaravelDbDoc\Contracts\Presenter;
56
use Bekwoh\LaravelDbDoc\Data\Schema;
67
use Illuminate\Support\Facades\DB;
78

89
class Processor
910
{
1011
protected array $data;
12+
protected Presenter $presenter;
1113

1214
public function __construct()
1315
{
@@ -27,16 +29,19 @@ public static function make()
2729
public function process()
2830
{
2931
$this->data = Schema::make($this->tables, $this->schema)->getData();
32+
33+
return $this;
3034
}
3135

3236
public function render()
3337
{
38+
$this->presenter::make($this->data)->write();
3439
}
3540

3641
public function connect(string $connection, string $format): self
3742
{
3843
$this->database_connection = $connection;
39-
$this->format = config('db-doc.presentations.'.$format.'.class');
44+
$this->presenter = config('db-doc.presentations.'.$format.'.class');
4045

4146
throw_if(! class_exists($this->format), 'RuntimeException', "$this->format not exists.");
4247

tests/ExampleTest.php

Lines changed: 0 additions & 5 deletions
This file was deleted.

tests/LaravelDbDocTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Artisan;
4+
5+
it('has db:schema command', function () {
6+
$this->assertTrue(in_array('db:schema', array_keys(Artisan::all())));
7+
});

0 commit comments

Comments
 (0)