Skip to content

Commit 11231db

Browse files
committed
Add swagger:make-schema-builder command
1 parent 5f0ddea commit 11231db

File tree

6 files changed

+164
-2
lines changed

6 files changed

+164
-2
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php namespace Mezatsong\SwaggerDocs\Commands;
2+
3+
use Illuminate\Console\GeneratorCommand;
4+
use Symfony\Component\Console\Input\InputArgument;
5+
6+
/**
7+
* Class MakeSwaggerSchemaBuilder
8+
* @package Mezatsong\SwaggerDocs\Commands
9+
*/
10+
class MakeSwaggerSchemaBuilder extends GeneratorCommand {
11+
12+
/**
13+
* The console command name.
14+
*
15+
* @var string
16+
*/
17+
protected $name = 'swagger:make-schema-builder';
18+
19+
/**
20+
* The console command description.
21+
*
22+
* @var string
23+
*/
24+
protected $description = 'Create a new Swagger schema builder';
25+
26+
/**
27+
* The type of class being generated.
28+
*
29+
* @var string
30+
*/
31+
protected $type = 'SchemaBuilder';
32+
33+
/**
34+
* @inheritDoc
35+
*/
36+
protected function getStub()
37+
{
38+
return __DIR__ . '/../stubs/SchemaBuilder.stub';
39+
}
40+
41+
/**
42+
* Get the console command arguments.
43+
*
44+
* @return array
45+
*/
46+
protected function getArguments()
47+
{
48+
return [
49+
['name', InputArgument::REQUIRED, 'The name of the class to be generated'],
50+
];
51+
}
52+
53+
/**
54+
* Get the default namespace for the class.
55+
*
56+
* @param string $rootNamespace
57+
* @return string
58+
*/
59+
protected function getDefaultNamespace($rootNamespace)
60+
{
61+
return $rootNamespace.'\Swagger\SchemaBuilders';
62+
}
63+
}

src/Responses/SchemaBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface SchemaBuilder {
99
/**
1010
* Build and return a custom swagger schema
1111
* @param string $modelRef a model swagger ref, (ex: #/components/schemas/User)
12-
* @param stirng $uri a current parsing uri
12+
* @param string $uri a current parsing uri
1313
* @return array an associative array representing the swagger schema for this resposne
1414
*/
1515
public function build(string $modelRef, string $uri): array;

src/Responses/SchemaBuilders/LaravelPaginateSchemaBuilder.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ class LaravelPaginateSchemaBuilder implements SchemaBuilder {
1111

1212
/**
1313
* Build a schema for Laravel pagination
14+
*
15+
* @param string $modelRef the swagger reference for model
16+
* @param string $uri the current parsing uri
1417
* @return array
1518
*/
1619
public function build(string $modelRef, string $uri): array {

src/Responses/SchemaBuilders/LaravelSimplePaginateSchemaBuilder.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ class LaravelSimplePaginateSchemaBuilder implements SchemaBuilder {
1212

1313
/**
1414
* Build a schema for Laravel simple pagination
15+
*
16+
* @param string $modelRef the swagger reference for model
17+
* @param string $uri the current parsing uri
1518
* @return array
1619
*/
1720
public function build(string $modelRef, string $uri): array {

src/SwaggerServiceProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Support\Facades\Validator;
66
use Illuminate\Support\ServiceProvider;
77
use Mezatsong\SwaggerDocs\Commands\GenerateSwaggerDocumentation;
8+
use Mezatsong\SwaggerDocs\Commands\MakeSwaggerSchemaBuilder;
89

910
/**
1011
* Class SwaggerServiceProvider
@@ -19,7 +20,8 @@ class SwaggerServiceProvider extends ServiceProvider {
1920
public function boot(): void {
2021
if ($this->app->runningInConsole()) {
2122
$this->commands([
22-
GenerateSwaggerDocumentation::class
23+
GenerateSwaggerDocumentation::class,
24+
MakeSwaggerSchemaBuilder::class
2325
]);
2426
}
2527

src/stubs/SchemaBuilder.stub

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace DummyNamespace;
4+
5+
use Illuminate\Support\Str;
6+
use Mezatsong\SwaggerDocs\Responses\SchemaBuilder;
7+
8+
class DummyClass implements SchemaBuilder
9+
{
10+
11+
/**
12+
* Build and return a custom swagger schema
13+
* Here is an example with Laravel pagination
14+
*
15+
* @param string $modelRef a model swagger ref, (ex: #/components/schemas/User)
16+
* @param string $uri a current parsing uri
17+
* @return array an associative array representing the swagger schema for this resposne
18+
*/
19+
public function build(string $modelRef, string $uri): array {
20+
if (!Str::startsWith($uri, '/')) {
21+
$uri = '/' . $uri;
22+
}
23+
$url = env('APP_URL') . $uri;
24+
return [
25+
'type' => 'object',
26+
'required' => [
27+
'current_page',
28+
'data',
29+
'first_page_url',
30+
'last_page',
31+
'last_page_url',
32+
'path',
33+
'per_page',
34+
'total'
35+
],
36+
'properties' => [
37+
"current_page" => [
38+
'type' => 'integer',
39+
'example' => 2
40+
],
41+
"data" => [
42+
'type' => 'array',
43+
'items' => [
44+
'type' => 'object',
45+
'$ref' => $modelRef
46+
]
47+
],
48+
"first_page_url" => [
49+
'type' => 'string',
50+
'example' => "$url?page=1"
51+
],
52+
"from" => [
53+
'type' => 'integer',
54+
'example' => 16
55+
],
56+
"last_page" => [
57+
'type' => 'integer',
58+
'example' => 10
59+
],
60+
"last_page_url" => [
61+
'type' => 'string',
62+
'example' => "$url?page=10"
63+
],
64+
"next_page_url" => [
65+
'type' => 'string',
66+
'example' => "$url?page=3"
67+
],
68+
"path" => [
69+
'type' => 'string',
70+
'example' => "$url"
71+
],
72+
"per_page" => [
73+
'type' => 'integer',
74+
'example' => 15
75+
],
76+
"prev_page_url" => [
77+
'type' => 'string',
78+
'example' => "$url?page=1"
79+
],
80+
"to" => [
81+
'type' => 'integer',
82+
'example' => 30
83+
],
84+
"total" => [
85+
'type' => 'integer',
86+
'example' => 150
87+
],
88+
]
89+
];
90+
}
91+
}

0 commit comments

Comments
 (0)