浏览代码

Added ability to use @hidefromapidocumentation on classes

shalvah 5 年之前
父节点
当前提交
42d43a7f09

+ 1 - 1
docs/documenting.md

@@ -10,4 +10,4 @@ Here's some of the information you can customise:
 - [Example responses returned from the endpoint](documenting-endpoint-responses.html)
 
 ## Excluding endpoints from the documentation
-You can exclude endpoints from the documentation by using the `@hideFromAPIDocumentation` tag in the method's doc block. Scribe will not extract any information about the route or add it to the generated docs.
+You can exclude endpoints from the documentation by using the `@hideFromAPIDocumentation` tag in the method or class doc block. Scribe will not extract any information about the route or add it to the generated docs.

+ 2 - 1
docs/guide-getting-started.md

@@ -51,7 +51,8 @@ For details, check out [Documenting API information](documenting-api-information
 Next up, decide what routes you want to document. This is configured in the `routes` key of `scribe.php`. By default, Scribe will try to document all of your routes, so if you're okay with that, you can leave it at that.
 
 If you'd like to exclude some routes, there are two ways:
-- In the docblock for the controller method or class, add this tag: `@hideFromAPIDocumentation`. Any routes handled by methods or controllers with this DocBlock tag won't be added to the doc.
+
+- In the docblock for the controller method or class, add this tag: `@hideFromAPIDocumentation`. Any routes handled by methods or controllers with this doc block tag won't be added to the doc.
 
 - The second way is by configuring your `routes` config. Here's what it looks like:
 

+ 14 - 7
src/Commands/GenerateDocumentation.php

@@ -172,18 +172,25 @@ class GenerateDocumentation extends Command
      */
     private function isRouteHiddenFromDocumentation(array $routeControllerAndMethod)
     {
-        $comment = u::reflectRouteMethod($routeControllerAndMethod)->getDocComment();
-
-        if (!$comment) {
-            return false;
+        if (! ($class = $routeControllerAndMethod[0]) instanceof \Closure) {
+            $classDocBlock = new DocBlock((new ReflectionClass($class))->getDocComment() ?: '');
+            $shouldIgnoreClass = collect($classDocBlock->getTags())
+                ->filter(function (Tag $tag) {
+                    return Str::lower($tag->getName()) === 'hidefromapidocumentation';
+                })->isNotEmpty();
+
+            if ($shouldIgnoreClass) {
+                return true;
+            }
         }
 
-        $phpdoc = new DocBlock($comment);
-
-        return collect($phpdoc->getTags())
+        $methodDocBlock = new DocBlock(u::getReflectedRouteMethod($routeControllerAndMethod)->getDocComment() ?: '');
+        $shouldIgnoreMethod = collect($methodDocBlock->getTags())
             ->filter(function (Tag $tag) {
                 return Str::lower($tag->getName()) === 'hidefromapidocumentation';
             })->isNotEmpty();
+
+        return $shouldIgnoreMethod;
     }
 
     public function bootstrap(): void

+ 1 - 1
src/Extracting/Generator.php

@@ -58,7 +58,7 @@ class Generator
     {
         [$controllerName, $methodName] = u::getRouteClassAndMethodNames($route);
         $controller = new ReflectionClass($controllerName);
-        $method = u::reflectRouteMethod([$controllerName, $methodName]);
+        $method = u::getReflectedRouteMethod([$controllerName, $methodName]);
 
         $parsedRoute = [
             'id' => md5($this->getUri($route) . ':' . implode($this->getMethods($route))),

+ 1 - 1
src/Extracting/RouteDocBlocker.php

@@ -41,7 +41,7 @@ class RouteDocBlocker
             throw new \Exception("Error while fetching docblock for route ". c::getRouteRepresentation($route).": Class $className does not contain method $methodName");
         }
 
-        $method = u::reflectRouteMethod([$className, $methodName]);
+        $method = u::getReflectedRouteMethod([$className, $methodName]);
 
         $docBlocks = [
             'method' => new DocBlock($method->getDocComment() ?: ''),

+ 3 - 1
src/Tools/Utils.php

@@ -97,13 +97,15 @@ class Utils
     }
 
     /**
+     * Returns the route method or closure as an instance of ReflectionMethod or ReflectionFunction
+     *
      * @param array $routeControllerAndMethod
      *
      * @throws ReflectionException
      *
      * @return ReflectionFunctionAbstract
      */
-    public static function reflectRouteMethod(array $routeControllerAndMethod): ReflectionFunctionAbstract
+    public static function getReflectedRouteMethod(array $routeControllerAndMethod): ReflectionFunctionAbstract
     {
         [$class, $method] = $routeControllerAndMethod;
 

+ 17 - 0
tests/Fixtures/TestIgnoreThisController.php

@@ -0,0 +1,17 @@
+<?php
+
+namespace Knuckles\Scribe\Tests\Fixtures;
+
+use Illuminate\Routing\Controller;
+
+/**
+ * @hideFromAPIDocumentation
+ * @group Group A
+ */
+class TestIgnoreThisController extends Controller
+{
+    public function dummy()
+    {
+        return '';
+    }
+}

+ 4 - 1
tests/GenerateDocumentationTest.php

@@ -9,6 +9,7 @@ use Illuminate\Support\Str;
 use Knuckles\Scribe\ScribeServiceProvider;
 use Knuckles\Scribe\Tests\Fixtures\TestController;
 use Knuckles\Scribe\Tests\Fixtures\TestGroupController;
+use Knuckles\Scribe\Tests\Fixtures\TestIgnoreThisController;
 use Knuckles\Scribe\Tests\Fixtures\TestPartialResourceController;
 use Knuckles\Scribe\Tests\Fixtures\TestResourceController;
 use Knuckles\Scribe\Tests\Fixtures\TestUser;
@@ -116,15 +117,17 @@ class GenerateDocumentationTest extends TestCase
     }
 
     /** @test */
-    public function can_skip_single_routes()
+    public function can_skip_methods_and_classes_with_hidefromapidocumentation_tag()
     {
         RouteFacade::get('/api/skip', TestController::class . '@skip');
+        RouteFacade::get('/api/skipClass', TestIgnoreThisController::class . '@dummy');
         RouteFacade::get('/api/test', TestController::class . '@withEndpointDescription');
 
         config(['scribe.routes.0.match.prefixes' => ['api/*']]);
         $output = $this->artisan('scribe:generate');
 
         $this->assertStringContainsString('Skipping route: [GET] api/skip', $output);
+        $this->assertStringContainsString('Skipping route: [GET] api/skipClass', $output);
         $this->assertStringContainsString('Processed route: [GET] api/test', $output);
     }