Skip to content

Commit c2b6766

Browse files
committed
clean up using dtos
1 parent 45b5b7c commit c2b6766

File tree

5 files changed

+121
-58
lines changed

5 files changed

+121
-58
lines changed

src/DataObjects/Middleware.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Mtrajano\LaravelSwagger\DataObjects;
4+
5+
class Middleware
6+
{
7+
private $name;
8+
private $parameters;
9+
10+
public function __construct(string $middleware)
11+
{
12+
$tokens = explode(':', $middleware, 2);
13+
$this->name = $tokens[0];
14+
$this->parameters = isset($tokens[1]) ? explode(',', $tokens[1]) : [];
15+
}
16+
17+
public function name()
18+
{
19+
return $this->name;
20+
}
21+
22+
public function parameters()
23+
{
24+
return $this->parameters;
25+
}
26+
}

src/DataObjects/Route.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Mtrajano\LaravelSwagger\DataObjects;
4+
5+
use Illuminate\Routing\Route as LaravelRoute;
6+
use Illuminate\Support\Arr;
7+
use Illuminate\Support\Str;
8+
9+
class Route
10+
{
11+
private $route;
12+
private $middleware;
13+
14+
public function __construct(LaravelRoute $route)
15+
{
16+
$this->route = $route;
17+
$this->middleware = $this->formatMiddleware();
18+
}
19+
20+
public function originalUri()
21+
{
22+
$uri = $this->route->uri();
23+
24+
if (!Str::startsWith($uri, '/')) {
25+
$uri = '/' . $uri;
26+
}
27+
28+
return $uri;
29+
}
30+
31+
public function uri()
32+
{
33+
return strip_optional_char($this->originalUri());
34+
}
35+
36+
public function middleware()
37+
{
38+
return $this->middleware;
39+
}
40+
41+
public function action()
42+
{
43+
return $this->route->getAction()['uses'];
44+
}
45+
46+
public function methods()
47+
{
48+
return array_map('strtolower', $this->route->methods());
49+
}
50+
51+
protected function formatMiddleware()
52+
{
53+
$middleware = $this->route->getAction()['middleware'] ?? [];
54+
55+
return array_map(function ($middleware) {
56+
return new Middleware($middleware);
57+
}, Arr::wrap($middleware));
58+
}
59+
}

src/Generator.php

Lines changed: 21 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
namespace Mtrajano\LaravelSwagger;
44

55
use Illuminate\Foundation\Http\FormRequest;
6-
use Illuminate\Routing\Route;
76
use Illuminate\Support\Str;
8-
use Illuminate\Support\Arr;
97
use phpDocumentor\Reflection\DocBlockFactory;
108
use ReflectionMethod;
119

@@ -18,11 +16,8 @@ class Generator
1816
protected $config;
1917
protected $routeFilter;
2018
protected $docs;
21-
protected $uri;
22-
protected $originalUri;
19+
protected $route;
2320
protected $method;
24-
protected $action;
25-
protected $middleware;
2621
protected $docParser;
2722
protected $hasSecurityDefinitions;
2823

@@ -44,26 +39,18 @@ public function generate()
4439
}
4540

