Sfoglia il codice sorgente

handle patterns in exclude list

Gabor Nemeth 6 anni fa
parent
commit
0622141e0b
2 ha cambiato i file con 47 aggiunte e 3 eliminazioni
  1. 8 3
      src/Tools/RouteMatcher.php
  2. 39 0
      tests/Unit/RouteMatcherTest.php

+ 8 - 3
src/Tools/RouteMatcher.php

@@ -23,7 +23,6 @@ class RouteMatcher
         $matchedRoutes = [];
 
         foreach ($routeRules as $routeRule) {
-            $excludes = $routeRule['exclude'] ?? [];
             $includes = $routeRule['include'] ?? [];
             $allRoutes = $this->getAllRoutes($usingDingoRouter, $routeRule['match']['versions'] ?? []);
 
@@ -32,8 +31,7 @@ class RouteMatcher
                     $route = new LumenRouteAdapter($route);
                 }
 
-                /** @var Route $route */
-                if (in_array($route->getName(), $excludes)) {
+                if ($this->shouldExcludeRoute($route, $routeRule)) {
                     continue;
                 }
 
@@ -75,4 +73,11 @@ class RouteMatcher
             && str_is($routeRule['match']['prefixes'] ?? [], $route->uri())
             && $matchesVersion);
     }
+
+    private function shouldExcludeRoute(Route $route, array $routeRule)
+    {
+        $excludes = $routeRule['exclude'] ?? [];
+
+        return str_is($excludes, $route->getName());
+    }
 }

+ 39 - 0
tests/Unit/RouteMatcherTest.php

@@ -235,6 +235,45 @@ class RouteMatcherTest extends TestCase
         $this->assertCount(0, $oddRuleOut);
     }
 
+    public function testWillExcludeRouteIfMatchForAnExcludePatternForLaravelRouter()
+    {
+        $this->registerLaravelRoutes();
+        $mustNotInclude = ['prefix1.domain1-1', 'prefix1.domain1-2'];
+        $excludePattern = 'prefix1.domain1-*';
+        $routeRules[0]['exclude'] = [$excludePattern];
+
+        $routeRules[0]['match']['domains'] = ['domain1.*'];
+        $routeRules[0]['match']['prefixes'] = ['prefix1/*'];
+        $routes = $this->matcher->getRoutesToBeDocumented($routeRules);
+        $oddRuleOut = collect($routes)->filter(function ($route) use ($mustNotInclude) {
+            return in_array($route['route']->getName(), $mustNotInclude);
+        });
+        $this->assertCount(0, $oddRuleOut);
+    }
+
+    public function testWillExcludeRouteIfMatchForAnExcludePatterForDingoRouter()
+    {
+        $this->registerDingoRoutes();
+
+        $mustNotInclude = ['v2.prefix1.domain2', 'v2.prefix2.domain2'];
+        $excludePattern = 'v2.*.domain2';
+        $routeRules = [
+            [
+                'match' => [
+                    'domains' => ['domain2.*'],
+                    'prefixes' => ['*'],
+                    'versions' => ['v2'],
+                ],
+                'exclude' => [$excludePattern],
+            ],
+        ];
+        $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules);
+        $oddRuleOut = collect($routes)->filter(function ($route) use ($mustNotInclude) {
+            return in_array($route['route']->getName(), $mustNotInclude);
+        });
+        $this->assertCount(0, $oddRuleOut);
+    }
+
     public function testMergesRoutesFromDifferentRuleGroupsForLaravelRouter()
     {
         $this->registerLaravelRoutes();