1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace zqhong\route; |
4
|
|
|
|
5
|
|
|
class RouteCollector |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var array 静态路由数组 |
9
|
|
|
* |
10
|
|
|
* 数据结构: |
11
|
|
|
* ``` |
12
|
|
|
* [ |
13
|
|
|
* 'GET' => [ |
14
|
|
|
* '/route1' => 'handler1', |
15
|
|
|
* '/route2' => 'handler2', |
16
|
|
|
* ], |
17
|
|
|
* 'POST' => [ |
18
|
|
|
* '/route3' => 'handler3', |
19
|
|
|
* '/route4' => 'handler4', |
20
|
|
|
* ] |
21
|
|
|
* ] |
22
|
|
|
* ``` |
23
|
|
|
*/ |
24
|
|
|
protected $staticRoutes; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array 动态路由数组(正则匹配) |
28
|
|
|
* |
29
|
|
|
* 数据结构: |
30
|
|
|
* ``` |
31
|
|
|
* [ |
32
|
|
|
* 'GET' => [ |
33
|
|
|
* '/user/(\d+) => [ |
34
|
|
|
* new Route('/user/(\d+)', 'handler', ['id' => 'id']), |
35
|
|
|
* ] |
36
|
|
|
* ] |
37
|
|
|
* ] |
38
|
|
|
* ``` |
39
|
|
|
*/ |
40
|
|
|
protected $variableRoutes; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var RouteParser |
44
|
|
|
*/ |
45
|
|
|
protected $routeParser; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var RouteGenerator |
49
|
|
|
*/ |
50
|
|
|
protected $routeGenerator; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* RouteCollector constructor. |
54
|
|
|
* @param RouteParser $routeParser |
55
|
|
|
* @param RouteGenerator $routeGenerator |
56
|
|
|
*/ |
57
|
|
|
public function __construct(RouteParser $routeParser, RouteGenerator $routeGenerator) |
58
|
|
|
{ |
59
|
|
|
$this->routeParser = $routeParser; |
60
|
|
|
$this->routeGenerator = $routeGenerator; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* 添加路由规则 |
65
|
|
|
* |
66
|
|
|
* @param string $httpMethod |
67
|
|
|
* @param string $route |
68
|
|
|
* @param string|array|\Closure $handler |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
public function addRoute($httpMethod, $route, $handler) |
72
|
|
|
{ |
73
|
|
|
/** @var Route $routeInstance */ |
74
|
|
|
$routeInstance = $this->routeParser->parse($route, $handler); |
75
|
|
|
|
76
|
|
|
if ($this->isStaticRoute($routeInstance)) { |
77
|
|
|
$this->staticRoutes[$httpMethod][$route] = $handler; |
78
|
|
|
} else { |
79
|
|
|
$this->variableRoutes[$httpMethod][] = $routeInstance; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* 判断所给路由是否静态路由 |
85
|
|
|
* |
86
|
|
|
* @param Route $routeInstance |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
protected function isStaticRoute($routeInstance) |
90
|
|
|
{ |
91
|
|
|
if (empty($routeInstance->params)) { |
92
|
|
|
return true; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
|
|
public function getStaticRoutes() |
102
|
|
|
{ |
103
|
|
|
return $this->staticRoutes; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
public function getVariableRoutes() |
110
|
|
|
{ |
111
|
|
|
return $this->variableRoutes; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $httpMethod |
116
|
|
|
* @return array|bool |
117
|
|
|
*/ |
118
|
|
|
public function getCombinedVarRoutes($httpMethod) |
119
|
|
|
{ |
120
|
|
|
if (empty($this->variableRoutes[$httpMethod])) { |
121
|
|
|
return false; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $this->routeGenerator->combineVarRoutes($this->variableRoutes[$httpMethod]); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|