1 | <?php |
||
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) |
||
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) |
||
82 | |||
83 | /** |
||
84 | * 判断所给路由是否静态路由 |
||
85 | * |
||
86 | * @param Route $routeInstance |
||
87 | * @return bool |
||
88 | */ |
||
89 | protected function isStaticRoute($routeInstance) |
||
97 | |||
98 | /** |
||
99 | * @return array |
||
100 | */ |
||
101 | public function getStaticRoutes() |
||
105 | |||
106 | /** |
||
107 | * @return array |
||
108 | */ |
||
109 | public function getVariableRoutes() |
||
113 | |||
114 | /** |
||
115 | * @param string $httpMethod |
||
116 | * @return array|bool |
||
117 | */ |
||
118 | public function getCombinedVarRoutes($httpMethod) |
||
126 | } |
||
127 |