1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the jade/jade package. |
5
|
|
|
* |
6
|
|
|
* (c) Slince <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jade\Routing; |
13
|
|
|
|
14
|
|
|
class RouteCollector |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $prefix; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Route[]|RouteCollection |
23
|
|
|
*/ |
24
|
|
|
protected $routes = []; |
25
|
|
|
|
26
|
|
|
public function __construct($prefix = '') |
27
|
|
|
{ |
28
|
|
|
$this->prefix = $prefix; |
29
|
|
|
$this->routes = new RouteCollection(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* 获取路由集合 |
34
|
|
|
* |
35
|
|
|
* @return RouteCollection |
36
|
|
|
*/ |
37
|
|
|
public function getRoutes() |
38
|
|
|
{ |
39
|
|
|
return $this->routes; |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* 创建一条 http get 路由 |
44
|
|
|
* |
45
|
|
|
* @param string $path |
46
|
|
|
* @param string|callable $action |
47
|
|
|
* @return Route |
48
|
|
|
*/ |
49
|
|
|
public function get($path, $action) |
50
|
|
|
{ |
51
|
|
|
return $this->map($path, $action, 'GET'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* 创建一条 http post 路由 |
56
|
|
|
* |
57
|
|
|
* @param string $path |
58
|
|
|
* @param string|callable $action |
59
|
|
|
* @return Route |
60
|
|
|
*/ |
61
|
|
|
public function post($path, $action) |
62
|
|
|
{ |
63
|
|
|
return $this->map($path, $action, 'POST'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* 创建一条 http delete 路由 |
68
|
|
|
* |
69
|
|
|
* @param string $path |
70
|
|
|
* @param string|callable $action |
71
|
|
|
* @return Route |
72
|
|
|
*/ |
73
|
|
|
public function delete($path, $action) |
74
|
|
|
{ |
75
|
|
|
return $this->map($path, $action, 'DELETE'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* 创建一条 http put/patch 路由 |
80
|
|
|
* |
81
|
|
|
* @param string $path |
82
|
|
|
* @param string|callable $action |
83
|
|
|
* @return Route |
84
|
|
|
*/ |
85
|
|
|
public function put($path, $action) |
86
|
|
|
{ |
87
|
|
|
return $this->map($path, $action, ['PUT', 'PATCH']); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* 创建一条 http options 路由 |
92
|
|
|
* |
93
|
|
|
* @param string $path |
94
|
|
|
* @param string|callable $action |
95
|
|
|
* @return Route |
96
|
|
|
*/ |
97
|
|
|
public function options($path, $action) |
98
|
|
|
{ |
99
|
|
|
return $this->map($path, $action, 'OPTIONS'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* 创建一条 http 请求路由 |
104
|
|
|
* |
105
|
|
|
* @param string $path |
106
|
|
|
* @param string|callable $action |
107
|
|
|
* @param array|string $methods |
108
|
|
|
* @return Route |
109
|
|
|
*/ |
110
|
|
|
public function map($path, $action, $methods = []) |
111
|
|
|
{ |
112
|
|
|
$path = $this->prefix . $path; |
113
|
|
|
$methods = array_map('strtoupper', (array)$methods); |
114
|
|
|
$route = new Route(null, $path, $action, $methods); |
115
|
|
|
$this->getRoutes()->add($route); |
116
|
|
|
return $route; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function any($path, $action) |
120
|
|
|
{ |
121
|
|
|
return $this->map($path, $action); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* 创建一条通用前缀的路由组 |
126
|
|
|
* |
127
|
|
|
* @param string $prefix |
128
|
|
|
* @param callable $callback |
129
|
|
|
*/ |
130
|
|
|
public function prefix($prefix, callable $callback) |
131
|
|
|
{ |
132
|
|
|
$collector = new RouteCollector($prefix); |
133
|
|
|
call_user_func($callback, $collector); |
134
|
|
|
$this->routes->merge($collector->getRoutes()); |
135
|
|
|
} |
136
|
|
|
} |