Skip to content

Commit 408d735

Browse files
committed
NEW RouteMatcher can now be extended in user code
- Created a `RouteMatcherInterface` to describe the required API of a route matcher - Created `Match` to provide better type saftey for the return value of the route matcher - The `RouteMatcher` is now resolved through the application container
1 parent 672c4f2 commit 408d735

File tree

7 files changed

+183
-90
lines changed

7 files changed

+183
-90
lines changed

config/apidoc.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,11 @@
242242
*
243243
*/
244244
'faker_seed' => null,
245+
246+
/**
247+
* If you would like to customize how routes are matched beyond the route configuration you may
248+
* declare your own implementation of RouteMatcherInterface
249+
*
250+
*/
251+
'routeMatcher' => \Mpociot\ApiDoc\Matching\RouteMatcher::class,
245252
];

src/ApiDocGeneratorServiceProvider.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Illuminate\Support\ServiceProvider;
66
use Mpociot\ApiDoc\Commands\GenerateDocumentation;
77
use Mpociot\ApiDoc\Commands\RebuildDocumentation;
8+
use Mpociot\ApiDoc\Matching\RouteMatcher;
9+
use Mpociot\ApiDoc\Matching\RouteMatcherInterface;
810

911
class ApiDocGeneratorServiceProvider extends ServiceProvider
1012
{
@@ -33,6 +35,9 @@ public function boot()
3335
RebuildDocumentation::class,
3436
]);
3537
}
38+
39+
// Bind the route matcher implementation
40+
$this->app->bind(RouteMatcherInterface::class, $this->app['config']['apidoc']['routeMatcher']);
3641
}
3742

3843
/**
@@ -42,6 +47,5 @@ public function boot()
4247
*/
4348
public function register()
4449
{
45-
//
4650
}
4751
}

src/Commands/GenerateDocumentation.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
use Illuminate\Support\Collection;
88
use Illuminate\Support\Facades\URL;
99
use Mpociot\ApiDoc\Extracting\Generator;
10-
use Mpociot\ApiDoc\Matching\RouteMatcher;
10+
use Mpociot\ApiDoc\Matching\RouteMatcher\Match;
11+
use Mpociot\ApiDoc\Matching\RouteMatcherInterface;
1112
use Mpociot\ApiDoc\Tools\DocumentationConfig;
1213
use Mpociot\ApiDoc\Tools\Flags;
1314
use Mpociot\ApiDoc\Tools\Utils;
@@ -44,8 +45,15 @@ class GenerateDocumentation extends Command
4445
*/
4546
private $baseUrl;
4647

47-
public function __construct()
48+
/**
49+
* @var RouteMatcherInterface
50+
*/
51+
private $routeMatcher;
52+
53+
public function __construct(RouteMatcherInterface $routeMatcher)
4854
{
55+
$this->routeMatcher = $routeMatcher;
56+
4957
parent::__construct();
5058
}
5159

@@ -65,8 +73,7 @@ public function handle()
6573

6674
URL::forceRootUrl($this->baseUrl);
6775

68-
$routeMatcher = new RouteMatcher($this->docConfig->get('routes'), $this->docConfig->get('router'));
69-
$routes = $routeMatcher->getRoutes();
76+
$routes = $this->routeMatcher->getRoutes($this->docConfig->get('routes'), $this->docConfig->get('router'));
7077

7178
$generator = new Generator($this->docConfig);
7279
$parsedRoutes = $this->processRoutes($generator, $routes);
@@ -87,15 +94,15 @@ public function handle()
8794

