Skip to content

Commit 976f5ed

Browse files
yespbsmpociot
authored andcommitted
Middleware generate (#92)
* added middleware generate support * added middleware generate support * added middleware generate support * added middleware generate support to dingo
1 parent 8a45951 commit 976f5ed

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Option | Description
4343
`output` | The output path used for the generated documentation. Default: `public/docs`
4444
`routePrefix` | The route prefix to use for generation - `*` can be used as a wildcard
4545
`routes` | The route names to use for generation - Required if no routePrefix is provided
46+
`middleware` | The middlewares to use for generation
4647
`noResponseCalls` | Disable API response calls
4748
`noPostmanCollection` | Disable Postman collection creation
4849
`actAsUserId` | The user ID to use for authenticated API response calls

src/Mpociot/ApiDoc/Commands/GenerateDocumentation.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class GenerateDocumentation extends Command
2424
{--output=public/docs : The output path for the generated documentation}
2525
{--routePrefix= : The route prefix to use for generation}
2626
{--routes=* : The route names to use for generation}
27+
{--middleware= : The middleware to use for generation}
2728
{--noResponseCalls : Disable API response calls}
2829
{--noPostmanCollection : Disable Postman collection creation}
2930
{--actAsUserId= : The user ID to use for API response calls}
@@ -64,19 +65,20 @@ public function handle()
6465

6566
$allowedRoutes = $this->option('routes');
6667
$routePrefix = $this->option('routePrefix');
68+
$middleware = $this->option('middleware');
6769

6870
$this->setUserToBeImpersonated($this->option('actAsUserId'));
6971

70-
if ($routePrefix === null && ! count($allowedRoutes)) {
71-
$this->error('You must provide either a route prefix or a route to generate the documentation.');
72+
if ($routePrefix === null && ! count($allowedRoutes) && $middleware === null) {
73+
$this->error('You must provide either a route prefix or a route or a middleware to generate the documentation.');
7274

7375
return false;
7476
}
7577

7678
if ($this->option('router') === 'laravel') {
77-
$parsedRoutes = $this->processLaravelRoutes($generator, $allowedRoutes, $routePrefix);
79+
$parsedRoutes = $this->processLaravelRoutes($generator, $allowedRoutes, $routePrefix, $middleware);
7880
} else {
79-
$parsedRoutes = $this->processDingoRoutes($generator, $allowedRoutes, $routePrefix);
81+
$parsedRoutes = $this->processDingoRoutes($generator, $allowedRoutes, $routePrefix, $middleware);
8082
}
8183
$parsedRoutes = collect($parsedRoutes)->groupBy('resource')->sortBy('resource');
8284

@@ -242,14 +244,14 @@ private function getRoutes()
242244
*
243245
* @return array
244246
*/
245-
private function processLaravelRoutes(AbstractGenerator $generator, $allowedRoutes, $routePrefix)
247+
private function processLaravelRoutes(AbstractGenerator $generator, $allowedRoutes, $routePrefix, $middleware)
246248
{
247249
$withResponse = $this->option('noResponseCalls') === false;
248250
$routes = $this->getRoutes();
249251
$bindings = $this->getBindings();
250252
$parsedRoutes = [];
251253
foreach ($routes as $route) {
252-
if (in_array($route->getName(), $allowedRoutes) || str_is($routePrefix, $route->getUri())) {
254+
if (in_array($route->getName(), $allowedRoutes) || str_is($routePrefix, $route->getUri()) || in_array($middleware, $route->middleware())) {
253255
if ($this->isValidRoute($route) && $this->isRouteVisibleForDocumentation($route->getAction()['uses'])) {
254256
$parsedRoutes[] = $generator->processRoute($route, $bindings, $withResponse);
255257
$this->info('Processed route: ['.implode(',', $route->getMethods()).'] '.$route->getUri());
@@ -269,14 +271,14 @@ private function processLaravelRoutes(AbstractGenerator $generator, $allowedRout
269271
*
270272
* @return array
271273
*/
272-
private function processDingoRoutes(AbstractGenerator $generator, $allowedRoutes, $routePrefix)
274+
private function processDingoRoutes(AbstractGenerator $generator, $allowedRoutes, $routePrefix, $middleware)
273275
{
274276
$withResponse = $this->option('noResponseCalls') === false;
275277
$routes = $this->getRoutes();
276278
$bindings = $this->getBindings();
277279
$parsedRoutes = [];
278280
foreach ($routes as $route) {
279-
if (empty($allowedRoutes) || in_array($route->getName(), $allowedRoutes) || str_is($routePrefix, $route->uri())) {
281+
if (empty($allowedRoutes) || in_array($route->getName(), $allowedRoutes) || str_is($routePrefix, $route->uri()) || in_array($middleware, $route->middleware())) {
280282
$parsedRoutes[] = $generator->processRoute($route, $bindings, $withResponse);
281283
$this->info('Processed route: ['.implode(',', $route->getMethods()).'] '.$route->uri());
282284
}

tests/GenerateDocumentationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ protected function getPackageProviders($app)
4545
public function testConsoleCommandNeedsAPrefixOrRoute()
4646
{
4747
$output = $this->artisan('api:generate');
48-
$this->assertEquals('You must provide either a route prefix or a route to generate the documentation.'.PHP_EOL, $output);
48+
$this->assertEquals('You must provide either a route prefix or a route or a middleware to generate the documentation.'.PHP_EOL, $output);
4949
}
5050

5151
public function testConsoleCommandDoesNotWorkWithClosure()

0 commit comments

Comments
 (0)