Browse Source

handle patterns in include list also

Gabor Nemeth 6 years ago
parent
commit
89ec6b2389
4 changed files with 46 additions and 7 deletions
  1. 2 2
      README.md
  2. 4 4
      config/apidoc.php
  3. 1 1
      src/Tools/RouteMatcher.php
  4. 39 0
      tests/Unit/RouteMatcherTest.php

+ 2 - 2
README.md

@@ -69,8 +69,8 @@ return [
                   'prefixes' => ['api/*', 'v2-api/*'],
                   'versions' => ['v1'],
               ],
-              'include' => ['users.index'],
-              'exclude' => ['users.create'],
+              'include' => ['users.index', 'healthcheck*'],
+              'exclude' => ['users.create', 'admin.*'],
               'apply' => [
                   'headers' => [
                       'Authorization' => 'Bearer: {token}',

+ 4 - 4
config/apidoc.php

@@ -58,19 +58,19 @@ return [
             /*
              * Include these routes when generating documentation,
              * even if they did not match the rules above.
-             * Note that the route must be referenced by name here.
+             * Note that the route must be referenced by name here (wildcards are supported).
              */
             'include' => [
-                // 'users.index',
+                // 'users.index', 'healthcheck*'
             ],
 
             /*
              * Exclude these routes when generating documentation,
              * even if they matched the rules above.
-             * Note that the route must be referenced by name here.
+             * Note that the route must be referenced by name here (wildcards are supported).
              */
             'exclude' => [
-                // 'users.create',
+                // 'users.create', 'admin.*'
             ],
 
             /*

+ 1 - 1
src/Tools/RouteMatcher.php

@@ -68,7 +68,7 @@ class RouteMatcher
             ? ! empty(array_intersect($route->versions(), $routeRule['match']['versions'] ?? []))
             : true;
 
-        return in_array($route->getName(), $mustIncludes)
+        return str_is($mustIncludes, $route->getName())
             || (str_is($routeRule['match']['domains'] ?? [], $route->getDomain())
             && str_is($routeRule['match']['prefixes'] ?? [], $route->uri())
             && $matchesVersion);

+ 39 - 0
tests/Unit/RouteMatcherTest.php

@@ -198,6 +198,45 @@ class RouteMatcherTest extends TestCase
         $this->assertCount(1, $oddRuleOut);
     }
 
+    public function testWillIncludeRouteIfMatchForAnIncludePatternForLaravelRouter()
+    {
+        $this->registerLaravelRoutes();
+        $mustInclude = ['domain1-1', 'domain1-2'];
+        $includePattern = 'domain1-*';
+        $routeRules[0]['include'] = [$includePattern];
+
+        $routeRules[0]['match']['domains'] = ['domain1.*'];
+        $routeRules[0]['match']['prefixes'] = ['prefix1/*'];
+        $routes = $this->matcher->getRoutesToBeDocumented($routeRules);
+        $oddRuleOut = collect($routes)->filter(function ($route) use ($mustInclude) {
+            return in_array($route['route']->getName(), $mustInclude);
+        });
+        $this->assertCount(count($mustInclude), $oddRuleOut);
+    }
+
+    public function testWillIncludeRouteIfMatchForAnIncludePatternForDingoRouter()
+    {
+        $this->registerDingoRoutes();
+
+        $mustInclude = ['v2.domain1', 'v2.domain2'];
+        $includePattern = 'v2.domain*';
+        $routeRules = [
+            [
+                'match' => [
+                    'domains' => ['domain1.*'],
+                    'prefixes' => ['prefix1/*'],
+                    'versions' => ['v1'],
+                ],
+                'include' => [$includePattern],
+            ],
+        ];
+        $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules);
+        $oddRuleOut = collect($routes)->filter(function ($route) use ($mustInclude) {
+            return in_array($route['route']->getName(), $mustInclude);
+        });
+        $this->assertCount(count($mustInclude), $oddRuleOut);
+    }
+
     public function testWillExcludeRouteIfListedExplicitlyForLaravelRouter()
     {
         $this->registerLaravelRoutes();