1
|
|
|
<?php |
2
|
|
|
namespace Wandu\Router; |
3
|
|
|
|
4
|
|
|
use IteratorAggregate; |
5
|
|
|
use Wandu\Router\Contracts\Route as RouteInterface; |
6
|
|
|
use Wandu\Router\Contracts\Router as RouterInterface; |
7
|
|
|
|
8
|
|
|
class Router implements IteratorAggregate, RouterInterface |
9
|
|
|
{ |
10
|
|
|
/** @var \Wandu\Router\Route[] */ |
11
|
|
|
protected $routes = []; |
12
|
|
|
|
13
|
|
|
/** @var string */ |
14
|
|
|
protected $host = ''; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
protected $prefix = ''; |
18
|
|
|
|
19
|
|
|
/** @var array */ |
20
|
|
|
protected $middlewares = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $prefix |
24
|
|
|
* @param callable $handler |
25
|
|
|
*/ |
26
|
3 |
|
public function prefix(string $prefix, callable $handler) |
27
|
|
|
{ |
28
|
3 |
|
$beforePrefix = $this->prefix; |
29
|
3 |
|
$this->prefix = "{$beforePrefix}/" . ($prefix ?? ''); |
30
|
|
|
|
31
|
3 |
|
call_user_func($handler, $this); |
32
|
|
|
|
33
|
3 |
|
$this->prefix = $beforePrefix; |
34
|
3 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
public function host(string $host, callable $handler) |
40
|
|
|
{ |
41
|
|
|
$beforeHost = $this->host; |
42
|
|
|
$this->host = $host; |
43
|
|
|
call_user_func($handler, $this); |
44
|
|
|
$this->host = $beforeHost; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param array|string $middlewares |
49
|
|
|
* @param callable $handler |
50
|
|
|
*/ |
51
|
25 |
|
public function middleware($middlewares, callable $handler) |
52
|
|
|
{ |
53
|
25 |
|
$middlewares = array_filter((array) $middlewares); |
54
|
25 |
|
$beforeMiddlewares = $this->middlewares; |
55
|
25 |
|
$this->middlewares = array_merge($beforeMiddlewares, $middlewares); |
56
|
25 |
|
call_user_func($handler, $this); |
57
|
25 |
|
$this->middlewares = $beforeMiddlewares; |
58
|
25 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param array $attributes |
62
|
|
|
* @param callable $handler |
63
|
|
|
*/ |
64
|
|
|
public function group(array $attributes, callable $handler) |
65
|
|
|
{ |
66
|
|
|
$this->prefix($attributes['prefix'] ?? '', function () use ($attributes, $handler) { |
67
|
2 |
|
$this->middleware($attributes['middleware'] ?? [], function () use ($attributes, $handler) { |
68
|
2 |
|
call_user_func($handler, $this); |
69
|
2 |
|
}); |
70
|
2 |
|
}); |
71
|
2 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param callable $handler |
75
|
|
|
*/ |
76
|
|
|
public function append(callable $handler) |
77
|
|
|
{ |
78
|
|
|
call_user_func($handler, $this); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function resource($className, $except = [], $only = []) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$this->get('', $className, 'index'); |
84
|
|
|
$this->get('create', $className, 'create'); |
85
|
|
|
$this->post('', $className, 'store'); |
86
|
|
|
$this->get('{id}', $className, 'show'); |
87
|
|
|
$this->put('{id}', $className, 'update'); |
88
|
|
|
$this->delete('{id}', $className, 'destroy'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
7 |
|
public function get(string $path, string $className, string $methodName = 'index'): RouteInterface |
95
|
|
|
{ |
96
|
7 |
|
return $this->createRoute(['GET', 'HEAD'], $path, $className, $methodName); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
2 |
|
public function post(string $path, string $className, string $methodName = 'index'): RouteInterface |
103
|
|
|
{ |
104
|
2 |
|
return $this->createRoute(['POST'], $path, $className, $methodName); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
1 |
|
public function put(string $path, string $className, string $methodName = 'index'): RouteInterface |
111
|
|
|
{ |
112
|
1 |
|
return $this->createRoute(['PUT'], $path, $className, $methodName); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
1 |
|
public function delete(string $path, string $className, string $methodName = 'index'): RouteInterface |
119
|
|
|
{ |
120
|
1 |
|
return $this->createRoute(['DELETE'], $path, $className, $methodName); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
1 |
|
public function options(string $path, string $className, string $methodName = 'index'): RouteInterface |
127
|
|
|
{ |
128
|
1 |
|
return $this->createRoute(['OPTIONS'], $path, $className, $methodName); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
1 |
|
public function patch(string $path, string $className, string $methodName = 'index'): RouteInterface |
135
|
|
|
{ |
136
|
1 |
|
return $this->createRoute(['PATCH'], $path, $className, $methodName); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritdoc} |
141
|
|
|
*/ |
142
|
1 |
|
public function any(string $path, string $className, string $methodName = 'index'): RouteInterface |
143
|
|
|
{ |
144
|
1 |
|
return $this->createRoute([ |
145
|
1 |
|
'GET', |
146
|
|
|
'HEAD', |
147
|
|
|
'POST', |
148
|
|
|
'PUT', |
149
|
|
|
'DELETE', |
150
|
|
|
'OPTIONS', |
151
|
|
|
'PATCH' |
152
|
1 |
|
], $path, $className, $methodName); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritdoc} |
157
|
|
|
*/ |
158
|
26 |
|
public function createRoute(array $methods, string $path, string $className, string $methodName = 'index'): RouteInterface |
159
|
|
|
{ |
160
|
26 |
|
$path = trim("{$this->prefix}/{$path}", '/'); |
161
|
26 |
|
while(strpos($path, '//') !== false) { |
162
|
2 |
|
$path = str_replace('//', '/', $path); |
163
|
|
|
} |
164
|
26 |
|
$path = '/' . $path; |
165
|
26 |
|
$route = new Route($className, $methodName, $this->middlewares); |
166
|
26 |
|
$this->routes[] = [$methods, $path, $route, $this->host]; |
167
|
26 |
|
return $route; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* {@inheritdoc} |
172
|
|
|
*/ |
173
|
26 |
|
public function getIterator() |
174
|
|
|
{ |
175
|
26 |
|
foreach ($this->routes as $route) { |
176
|
26 |
|
yield $route; |
177
|
|
|
} |
178
|
26 |
|
} |
179
|
|
|
} |
180
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.