Skip to content

Commit 85e61be

Browse files
committed
Added CSV Presentation
1 parent e675df1 commit 85e61be

File tree

4 files changed

+82
-4
lines changed

4 files changed

+82
-4
lines changed

config/db-doc.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
// config for Bekwoh/LaravelDbDoc
44

5+
use Bekwoh\LaravelDbDoc\Presentation\Csv;
56
use Bekwoh\LaravelDbDoc\Presentation\Json;
67
use Bekwoh\LaravelDbDoc\Presentation\Markdown;
78

@@ -18,6 +19,11 @@
1819
'disk' => env('LARAVEL_DB_DOC_JSON_DISK', 'local'),
1920
'view' => null,
2021
],
22+
'csv' => [
23+
'class' => Csv::class,
24+
'disk' => env('LARAVEL_DB_DOC_CSV_DISK', 'local'),
25+
'view' => null,
26+
],
2127
],
2228
'middleware' => [
2329
'auth',

src/LaravelDbDoc.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ public static function routes()
2020
]);
2121
}
2222

23+
if ($format == 'csv') {
24+
$filename = date('Ymd').self::filename($format);
25+
26+
return response($content, 200, [
27+
'Content-Type' => 'text/csv',
28+
'Content-Disposition' => 'attachment; filename="' . $filename . '"',
29+
]);
30+
}
31+
2332
return view('laravel-db-doc::markdown', [
2433
'content' => Str::markdown(
2534
$content
@@ -32,23 +41,27 @@ public static function routes()
3241

3342
public static function content($format)
3443
{
35-
throw_if(! in_array($format, ['json', 'markdown']));
44+
throw_if(! in_array($format, ['json', 'markdown', 'csv']));
3645

3746
return Storage::disk(self::disk($format))->get(self::filename($format));
3847
}
3948

4049
public static function disk($format)
4150
{
42-
throw_if(! in_array($format, ['json', 'markdown']));
51+
throw_if(! in_array($format, ['json', 'markdown', 'csv']));
4352

4453
return config("db-doc.presentations.$format.disk");
4554
}
4655

4756
public static function filename($format)
4857
{
49-
throw_if(! in_array($format, ['json', 'markdown']));
58+
throw_if(! in_array($format, ['json', 'markdown', 'csv']));
5059

51-
$extension = $format != 'markdown' ? 'json' : 'md';
60+
$extension = match ($format) {
61+
'markdown' => 'md',
62+
'csv' => 'csv',
63+
default => 'json',
64+
};
5265

5366
return config('app.name')." Database Schema.$extension";
5467
}

src/Presentation/Csv.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Bekwoh\LaravelDbDoc\Presentation;
4+
5+
use Bekwoh\LaravelDbDoc\Contracts\Presenter;
6+
use Illuminate\Support\Str;
7+
8+
class Csv extends AbstractPresenter implements Presenter
9+
{
10+
public static function make(array $contents): self
11+
{
12+
return new self($contents);
13+
}
14+
15+
public function getContents()
16+
{
17+
$contents = $this->contents;
18+
$output = [];
19+
20+
foreach ($contents as $table => $properties) {
21+
22+
$output[] = $table;
23+
$output[] = 'Column,Type,Length,Default,Nullable,Comment';
24+
foreach ($properties as $key => $value) {
25+
$fields = [];
26+
foreach ($value as $k => $v) {
27+
$fields[] = "{$v}";
28+
}
29+
$output[] = implode(',', $fields);
30+
}
31+
$output[] = PHP_EOL;
32+
}
33+
34+
$schema = implode('', $output);
35+
$stub = $this->getStub();
36+
$database_config = config('database.connections.'.$this->connection);
37+
$host = isset($database_config['host']) ? $database_config['host'] : null;
38+
$port = isset($database_config['port']) ? $database_config['port'] : null;
39+
40+
return str_replace([
41+
'APP_NAME',
42+
'DB_CONNECTION', 'DB_HOST', 'DB_PORT', 'DB_DATABASE',
43+
'SCHEMA_CONTENT',
44+
], [
45+
config('app.name'),
46+
$this->connection, $host, $port, $database_config['database'],
47+
$schema,
48+
], $stub);
49+
}
50+
}

tests/LaravelDbDocTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@
2727
);
2828
});
2929

30+
it('can generate db doc schema - csv', function () {
31+
Artisan::call('db:schema --format=csv');
32+
33+
$this->assertTrue(
34+
Storage::disk(LaravelDbDoc::disk('csv'))
35+
->exists(LaravelDbDoc::filename('csv'))
36+
);
37+
});
38+
3039
it('has doc/db-schema route', function () {
3140
$this->assertTrue(
3241
Route::has('doc.db-schema')

0 commit comments

Comments
 (0)