Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Option | Description
`output` | The output path used for the generated documentation. Default: `public/docs`
`routePrefix` | The route prefix to use for generation - `*` can be used as a wildcard
`routes` | The route names to use for generation - Required if no routePrefix is provided
`middleware` | The middlewares to use for generation
`noResponseCalls` | Disable API response calls
`noPostmanCollection` | Disable Postman collection creation
`actAsUserId` | The user ID to use for authenticated API response calls
Expand Down
18 changes: 10 additions & 8 deletions src/Mpociot/ApiDoc/Commands/GenerateDocumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class GenerateDocumentation extends Command
{--output=public/docs : The output path for the generated documentation}
{--routePrefix= : The route prefix to use for generation}
{--routes=* : The route names to use for generation}
{--middleware= : The middleware to use for generation}
{--noResponseCalls : Disable API response calls}
{--noPostmanCollection : Disable Postman collection creation}
{--actAsUserId= : The user ID to use for API response calls}
Expand Down Expand Up @@ -64,19 +65,20 @@ public function handle()

$allowedRoutes = $this->option('routes');
$routePrefix = $this->option('routePrefix');
$middleware = $this->option('middleware');

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

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

return false;
}

if ($this->option('router') === 'laravel') {
$parsedRoutes = $this->processLaravelRoutes($generator, $allowedRoutes, $routePrefix);
$parsedRoutes = $this->processLaravelRoutes($generator, $allowedRoutes, $routePrefix, $middleware);
} else {
$parsedRoutes = $this->processDingoRoutes($generator, $allowedRoutes, $routePrefix);
$parsedRoutes = $this->processDingoRoutes($generator, $allowedRoutes, $routePrefix, $middleware);
}
$parsedRoutes = collect($parsedRoutes)->groupBy('resource')->sortBy('resource');

Expand Down Expand Up @@ -242,14 +244,14 @@ private function getRoutes()
*
* @return array
*/
private function processLaravelRoutes(AbstractGenerator $generator, $allowedRoutes, $routePrefix)
private function processLaravelRoutes(AbstractGenerator $generator, $allowedRoutes, $routePrefix, $middleware)
{
$withResponse = $this->option('noResponseCalls') === false;
$routes = $this->getRoutes();
$bindings = $this->getBindings();
$parsedRoutes = [];
foreach ($routes as $route) {
if (in_array($route->getName(), $allowedRoutes) || str_is($routePrefix, $route->getUri())) {
if (in_array($route->getName(), $allowedRoutes) || str_is($routePrefix, $route->getUri()) || in_array($middleware, $route->middleware())) {
if ($this->isValidRoute($route) && $this->isRouteVisibleForDocumentation($route->getAction()['uses'])) {
$parsedRoutes[] = $generator->processRoute($route, $bindings, $withResponse);
$this->info('Processed route: ['.implode(',', $route->getMethods()).'] '.$route->getUri());
Expand All @@ -269,14 +271,14 @@ private function processLaravelRoutes(AbstractGenerator $generator, $allowedRout
*
* @return array
*/
private function processDingoRoutes(AbstractGenerator $generator, $allowedRoutes, $routePrefix)
private function processDingoRoutes(AbstractGenerator $generator, $allowedRoutes, $routePrefix, $middleware)
{
$withResponse = $this->option('noResponseCalls') === false;
$routes = $this->getRoutes();
$bindings = $this->getBindings();
$parsedRoutes = [];
foreach ($routes as $route) {
if (empty($allowedRoutes) || in_array($route->getName(), $allowedRoutes) || str_is($routePrefix, $route->uri())) {
if (empty($allowedRoutes) || in_array($route->getName(), $allowedRoutes) || str_is($routePrefix, $route->uri()) || in_array($middleware, $route->middleware())) {
$parsedRoutes[] = $generator->processRoute($route, $bindings, $withResponse);
$this->info('Processed route: ['.implode(',', $route->getMethods()).'] '.$route->uri());
}
Expand Down
2 changes: 1 addition & 1 deletion tests/GenerateDocumentationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected function getPackageProviders($app)
public function testConsoleCommandNeedsAPrefixOrRoute()
{
$output = $this->artisan('api:generate');
$this->assertEquals('You must provide either a route prefix or a route to generate the documentation.'.PHP_EOL, $output);
$this->assertEquals('You must provide either a route prefix or a route or a middleware to generate the documentation.'.PHP_EOL, $output);
}

public function testConsoleCommandDoesNotWorkWithClosure()
Expand Down