4641
foreach ($this->getAppRoutes() as $route) {
47-
$this->originalUri = $uri = $this->getRouteUri($route);
48-
$this->uri = strip_optional_char($uri);
42+
$this->route = $route;
4943

5044
if ($this->routeFilter && $this->isFilteredRoute()) {
5145
continue;
5246
}
5347

54-
$middleware = isset($route->getAction()['middleware']) ? $route->getAction()['middleware'] : [];
55-
56-
$this->action = $route->getAction()['uses'];
57-
$this->middleware = $this->formatMiddleware($middleware);
58-
59-
$methods = $route->methods();
60-
61-
if (!isset($this->docs['paths'][$this->uri])) {
62-
$this->docs['paths'][$this->uri] = [];
48+
if (!isset($this->docs['paths'][$this->route->uri()])) {
49+
$this->docs['paths'][$this->route->uri()] = [];
6350
}
6451

65-
foreach ($methods as $method) {
66-
$this->method = strtolower($method);
52+
foreach ($route->methods() as $method) {
53+
$this->method = $method;
6754

6855
if (in_array($this->method, $this->config['ignoredMethods'])) {
6956
continue;
@@ -108,18 +95,9 @@ protected function getBaseInfo()
10895

10996
protected function getAppRoutes()
11097
{
111-
return app('router')->getRoutes();
112-
}
113-
114-
protected function getRouteUri(Route $route)
115-
{
116-
$uri = $route->uri();
117-
118-
if (!Str::startsWith($uri, '/')) {
119-
$uri = '/' . $uri;
120-
}
121-
122-
return $uri;
98+
return array_map(function ($route) {
99+
return new DataObjects\Route($route);
100+
}, app('router')->getRoutes()->getRoutes());
123101
}
124102

125103
protected function generateSecurityDefinitions()
@@ -151,12 +129,12 @@ protected function generateSecurityDefinitions()
151129

152130
protected function generatePath()
153131
{
154-
$actionInstance = is_string($this->action) ? $this->getActionClassInstance($this->action) : null;
132+
$actionInstance = is_string($this->route->action()) ? $this->getActionClassInstance($this->route->action()) : null;
155133
$docBlock = $actionInstance ? ($actionInstance->getDocComment() ?: '') : '';
156134

157135
[$isDeprecated, $summary, $description] = $this->parseActionDocBlock($docBlock);
158136

159-
$this->docs['paths'][$this->uri][$this->method] = [
137+
$this->docs['paths'][$this->route->uri()][$this->method] = [
160138
'summary' => $summary,
161139
'description' => $description,
162140
'deprecated' => $isDeprecated,
@@ -178,7 +156,7 @@ protected function addActionParameters()
178156
{
179157
$rules = $this->getFormRules() ?: [];
180158

181-
$parameters = (new Parameters\PathParameterGenerator($this->originalUri))->getParameters();
159+
$parameters = (new Parameters\PathParameterGenerator($this->route->originalUri()))->getParameters();
182160

183161
if (!empty($rules)) {
184162
$parameterGenerator = $this->getParameterGenerator($rules);
@@ -187,28 +165,28 @@ protected function addActionParameters()
187165
}
188166

189167
if (!empty($parameters)) {
190-
$this->docs['paths'][$this->uri][$this->method]['parameters'] = $parameters;
168+
$this->docs['paths'][$this->route->uri()][$this->method]['parameters'] = $parameters;
191169
}
192170
}
193171

194172
protected function addActionScopes()
195173
{
196-
foreach ($this->middleware as $middleware) {
197-
if ($middleware['name'] === 'scope' || $middleware['name'] === 'scopes') {
198-
$this->docs['paths'][$this->uri][$this->method]['security'] = [
199-
self::SECURITY_DEFINITION_NAME => $middleware['parameters']
174+
foreach ($this->route->middleware() as $middleware) {
175+
if ($middleware->name() === 'scope' || $middleware->name() === 'scopes') {
176+
$this->docs['paths'][$this->route->uri()][$this->method]['security'] = [
177+
self::SECURITY_DEFINITION_NAME => $middleware->parameters()
200178
];
201179
}
202180
}
203181
}
204182

205183
protected function getFormRules()
206184
{
207-
if (!is_string($this->action)) {
185+
if (!is_string($this->route->action())) {
208186
return false;
209187
}
210188

211-
$parameters = $this->getActionClassInstance($this->action)->getParameters();
189+
$parameters = $this->getActionClassInstance($this->route->action())->getParameters();
212190

213191
foreach ($parameters as $parameter) {
214192
$class = (string) $parameter->getClass();
@@ -260,7 +238,7 @@ private function parseActionDocBlock(string $docBlock)
260238

261239
private function isFilteredRoute()
262240
{
263-
return !preg_match('/^' . preg_quote($this->routeFilter, '/') . '/', $this->uri);
241+
return !preg_match('/^' . preg_quote($this->routeFilter, '/') . '/', $this->route->uri());
264242
}
265243

266244
/**
@@ -269,7 +247,7 @@ private function isFilteredRoute()
269247
private function hasOauthRoutes()
270248
{
271249
foreach ($this->getAppRoutes() as $route) {
272-
$uri = $this->getRouteUri($route);
250+
$uri = $route->uri();
273251

274252
if ($uri === self::OAUTH_TOKEN_PATH || $uri === self::OAUTH_AUTHORIZE_PATH) {
275253
return true;
@@ -301,17 +279,4 @@ private function validateAuthFlow(string $flow)
301279
throw new LaravelSwaggerException('Invalid OAuth flow passed');
302280
}
303281
}
304-
305-
private function formatMiddleware($middleware)
306-
{
307-
$middleware = Arr::wrap($middleware);
308-
309-
return array_map(function($mw) {
310-
$tokens = explode(':', $mw, 2);
311-
$name = $tokens[0];
312-
$parameters = isset($tokens[1]) ? explode(',', $tokens[1]) : [];
313-
314-
return compact('name', 'parameters');
315-
}, $middleware);
316-
}
317282
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Mtrajano\LaravelSwagger\Tests\Stubs\Middleware;
4+
5+
class RandomMiddleware
6+
{
7+
public function handle($request, $next)
8+
{
9+
return $next($request);
10+
}
11+
}

tests/TestCase.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
namespace Mtrajano\LaravelSwagger\Tests;
44

5-
use Orchestra\Testbench\TestCase as OrchestraTestCase;
65
use Laravel\Passport\Passport;
6+
use Mtrajano\LaravelSwagger\Tests\Stubs\Middleware\RandomMiddleware;
7+
use Orchestra\Testbench\TestCase as OrchestraTestCase;
78

89
class TestCase extends OrchestraTestCase
910
{
@@ -25,7 +26,8 @@ protected function getEnvironmentSetUp($app)
2526
});
2627
});
2728

28-
$app['router']->get('/api', 'Mtrajano\\LaravelSwagger\\Tests\\Stubs\\Controllers\\ApiController@index');
29+
$app['router']->get('/api', 'Mtrajano\\LaravelSwagger\\Tests\\Stubs\\Controllers\\ApiController@index')
30+
->middleware(RandomMiddleware::class);
2931
$app['router']->put('/api/store', 'Mtrajano\\LaravelSwagger\\Tests\\Stubs\\Controllers\\ApiController@store');
3032

3133
Passport::routes();

0 commit comments

Comments
 (0)