|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Drest package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Lee Davis |
|
9
|
|
|
* @copyright Copyright (c) Lee Davis <@leedavis81> |
|
10
|
|
|
* @link https://github.com/leedavis81/drest/blob/master/LICENSE |
|
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT X License (MIT) |
|
12
|
|
|
*/ |
|
13
|
|
|
namespace Drest; |
|
14
|
|
|
|
|
15
|
|
|
use Drest\Mapping\RouteMetaData; |
|
16
|
|
|
use DrestCommon\Request\Request; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Drest Router |
|
20
|
|
|
* @author Lee |
|
21
|
|
|
*/ |
|
22
|
|
|
class Router |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* A collection of registered route objects |
|
26
|
|
|
* @var array $routes |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $routes = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* An array of Route Base Paths |
|
32
|
|
|
* @var array $routeBasePaths |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $routeBasePaths = []; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Get matched routes |
|
38
|
|
|
* @param Request $request |
|
39
|
|
|
* @param boolean $matchVerb - Whether you want to match the route using the request HTTP verb |
|
40
|
|
|
* @return array $routes sends back an array of routes that have matched |
|
41
|
|
|
*/ |
|
42
|
30 |
|
public function getMatchedRoutes(Request $request, $matchVerb = true) |
|
43
|
|
|
{ |
|
44
|
30 |
|
$matches = []; |
|
45
|
|
|
|
|
46
|
30 |
|
foreach ($this->routes as $route) { |
|
47
|
|
|
/* @var RouteMetaData $route */ |
|
48
|
30 |
|
if (sizeof($this->routeBasePaths) > 0) { |
|
49
|
1 |
|
foreach ($this->routeBasePaths as $basePath) { |
|
50
|
1 |
|
if ($route->matches($request, $matchVerb, $basePath)) { |
|
51
|
1 |
|
$matches[] = $route; |
|
52
|
1 |
|
} |
|
53
|
1 |
|
} |
|
54
|
1 |
|
return $matches; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
29 |
|
if ($route->matches($request, $matchVerb)) { |
|
58
|
26 |
|
$matches[] = $route; |
|
59
|
26 |
|
} |
|
60
|
29 |
|
} |
|
61
|
|
|
|
|
62
|
29 |
|
return $matches; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Register a route definition (pulled from annotations) into the router stack |
|
67
|
|
|
* @param RouteMetaData $route |
|
68
|
|
|
*/ |
|
69
|
34 |
|
public function registerRoute(RouteMetaData $route) |
|
70
|
|
|
{ |
|
71
|
34 |
|
$this->routes[$route->getName()] = $route; |
|
72
|
34 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Register an array of routes |
|
76
|
|
|
* @param RouteMetaData[] $routes |
|
77
|
|
|
*/ |
|
78
|
31 |
|
public function registerRoutes(array $routes) |
|
79
|
|
|
{ |
|
80
|
31 |
|
foreach ($routes as $route) |
|
81
|
|
|
{ |
|
82
|
31 |
|
if (!$route instanceof RouteMetaData) |
|
83
|
31 |
|
{ |
|
84
|
|
|
continue; |
|
85
|
|
|
} |
|
86
|
31 |
|
$this->registerRoute($route); |
|
87
|
31 |
|
} |
|
88
|
31 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* A check to see if the router object has already been populated with a route object by $name |
|
92
|
|
|
* @param string $name |
|
93
|
|
|
* @return bool |
|
94
|
|
|
*/ |
|
95
|
1 |
|
public function hasRoute($name) |
|
96
|
|
|
{ |
|
97
|
1 |
|
return isset($this->routes[$name]); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Set any route base paths |
|
102
|
|
|
* @param array $basePaths |
|
103
|
|
|
*/ |
|
104
|
1 |
|
public function setRouteBasePaths(array $basePaths) |
|
105
|
|
|
{ |
|
106
|
1 |
|
$this->routeBasePaths = $basePaths; |
|
107
|
1 |
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|