8895
/**
8996
* @param \Mpociot\ApiDoc\Extracting\Generator $generator
90-
* @param array $routes
97+
* @param Match[] $routes
9198
*
9299
* @return array
93100
*/
94101
private function processRoutes(Generator $generator, array $routes)
95102
{
96103
$parsedRoutes = [];
97104
foreach ($routes as $routeItem) {
98-
$route = $routeItem['route'];
105+
$route = $routeItem->getRoute();
99106
/** @var Route $route */
100107
$messageFormat = '%s route: [%s] %s';
101108
$routeMethods = implode(',', $generator->getMethods($route));
@@ -107,7 +114,7 @@ private function processRoutes(Generator $generator, array $routes)
107114
}
108115

109116
try {
110-
$parsedRoutes[] = $generator->processRoute($route, $routeItem['apply'] ?? []);
117+
$parsedRoutes[] = $generator->processRoute($route, $routeItem->getRules());
111118
$this->info(sprintf($messageFormat, 'Processed', $routeMethods, $routePath));
112119
} catch (\Exception $exception) {
113120
$this->warn(sprintf($messageFormat, 'Skipping', $routeMethods, $routePath).' - '.$exception->getMessage());

src/Matching/RouteMatcher.php

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,21 @@
66
use Illuminate\Routing\Route;
77
use Illuminate\Support\Facades\Route as RouteFacade;
88
use Illuminate\Support\Str;
9+
use Mpociot\ApiDoc\Matching\RouteMatcher\Match;
910

10-
class RouteMatcher
11+
class RouteMatcher implements RouteMatcherInterface
1112
{
12-
/**
13-
* @var string
14-
*/
15-
protected $router;
16-
17-
/**
18-
* @var array
19-
*/
20-
protected $routeRules;
21-
22-
public function __construct(array $routeRules = [], string $router = 'laravel')
13+
public function getRoutes(array $routeRules = [], string $router = 'laravel')
2314
{
24-
$this->router = $router;
25-
$this->routeRules = $routeRules;
26-
}
15+
$usingDingoRouter = strtolower($router) == 'dingo';
2716

28-
public function getRoutes()
29-
{
30-
$usingDingoRouter = strtolower($this->router) == 'dingo';
31-
32-
return $this->getRoutesToBeDocumented($this->routeRules, $usingDingoRouter);
17+
return $this->getRoutesToBeDocumented($routeRules, $usingDingoRouter);
3318
}
3419

35-
protected function getRoutesToBeDocumented(array $routeRules, bool $usingDingoRouter = false)
20+
private function getRoutesToBeDocumented(array $routeRules, bool $usingDingoRouter = false)
3621
{
3722
$allRoutes = $this->getAllRoutes($usingDingoRouter);
23+
3824
$matchedRoutes = [];
3925

4026
foreach ($routeRules as $routeRule) {
@@ -50,11 +36,7 @@ protected function getRoutesToBeDocumented(array $routeRules, bool $usingDingoRo
5036
}
5137

5238
if ($this->shouldIncludeRoute($route, $routeRule, $includes, $usingDingoRouter)) {
53-
$matchedRoutes[] = [
54-
'route' => $route,
55-
'apply' => $routeRule['apply'] ?? [],
56-
];
57-
continue;
39+
$matchedRoutes[] = new Match($route, $routeRule['apply'] ?? []);
5840
}
5941
}
6042
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
namespace Mpociot\ApiDoc\Matching\RouteMatcher;
3+
4+
use Illuminate\Routing\Route;
5+
6+
class Match implements \ArrayAccess
7+
{
8+
/**
9+
* @var Route
10+
*/
11+
protected $route;
12+
13+
/**
14+
* @var array
15+
*/
16+
protected $rules;
17+
18+
/**
19+
* Match constructor.
20+
* @param Route $route
21+
* @param array $applyRules
22+
*/
23+
public function __construct(Route $route, array $applyRules)
24+
{
25+
$this->route = $route;
26+
$this->rules = $applyRules;
27+
}
28+
29+
/**
30+
* @return Route
31+
*/
32+
public function getRoute()
33+
{
34+
return $this->route;
35+
}
36+
37+
/**
38+
* @return array
39+
*/
40+
public function getRules()
41+
{
42+
return $this->rules;
43+
}
44+
45+
/**
46+
* @inheritDoc
47+
*/
48+
public function offsetExists($offset)
49+
{
50+
return is_callable([$this, 'get' . ucfirst($offset)]);
51+
}
52+
53+
/**
54+
* @inheritDoc
55+
*/
56+
public function offsetGet($offset)
57+
{
58+
return call_user_func([$this, 'get' . ucfirst($offset)]);
59+
}
60+
61+
/**
62+
* @inheritDoc
63+
*/
64+
public function offsetSet($offset, $value)
65+
{
66+
return $this->$offset = $value;
67+
}
68+
69+
/**
70+
* @inheritDoc
71+
*/
72+
public function offsetUnset($offset)
73+
{
74+
$this->$offset = null;
75+
}
76+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Mpociot\ApiDoc\Matching;
4+
5+
use Mpociot\ApiDoc\Matching\RouteMatcher\Match;
6+
7+
interface RouteMatcherInterface
8+
{
9+
/**
10+
* Resolve matched routes that should be documented
11+
*
12+
* @param array $routeRules Route rules defined under the "routes" section in config
13+
* @param string $router
14+
* @return Match[]
15+
*/
16+
public function getRoutes(array $routeRules = [], string $router = 'laravel');
17+
}

0 commit comments

Comments
 (0)