Sfoglia il codice sorgente

Merge branch 'master' of github.com:mpociot/laravel-apidoc-generator

Marcel Pociot 8 anni fa
parent
commit
438eea7633

+ 1 - 0
README.md

@@ -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

+ 10 - 8
src/Mpociot/ApiDoc/Commands/GenerateDocumentation.php

@@ -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}
@@ -64,19 +65,20 @@ class GenerateDocumentation extends Command
 
         $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');
 
@@ -242,14 +244,14 @@ class GenerateDocumentation extends Command
      *
      * @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());
@@ -269,14 +271,14 @@ class GenerateDocumentation extends Command
      *
      * @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());
             }

+ 1 - 0
src/Mpociot/ApiDoc/Generators/DingoGenerator.php

@@ -46,6 +46,7 @@ class DingoGenerator extends AbstractGenerator
     public function callRoute($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
     {
         $dispatcher = app('Dingo\Api\Dispatcher')->raw();
+
         return call_user_func_array([$dispatcher, strtolower($method)], [$uri]);
     }
 

+ 1 - 1
tests/GenerateDocumentationTest.php

@@ -45,7 +45,7 @@ class GenerateDocumentationTest extends TestCase
     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()