Skip to content

Commit e639cd8

Browse files
committed
Update
1 parent d438a22 commit e639cd8

File tree

10 files changed

+78
-32
lines changed

10 files changed

+78
-32
lines changed

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
],
1818
"require": {
1919
"php": "^8.1",
20-
"spatie/laravel-package-tools": "^1.13.0",
21-
"illuminate/contracts": "^9.0"
20+
"doctrine/dbal": "^3.4",
21+
"illuminate/contracts": "^9.0",
22+
"spatie/laravel-package-tools": "^1.13.0"
2223
},
2324
"require-dev": {
2425
"laravel/pint": "^1.0",
@@ -68,4 +69,4 @@
6869
},
6970
"minimum-stability": "dev",
7071
"prefer-stable": true
71-
}
72+
}

src/Commands/LaravelDbDocCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class LaravelDbDocCommand extends Command
1212
*
1313
* @var string
1414
*/
15-
protected $signature = 'db:schema {--database=} {--format=md} {--path=} {--emoji}';
15+
protected $signature = 'db:schema {--database=} {--format=markdown}';
1616

1717
/**
1818
* The console command description.

src/Contracts/Presenter.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ public function getStub();
1717
public function write();
1818

1919
public function read();
20+
21+
public function connection(string $connection): self;
2022
}

src/LaravelDbDoc.php

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

33
namespace Bekwoh\LaravelDbDoc;
44

5+
use Illuminate\Support\Facades\Gate;
56
use Illuminate\Support\Facades\Route;
67
use Illuminate\Support\Facades\Storage;
78
use Illuminate\Support\Str;
@@ -12,15 +13,11 @@ public static function routes()
1213
{
1314
if (app()->environment() != 'production') {
1415
Route::get('doc/db-schema', function () {
15-
$format = request()->query('format', 'markdown');
16+
// Todo: define role
1617

17-
$filename = self::filename($format);
18+
$format = request()->query('format', 'markdown');
1819
$content = self::content($format);
1920

20-
$filepath = config('database.doc_schema_path').DIRECTORY_SEPARATOR.$filename;
21-
22-
abort_if(! file_exists($filepath), 404, 'Database schema document not yet generated. Do run php artisan db:schema to generate the schema document.');
23-
2421
if ($format == 'json') {
2522
return response()->json([
2623
'content' => json_decode($content),
@@ -32,7 +29,7 @@ public static function routes()
3229
$content
3330
),
3431
]);
35-
});
32+
})->middleware('auth', 'verified')->name('doc.db-schema');
3633
}
3734
}
3835

@@ -58,11 +55,4 @@ public static function filename($format)
5855

5956
return config('app.name')." Database Schema.$extension";
6057
}
61-
62-
public static function view($format)
63-
{
64-
throw_if(! in_array($format, ['json', 'markdown']));
65-
66-
return config("db-doc.presentations.$format.view");
67-
}
6858
}

src/LaravelDbDocServiceProvider.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Bekwoh\LaravelDbDoc;
44

55
use Bekwoh\LaravelDbDoc\Commands\LaravelDbDocCommand;
6+
use Bekwoh\LaravelDbDoc\Facades\LaravelDbDoc;
67
use Spatie\LaravelPackageTools\Package;
78
use Spatie\LaravelPackageTools\PackageServiceProvider;
89

@@ -21,4 +22,9 @@ public function configurePackage(Package $package): void
2122
->hasViews()
2223
->hasCommand(LaravelDbDocCommand::class);
2324
}
25+
26+
public function bootingPackage()
27+
{
28+
LaravelDbDoc::routes();
29+
}
2430
}

src/Presentation/AbstractPresenter.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,31 @@
88

99
abstract class AbstractPresenter implements Presenter
1010
{
11+
protected string $connection;
12+
1113
abstract public function getContents();
1214

1315
public function __construct(protected array $contents)
1416
{
1517
}
1618

17-
public static function make(array $contents): self
18-
{
19-
return new self($contents);
20-
}
21-
2219
public function getDisk()
2320
{
24-
return LaravelDbDoc::disk(strtolower(basename(__CLASS__)));
21+
return LaravelDbDoc::disk(strtolower(class_basename($this)));
2522
}
2623

2724
public function getFilename()
2825
{
29-
return LaravelDbDoc::filename(strtolower(basename(__CLASS__)));
26+
return LaravelDbDoc::filename(strtolower(class_basename($this)));
3027
}
3128

3229
public function getStub()
3330
{
34-
return file_get_contents(
31+
$path = file_exists(
3532
base_path('stubs/db-doc.stub')
36-
);
33+
) ? base_path('stubs/db-doc.stub') : __DIR__ . '/../../stubs/db-doc.stub';
34+
35+
return file_get_contents($path);
3736
}
3837

3938
public function write()
@@ -50,4 +49,11 @@ public function read()
5049
return Storage::disk($this->getDisk())
5150
->get($this->getFilename());
5251
}
52+
53+
public function connection(string $connection): self
54+
{
55+
$this->connection = $connection;
56+
57+
return $this;
58+
}
5359
}

src/Presentation/Json.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
class Json extends AbstractPresenter implements Presenter
88
{
9+
public static function make(array $contents): self
10+
{
11+
return new self($contents);
12+
}
13+
914
public function getContents()
1015
{
1116
return json_encode($this->contents, JSON_PRETTY_PRINT);

src/Presentation/Markdown.php

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

88
class Markdown extends AbstractPresenter implements Presenter
99
{
10+
public static function make(array $contents): self
11+
{
12+
return new self($contents);
13+
}
14+
1015
public function getContents()
1116
{
1217
$contents = $this->contents;
@@ -28,7 +33,7 @@ public function getContents()
2833

2934
$schema = implode('', $output);
3035
$stub = $this->getStub();
31-
$database_config = config('database.connections.'.$this->database_connection);
36+
$database_config = config('database.connections.'.$this->connection);
3237
$host = isset($database_config['host']) ? $database_config['host'] : null;
3338
$port = isset($database_config['port']) ? $database_config['port'] : null;
3439

@@ -38,7 +43,7 @@ public function getContents()
3843
'SCHEMA_CONTENT',
3944
], [
4045
config('app.name'),
41-
$this->database_connection, $host, $port, $database_config['database'],
46+
$this->connection, $host, $port, $database_config['database'],
4247
$schema,
4348
], $stub);
4449
}

src/Processor.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Processor
1010
{
1111
protected array $data;
1212

13-
protected Presenter $presenter;
13+
protected string $presenter;
1414

1515
public function __construct()
1616
{
@@ -36,15 +36,19 @@ public function process()
3636

3737
public function render()
3838
{
39-
$this->presenter::make($this->data)->write();
39+
throw_if(! in_array(Presenter::class, class_implements($this->presenter)));
40+
41+
$this->presenter::make($this->data)
42+
->connection($this->database_connection)
43+
->write();
4044
}
4145

4246
public function connect(string $connection, string $format): self
4347
{
4448
$this->database_connection = $connection;
4549
$this->presenter = config('db-doc.presentations.'.$format.'.class');
4650

47-
throw_if(! class_exists($this->format), 'RuntimeException', "$this->format not exists.");
51+
throw_if(! class_exists($this->presenter), 'RuntimeException', "$this->presenter not exists.");
4852

4953
$this->connection = DB::connection($this->database_connection)->getDoctrineConnection();
5054
$this->schema = $this->connection->getSchemaManager();

tests/LaravelDbDocTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,34 @@
11
<?php
22

3+
use Bekwoh\LaravelDbDoc\Facades\LaravelDbDoc;
34
use Illuminate\Support\Facades\Artisan;
5+
use Illuminate\Support\Facades\Route;
6+
use Illuminate\Support\Facades\Storage;
47

58
it('has db:schema command', function () {
69
$this->assertTrue(in_array('db:schema', array_keys(Artisan::all())));
710
});
11+
12+
it('can generate db doc schema - markdown', function () {
13+
Artisan::call('db:schema');
14+
15+
$this->assertTrue(
16+
Storage::disk(LaravelDbDoc::disk('markdown'))
17+
->exists(LaravelDbDoc::filename('markdown'))
18+
);
19+
});
20+
21+
it('can generate db doc schema - json', function () {
22+
Artisan::call('db:schema --format=json');
23+
24+
$this->assertTrue(
25+
Storage::disk(LaravelDbDoc::disk('json'))
26+
->exists(LaravelDbDoc::filename('json'))
27+
);
28+
});
29+
30+
it('has doc/db-schema route', function () {
31+
$this->assertTrue(
32+
Route::has('doc.db-schema')
33+
);
34+
});

0 commit comments

Comments
 (0)