1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package WPEmerge |
4
|
|
|
* @author Atanas Angelov <[email protected]> |
5
|
|
|
* @copyright 2017-2019 Atanas Angelov |
6
|
|
|
* @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0 |
7
|
|
|
* @link https://wpemerge.com/ |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace WPEmerge\Routing; |
11
|
|
|
|
12
|
|
|
use Closure; |
13
|
|
|
use WPEmerge\Helpers\HasAttributesTrait; |
14
|
|
|
use WPEmerge\Routing\Conditions\ConditionInterface; |
15
|
|
|
use WPEmerge\View\ViewService; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Provide a fluent interface for registering routes with the router. |
19
|
|
|
*/ |
20
|
|
|
class RouteBlueprint { |
21
|
|
|
use HasAttributesTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Router. |
25
|
|
|
* |
26
|
|
|
* @var Router |
27
|
|
|
*/ |
28
|
|
|
protected $router = null; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* View service. |
32
|
|
|
* |
33
|
|
|
* @var ViewService |
34
|
|
|
*/ |
35
|
|
|
protected $view_service = null; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Constructor. |
39
|
|
|
* |
40
|
|
|
* @codeCoverageIgnore |
41
|
|
|
* @param Router $router |
42
|
|
|
* @param ViewService $view_service |
43
|
|
|
*/ |
44
|
|
|
public function __construct( Router $router, ViewService $view_service ) { |
45
|
|
|
$this->router = $router; |
46
|
|
|
$this->view_service = $view_service; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Match requests using one of the specified methods. |
51
|
|
|
* |
52
|
|
|
* @param string[] $methods |
53
|
|
|
* @return static $this |
54
|
|
|
*/ |
55
|
1 |
|
public function methods( $methods ) { |
56
|
1 |
|
$methods = $this->router->mergeMethodsAttribute( |
57
|
1 |
|
(array) $this->getAttribute( 'methods', [] ), |
58
|
1 |
|
(array) $methods |
59
|
|
|
); |
60
|
|
|
|
61
|
1 |
|
return $this->attribute( 'methods', $methods ); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Set the condition attribute to a URL. |
66
|
|
|
* |
67
|
|
|
* @param string $url |
68
|
|
|
* @param array<string, string> $where |
69
|
|
|
* @return static $this |
70
|
|
|
*/ |
71
|
1 |
|
public function url( $url, $where = [] ) { |
72
|
1 |
|
return $this->where( 'url', $url, $where ); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Set the condition attribute. |
77
|
|
|
* |
78
|
|
|
* @param string|array|ConditionInterface $condition |
79
|
|
|
* @param mixed ,...$arguments |
80
|
|
|
* @return static $this |
81
|
|
|
*/ |
82
|
3 |
|
public function where( $condition ) { |
83
|
3 |
|
if ( ! $condition instanceof ConditionInterface ) { |
84
|
3 |
|
$condition = func_get_args(); |
85
|
|
|
} |
86
|
|
|
|
87
|
3 |
|
$condition = $this->router->mergeConditionAttribute( |
88
|
3 |
|
$this->getAttribute( 'condition', null ), |
89
|
|
|
$condition |
90
|
|
|
); |
91
|
|
|
|
92
|
3 |
|
return $this->attribute( 'condition', $condition ); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Set the middleware attribute. |
97
|
|
|
* |
98
|
|
|
* @param string|string[] $middleware |
99
|
|
|
* @return static $this |
100
|
|
|
*/ |
101
|
1 |
|
public function middleware( $middleware ) { |
102
|
1 |
|
$middleware = $this->router->mergeMiddlewareAttribute( |
103
|
1 |
|
(array) $this->getAttribute( 'middleware', [] ), |
104
|
1 |
|
(array) $middleware |
105
|
|
|
); |
106
|
|
|
|
107
|
1 |
|
return $this->attribute( 'middleware', $middleware ); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Set the namespace attribute. |
112
|
|
|
* This should be renamed to namespace for consistency once minimum PHP |
113
|
|
|
* version is increased to 7+. |
114
|
|
|
* |
115
|
|
|
* @param string $namespace |
116
|
|
|
* @return static $this |
117
|
|
|
*/ |
118
|
1 |
|
public function setNamespace( $namespace ) { |
119
|
1 |
|
$namespace = $this->router->mergeNamespaceAttribute( |
120
|
1 |
|
$this->getAttribute( 'namespace', '' ), |
121
|
|
|
$namespace |
122
|
|
|
); |
123
|
|
|
|
124
|
1 |
|
return $this->attribute( 'namespace', $namespace ); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Set the query attribute. |
129
|
|
|
* |
130
|
|
|
* @param callable $query |
131
|
|
|
* @return static $this |
132
|
|
|
*/ |
133
|
1 |
|
public function query( $query ) { |
134
|
1 |
|
$query = $this->router->mergeQueryAttribute( |
135
|
1 |
|
$this->getAttribute( 'query', null ), |
136
|
|
|
$query |
137
|
|
|
); |
138
|
|
|
|
139
|
1 |
|
return $this->attribute( 'query', $query ); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Set the name attribute. |
144
|
|
|
* |
145
|
|
|
* @param string $name |
146
|
|
|
* @return static $this |
147
|
|
|
*/ |
148
|
1 |
|
public function name( $name ) { |
149
|
1 |
|
return $this->attribute( 'name', $name ); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Create a route group. |
154
|
|
|
* |
155
|
|
|
* @param Closure|string $routes Closure or path to file. |
156
|
|
|
* @return void |
157
|
|
|
*/ |
158
|
1 |
|
public function group( $routes ) { |
159
|
1 |
|
$this->router->group( $this->getAttributes(), $routes ); |
160
|
1 |
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Create a route. |
164
|
|
|
* |
165
|
|
|
* @param string|Closure $handler |
166
|
|
|
* @return void |
167
|
|
|
*/ |
168
|
4 |
|
public function handle( $handler = '' ) { |
169
|
4 |
|
if ( ! empty( $handler ) ) { |
170
|
1 |
|
$this->attribute( 'handler', $handler ); |
171
|
|
|
} |
172
|
|
|
|
173
|
4 |
|
$route = $this->router->route( $this->getAttributes() ); |
174
|
|
|
|
175
|
4 |
|
$trace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 1 ); |
176
|
|
|
|
177
|
4 |
|
if ( ! empty( $trace ) && ! empty( $trace[0]['file'] ) ) { |
178
|
4 |
|
$route->attribute( '__definition', $trace[0]['file'] . ':' . $trace[0]['line'] ); |
179
|
|
|
} |
180
|
|
|
|
181
|
4 |
|
$this->router->addRoute( $route ); |
182
|
4 |
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Handle a request by directly rendering a view. |
186
|
|
|
* |
187
|
|
|
* @param string|string[] $views |
188
|
|
|
* @return void |
189
|
|
|
*/ |
190
|
1 |
|
public function view( $views ) { |
191
|
|
|
$this->handle( function () use ( $views ) { |
192
|
1 |
|
return $this->view_service->make( $views ); |
193
|
1 |
|
} ); |
194
|
1 |
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Match ALL requests. |
198
|
|
|
* |
199
|
|
|
* @param string|Closure $handler |
200
|
|
|
* @return void |
201
|
|
|
*/ |
202
|
1 |
|
public function all( $handler = '' ) { |
203
|
1 |
|
$this->any()->url( '*' )->handle( $handler ); |
204
|
1 |
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Match requests with a method of GET or HEAD. |
208
|
|
|
* |
209
|
|
|
* @return static $this |
210
|
|
|
*/ |
211
|
1 |
|
public function get() { |
212
|
1 |
|
return $this->methods( ['GET', 'HEAD'] ); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Match requests with a method of POST. |
217
|
|
|
* |
218
|
|
|
* @return static $this |
219
|
|
|
*/ |
220
|
1 |
|
public function post() { |
221
|
1 |
|
return $this->methods( ['POST'] ); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Match requests with a method of PUT. |
226
|
|
|
* |
227
|
|
|
* @return static $this |
228
|
|
|
*/ |
229
|
1 |
|
public function put() { |
230
|
1 |
|
return $this->methods( ['PUT'] ); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Match requests with a method of PATCH. |
235
|
|
|
* |
236
|
|
|
* @return static $this |
237
|
|
|
*/ |
238
|
1 |
|
public function patch() { |
239
|
1 |
|
return $this->methods( ['PATCH'] ); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Match requests with a method of DELETE. |
244
|
|
|
* |
245
|
|
|
* @return static $this |
246
|
|
|
*/ |
247
|
1 |
|
public function delete() { |
248
|
1 |
|
return $this->methods( ['DELETE'] ); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Match requests with a method of OPTIONS. |
253
|
|
|
* |
254
|
|
|
* @return static $this |
255
|
|
|
*/ |
256
|
1 |
|
public function options() { |
257
|
1 |
|
return $this->methods( ['OPTIONS'] ); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Match requests with any method. |
262
|
|
|
* |
263
|
|
|
* @return static $this |
264
|
|
|
*/ |
265
|
1 |
|
public function any() { |
266
|
1 |
|
return $this->methods( ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'] ); |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|