Skip to content

Commit cb41a0f

Browse files
committed
✨ automatic swagger documentation
1 parent 8a93fc9 commit cb41a0f

File tree

7 files changed

+85
-4
lines changed

7 files changed

+85
-4
lines changed

config/rest.php

+8
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@
5252
*/
5353

5454
'documentation' => [
55+
'routing' => [
56+
'enabled' => true,
57+
'domain' => null,
58+
'path' => '/api-documentation',
59+
'middlewares' => [
60+
'web'
61+
]
62+
],
5563
'info' => [
5664
'title' => config('app.name'),
5765
'summary' => 'This is my projet\'s documentation',

resources/views/index.blade.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1" />
6+
<meta
7+
name="description"
8+
content="SwaggerUI"
9+
/>
10+
<title>SwaggerUI</title>
11+
<link rel="stylesheet" href="https://unpkg.com/[email protected]/swagger-ui.css" />
12+
</head>
13+
<body>
14+
<div id="swagger-ui"></div>
15+
<script src="https://unpkg.com/[email protected]/swagger-ui-bundle.js" crossorigin></script>
16+
<script>
17+
window.onload = () => {
18+
window.ui = SwaggerUIBundle({
19+
url: '/vendor/rest/openapi.json',
20+
dom_id: '#swagger-ui',
21+
});
22+
};
23+
</script>
24+
</body>
25+
</html>

src/Console/Commands/DocumentationCommand.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function handle()
3737
$openApi = (new OpenAPI)
3838
->generate();
3939

40-
$path = $this->getPath('open-api');
40+
$path = $this->getPath('openapi');
4141

4242
$this->makeDirectory($path);
4343

@@ -55,7 +55,7 @@ public function handle()
5555
*/
5656
protected function getPath($name)
5757
{
58-
return storage_path('app/documentation/'.$name.'.json');
58+
return public_path('vendor/rest/'.$name.'.json');
5959
}
6060

6161
protected function getStub()

src/Documentation/Schemas/OpenAPI.php

-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
class OpenAPI extends Schema
1212
{
13-
// @TODO: generate swagger doc automatically
14-
1513
/**
1614
* The version number of the OpenAPI specification
1715
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Lomkit\Rest\Http\Controllers;
4+
5+
class DocumentationController extends \Illuminate\Routing\Controller
6+
{
7+
public function index() {
8+
return view('rest::index');
9+
}
10+
}

src/Http/routes.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Route;
4+
5+
if (config('rest.documentation.routing.enabled')) {
6+
Route::get('/', [\Lomkit\Rest\Http\Controllers\DocumentationController::class, 'index']);
7+
}

src/RestServiceProvider.php

+33
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Lomkit\Rest;
44

5+
use Illuminate\Support\Facades\Route;
56
use Illuminate\Support\ServiceProvider;
67
use Illuminate\Support\Str;
78
use Lomkit\Rest\Console\Commands\ActionCommand;
@@ -40,6 +41,38 @@ public function boot()
4041
{
4142
$this->registerCommands();
4243
$this->registerPublishing();
44+
45+
$this->registerRoutes();
46+
47+
$this->loadViewsFrom(
48+
__DIR__.'/../resources/views', 'rest'
49+
);
50+
}
51+
52+
/**
53+
* Register the package routes.
54+
*
55+
* @return void
56+
*/
57+
private function registerRoutes()
58+
{
59+
Route::group($this->routeConfiguration(), function () {
60+
$this->loadRoutesFrom(__DIR__.'/Http/routes.php');
61+
});
62+
}
63+
64+
/**
65+
* Get the Telescope route group configuration array.
66+
*
67+
* @return array
68+
*/
69+
private function routeConfiguration()
70+
{
71+
return [
72+
'domain' => config('rest.documentation.routing.domain'),
73+
'prefix' => config('rest.documentation.routing.path'),
74+
'middleware' => config('rest.documentation.routing.middlewares', []),
75+
];
4376
}
4477

4578
/**

0 commit comments

Comments
 (0)