Skip to content

Commit 3233d33

Browse files
committed
initial commit
0 parents  commit 3233d33

File tree

5 files changed

+209
-0
lines changed

5 files changed

+209
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor/

composer.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "mtrajano/laravel-swagger",
3+
"description": "Auto generates the swagger documentation for a laravel project",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "Mauricio Trajano",
8+
"email": "[email protected]"
9+
}
10+
],
11+
"require": {
12+
"laravel/framework": ">=5.0"
13+
},
14+
"autoload": {
15+
"psr-4": {
16+
"Mtrajano\\LaravelSwagger\\": "src/"
17+
}
18+
},
19+
"extra": {
20+
"laravel": {
21+
"providers": [
22+
"Mtrajano\\LaravelSwagger\\SwaggerServiceProvider"
23+
]
24+
}
25+
}
26+
}

config/laravel-swagger.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
return [
4+
'title' => env('APP_NAME'),
5+
'description' => '',
6+
'appVersion' => '1.0.0',
7+
'host' => env('APP_URL'),
8+
'basePath' => '/',
9+
];

src/GenerateSwaggerDoc.php

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
3+
namespace Mtrajano\LaravelSwagger;
4+
5+
use ReflectionMethod;
6+
use Illuminate\Support\Str;
7+
use Illuminate\Console\Command;
8+
use Illuminate\Foundation\Http\FormRequest;
9+
10+
class GenerateSwaggerDoc extends Command
11+
{
12+
/**
13+
* The name and signature of the console command.
14+
*
15+
* @var string
16+
*/
17+
protected $signature = 'laravel-swagger:generate';
18+
19+
/**
20+
* The console command description.
21+
*
22+
* @var string
23+
*/
24+
protected $description = 'Automatically generates a swagger documentation file for this application';
25+
26+
/**
27+
* Execute the console command.
28+
*
29+
* @return mixed
30+
*/
31+
public function handle()
32+
{
33+
$parsed = [
34+
'swagger' => '2.0',
35+
'info' => [
36+
'title' => config('laravel-swagger.title'),
37+
'description' => config('laravel-swagger.description'),
38+
'version' => config('laravel-swagger.appVersion'),
39+
],
40+
'host' => config('laravel-swagger.host'),
41+
'basePath' => config('laravel-swagger.basePath'),
42+
'paths' => [],
43+
];
44+
45+
foreach (app('routes')->get() as $route) {
46+
$uri = $route->uri();
47+
$methods = $route->methods();
48+
$action = $route->getAction('uses');
49+
50+
if (!starts_with($uri, '/')) {
51+
$uri = '/' . $uri;
52+
}
53+
54+
if (!isset($parsed['paths'][$uri])) {
55+
$parsed['paths'][$uri] = [];
56+
}
57+
58+
foreach ($methods as $method) {
59+
$method = strtolower($method);
60+
$parsed['paths'][$uri][$method] = [
61+
'description' => strtoupper($method) . ' ' . $uri,
62+
'responses' => [
63+
'200' => [
64+
'description' => 'OK'
65+
]
66+
],
67+
];
68+
69+
if ($rules = $this->getFormRules($action)) {
70+
$parsed['paths'][$uri][$method]['parameters'] = $this->getActionParameters($method, $rules);
71+
}
72+
}
73+
}
74+
75+
echo json_encode($parsed, JSON_PRETTY_PRINT) . "\n";
76+
}
77+
78+
protected function getFormRules($action)
79+
{
80+
if (!is_string($action)) return false;
81+
82+
$parsedAction = Str::parseCallback($action);
83+
84+
$parameters = (new ReflectionMethod($parsedAction[0], $parsedAction[1]))->getParameters();
85+
86+
foreach ($parameters as $parameter) {
87+
$class = (string) $parameter->getType();
88+
89+
if (is_subclass_of($class, FormRequest::class)) {
90+
return (new $class)->rules();
91+
}
92+
}
93+
}
94+
95+
protected function getActionParameters($method, $rules)
96+
{
97+
$params = [];
98+
99+
foreach ($rules as $param => $rule) {
100+
$paramRules = explode('|', $rule);
101+
102+
$params[] = [
103+
'in' => $this->getParamLocation($method),
104+
'name' => $param,
105+
'type' => $this->getParamType($paramRules),
106+
'required' => $this->isParamRequired($paramRules),
107+
'description' => '',
108+
];
109+
}
110+
111+
return $params;
112+
}
113+
114+
protected function getParamLocation($method)
115+
{
116+
return in_array($method, ['get', 'head']) ?
117+
'query':
118+
'body';
119+
}
120+
121+
protected function getParamType(array $paramRules)
122+
{
123+
if (in_array('integer', $paramRules)) {
124+
return 'integer';
125+
} else if (in_array('numeric', $paramRules)) {
126+
return 'number';
127+
} else if (in_array('boolean', $paramRules)) {
128+
return 'boolean';
129+
} else if (in_array('array', $paramRules)) {
130+
return 'array';
131+
} else {
132+
//date, ip, file, etc..
133+
return 'string';
134+
}
135+
}
136+
137+
protected function isParamRequired(array $paramRules)
138+
{
139+
return in_array('required', $paramRules);
140+
}
141+
}

src/SwaggerServiceProvider.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Mtrajano\LaravelSwagger;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class SwaggerServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* Bootstrap any application services.
11+
*
12+
* @return void
13+
*/
14+
public function boot()
15+
{
16+
if ($this->app->runningInConsole()) {
17+
$this->commands([
18+
GenerateSwaggerDoc::class,
19+
]);
20+
}
21+
22+
$source = __DIR__.'/../config/laravel-swagger.php';
23+
24+
$this->publishes([
25+
$source => config_path('laravel-swagger.php'),
26+
]);
27+
28+
$this->mergeConfigFrom(
29+
$source, 'laravel-swagger'
30+
);
31+
}
32+
}

0 commit comments

Comments
 (0)