1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mpociot\ApiDoc\Matching; |
4
|
|
|
|
5
|
|
|
use Dingo\Api\Routing\RouteCollection; |
6
|
|
|
use Illuminate\Routing\Route; |
7
|
|
|
use Illuminate\Support\Facades\Route as RouteFacade; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
use Mpociot\ApiDoc\Matching\RouteMatcher\Match; |
10
|
|
|
|
11
|
|
|
class RouteMatcher implements RouteMatcherInterface |
12
|
|
|
{ |
13
|
|
|
public function getRoutes(array $routeRules = [], string $router = 'laravel') |
14
|
|
|
{ |
15
|
|
|
$usingDingoRouter = strtolower($router) == 'dingo'; |
16
|
|
|
|
17
|
|
|
return $this->getRoutesToBeDocumented($routeRules, $usingDingoRouter); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
private function getRoutesToBeDocumented(array $routeRules, bool $usingDingoRouter = false) |
21
|
|
|
{ |
22
|
|
|
$allRoutes = $this->getAllRoutes($usingDingoRouter); |
23
|
|
|
|
24
|
|
|
$matchedRoutes = []; |
25
|
|
|
|
26
|
|
|
foreach ($routeRules as $routeRule) { |
27
|
|
|
$includes = $routeRule['include'] ?? []; |
28
|
|
|
|
29
|
|
|
foreach ($allRoutes as $route) { |
30
|
|
|
if (is_array($route)) { |
31
|
|
|
$route = new LumenRouteAdapter($route); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if ($this->shouldExcludeRoute($route, $routeRule)) { |
35
|
|
|
continue; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if ($this->shouldIncludeRoute($route, $routeRule, $includes, $usingDingoRouter)) { |
39
|
|
|
$matchedRoutes[] = new Match($route, $routeRule['apply'] ?? []); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $matchedRoutes; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private function getAllRoutes(bool $usingDingoRouter) |
48
|
|
|
{ |
49
|
|
|
if (! $usingDingoRouter) { |
50
|
|
|
return RouteFacade::getRoutes(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$allRouteCollections = app(\Dingo\Api\Routing\Router::class)->getRoutes(); |
54
|
|
|
|
55
|
|
|
return collect($allRouteCollections) |
56
|
|
|
->flatMap(function (RouteCollection $collection) { |
57
|
|
|
return $collection->getRoutes(); |
58
|
|
|
})->toArray(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function shouldIncludeRoute(Route $route, array $routeRule, array $mustIncludes, bool $usingDingoRouter) |
62
|
|
|
{ |
63
|
|
|
$matchesVersion = $usingDingoRouter |
64
|
|
|
? ! empty(array_intersect($route->versions(), $routeRule['match']['versions'] ?? [])) |
65
|
|
|
: true; |
66
|
|
|
|
67
|
|
|
return Str::is($mustIncludes, $route->getName()) |
68
|
|
|
|| Str::is($mustIncludes, $route->uri()) |
69
|
|
|
|| (Str::is($routeRule['match']['domains'] ?? [], $route->getDomain()) |
70
|
|
|
&& Str::is($routeRule['match']['prefixes'] ?? [], $route->uri()) |
71
|
|
|
&& $matchesVersion); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private function shouldExcludeRoute(Route $route, array $routeRule) |
75
|
|
|
{ |
76
|
|
|
$excludes = $routeRule['exclude'] ?? []; |
77
|
|
|
|
78
|
|
|
// Exclude this package's routes |
79
|
|
|
$excludes[] = 'apidoc'; |
80
|
|
|
|
81
|
|
|
// Exclude Laravel Telescope routes |
82
|
|
|
if (class_exists("Laravel\Telescope\Telescope")) { |
83
|
|
|
$excludes[] = 'telescope/*'; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return Str::is($excludes, $route->getName()) |
87
|
|
|
|| Str::is($excludes, $route->uri()); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|