|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mpociot\ApiDoc\Tools; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Routing\Route; |
|
6
|
|
|
use Dingo\Api\Routing\RouteCollection; |
|
7
|
|
|
use Illuminate\Support\Facades\Route as RouteFacade; |
|
8
|
|
|
|
|
9
|
|
|
class RouteMatcher |
|
10
|
|
|
{ |
|
11
|
|
|
public function getDingoRoutesToBeDocumented(array $routeRules) |
|
12
|
|
|
{ |
|
13
|
|
|
return $this->getRoutesToBeDocumented($routeRules, true); |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
public function getLaravelRoutesToBeDocumented(array $routeRules) |
|
17
|
|
|
{ |
|
18
|
|
|
return $this->getRoutesToBeDocumented($routeRules); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function getRoutesToBeDocumented(array $routeRules, bool $usingDingoRouter = false) |
|
22
|
|
|
{ |
|
23
|
|
|
$matchedRoutes = []; |
|
24
|
|
|
|
|
25
|
|
|
foreach ($routeRules as $routeRule) { |
|
26
|
|
|
$excludes = $routeRule['exclude'] ?? []; |
|
27
|
|
|
$includes = $routeRule['include'] ?? []; |
|
28
|
|
|
$allRoutes = $this->getAllRoutes($usingDingoRouter, $routeRule['match']['versions'] ?? []); |
|
29
|
|
|
|
|
30
|
|
|
foreach ($allRoutes as $route) { |
|
31
|
|
|
if (is_array($route)) { |
|
32
|
|
|
$route = new LumenRouteAdapter($route); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** @var Route $route */ |
|
36
|
|
|
if (in_array($route->getName(), $excludes)) { |
|
37
|
|
|
continue; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if ($this->shouldIncludeRoute($route, $routeRule, $includes, $usingDingoRouter)) { |
|
41
|
|
|
$matchedRoutes[] = [ |
|
42
|
|
|
'route' => $route, |
|
43
|
|
|
'apply' => $routeRule['apply'] ?? [], |
|
44
|
|
|
]; |
|
45
|
|
|
continue; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $matchedRoutes; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
private function getAllRoutes(bool $usingDingoRouter, array $versions = []) |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
|
if (! $usingDingoRouter) { |
|
56
|
|
|
return RouteFacade::getRoutes(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$allRouteCollections = app(\Dingo\Api\Routing\Router::class)->getRoutes(); |
|
60
|
|
|
|
|
61
|
|
|
return collect($allRouteCollections) |
|
62
|
|
|
->flatMap(function (RouteCollection $collection) { |
|
63
|
|
|
return $collection->getRoutes(); |
|
64
|
|
|
})->toArray(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
private function shouldIncludeRoute(Route $route, array $routeRule, array $mustIncludes, bool $usingDingoRouter) |
|
68
|
|
|
{ |
|
69
|
|
|
$matchesVersion = $usingDingoRouter |
|
70
|
|
|
? ! empty(array_intersect($route->versions(), $routeRule['match']['versions'] ?? [])) |
|
71
|
|
|
: true; |
|
72
|
|
|
|
|
73
|
|
|
return in_array($route->getName(), $mustIncludes) |
|
74
|
|
|
|| (str_is($routeRule['match']['domains'] ?? [], $route->getDomain()) |
|
75
|
|
|
&& str_is($routeRule['match']['prefixes'] ?? [], $route->uri()) |
|
76
|
|
|
&& $matchesVersion); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.