Skip to content

Commit 18a576f

Browse files
committed
generator class
1 parent 3233d33 commit 18a576f

File tree

2 files changed

+156
-108
lines changed

2 files changed

+156
-108
lines changed

src/GenerateSwaggerDoc.php

Lines changed: 3 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
namespace Mtrajano\LaravelSwagger;
44

5-
use ReflectionMethod;
6-
use Illuminate\Support\Str;
75
use Illuminate\Console\Command;
8-
use Illuminate\Foundation\Http\FormRequest;
96

107
class GenerateSwaggerDoc extends Command
118
{
@@ -30,112 +27,10 @@ class GenerateSwaggerDoc extends Command
3027
*/
3128
public function handle()
3229
{
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-
];
30+
$config = config('laravel-swagger');
4431

45-
foreach (app('routes')->get() as $route) {
46-
$uri = $route->uri();
47-
$methods = $route->methods();
48-
$action = $route->getAction('uses');
32+
$docs = (new Generator($config))->generate();
4933

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);
34+
echo json_encode($docs, JSON_PRETTY_PRINT) . "\n";
14035
}
14136
}

src/Generator.php

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

0 commit comments

Comments
 (0)