1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* It's free open-source software released under the MIT License. |
5
|
|
|
* |
6
|
|
|
* @author Anatoly Fenric <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2018, Anatoly Fenric |
8
|
|
|
* @license https://github.com/sunrise-php/http-router/blob/master/LICENSE |
9
|
|
|
* @link https://github.com/sunrise-php/http-router |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sunrise\Http\Router; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Import classes |
16
|
|
|
*/ |
17
|
|
|
use Fig\Http\Message\RequestMethodInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* RouteCollection |
21
|
|
|
*/ |
22
|
|
|
class RouteCollection implements RouteCollectionInterface |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The collection routes |
27
|
|
|
* |
28
|
|
|
* @var RouteInterface[] |
29
|
|
|
*/ |
30
|
|
|
protected $routes = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritDoc} |
34
|
|
|
*/ |
35
|
|
|
public function addRoute(RouteInterface $route) : RouteCollectionInterface |
36
|
|
|
{ |
37
|
|
|
$this->routes[] = $route; |
38
|
|
|
|
39
|
|
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritDoc} |
44
|
|
|
*/ |
45
|
|
|
public function getRoutes() : array |
46
|
|
|
{ |
47
|
|
|
return $this->routes; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritDoc} |
52
|
|
|
*/ |
53
|
|
|
public function route(string $id, string $path, array $methods) : RouteInterface |
54
|
|
|
{ |
55
|
|
|
$route = new Route($id, $path, $methods); |
56
|
|
|
|
57
|
|
|
$this->addRoute($route); |
58
|
|
|
|
59
|
|
|
return $route; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritDoc} |
64
|
|
|
*/ |
65
|
|
|
public function head(string $id, string $path) : RouteInterface |
66
|
|
|
{ |
67
|
|
|
$methods[] = RequestMethodInterface::METHOD_HEAD; |
|
|
|
|
68
|
|
|
|
69
|
|
|
return $this->route($id, $path, $methods); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritDoc} |
74
|
|
|
*/ |
75
|
|
|
public function get(string $id, string $path) : RouteInterface |
76
|
|
|
{ |
77
|
|
|
$methods[] = RequestMethodInterface::METHOD_GET; |
|
|
|
|
78
|
|
|
|
79
|
|
|
return $this->route($id, $path, $methods); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritDoc} |
84
|
|
|
*/ |
85
|
|
|
public function post(string $id, string $path) : RouteInterface |
86
|
|
|
{ |
87
|
|
|
$methods[] = RequestMethodInterface::METHOD_POST; |
|
|
|
|
88
|
|
|
|
89
|
|
|
return $this->route($id, $path, $methods); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritDoc} |
94
|
|
|
*/ |
95
|
|
|
public function put(string $id, string $path) : RouteInterface |
96
|
|
|
{ |
97
|
|
|
$methods[] = RequestMethodInterface::METHOD_PUT; |
|
|
|
|
98
|
|
|
|
99
|
|
|
return $this->route($id, $path, $methods); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritDoc} |
104
|
|
|
*/ |
105
|
|
|
public function patch(string $id, string $path) : RouteInterface |
106
|
|
|
{ |
107
|
|
|
$methods[] = RequestMethodInterface::METHOD_PATCH; |
|
|
|
|
108
|
|
|
|
109
|
|
|
return $this->route($id, $path, $methods); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritDoc} |
114
|
|
|
*/ |
115
|
|
|
public function delete(string $id, string $path) : RouteInterface |
116
|
|
|
{ |
117
|
|
|
$methods[] = RequestMethodInterface::METHOD_DELETE; |
|
|
|
|
118
|
|
|
|
119
|
|
|
return $this->route($id, $path, $methods); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritDoc} |
124
|
|
|
*/ |
125
|
|
|
public function purge(string $id, string $path) : RouteInterface |
126
|
|
|
{ |
127
|
|
|
$methods[] = RequestMethodInterface::METHOD_PURGE; |
|
|
|
|
128
|
|
|
|
129
|
|
|
return $this->route($id, $path, $methods); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritDoc} |
134
|
|
|
*/ |
135
|
|
|
public function safe(string $id, string $path) : RouteInterface |
136
|
|
|
{ |
137
|
|
|
$methods[] = RequestMethodInterface::METHOD_HEAD; |
|
|
|
|
138
|
|
|
$methods[] = RequestMethodInterface::METHOD_GET; |
139
|
|
|
|
140
|
|
|
return $this->route($id, $path, $methods); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* {@inheritDoc} |
145
|
|
|
*/ |
146
|
|
|
public function any(string $id, string $path) : RouteInterface |
147
|
|
|
{ |
148
|
|
|
$methods[] = RequestMethodInterface::METHOD_HEAD; |
|
|
|
|
149
|
|
|
$methods[] = RequestMethodInterface::METHOD_GET; |
150
|
|
|
$methods[] = RequestMethodInterface::METHOD_POST; |
151
|
|
|
$methods[] = RequestMethodInterface::METHOD_PUT; |
152
|
|
|
$methods[] = RequestMethodInterface::METHOD_PATCH; |
153
|
|
|
$methods[] = RequestMethodInterface::METHOD_DELETE; |
154
|
|
|
$methods[] = RequestMethodInterface::METHOD_PURGE; |
155
|
|
|
|
156
|
|
|
return $this->route($id, $path, $methods); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* {@inheritDoc} |
161
|
|
|
*/ |
162
|
|
|
public function group(string $prefix, callable $callback) : void |
163
|
|
|
{ |
164
|
|
|
$collection = new self; |
165
|
|
|
|
166
|
|
|
$callback($collection); |
167
|
|
|
|
168
|
|
|
foreach ($collection->getRoutes() as $route) |
169
|
|
|
{ |
170
|
|
|
$route->addPrefix($prefix); |
171
|
|
|
|
172
|
|
|
$this->addRoute($route); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|