|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace HttpSoft\Basis; |
|
6
|
|
|
|
|
7
|
|
|
use HttpSoft\Emitter\EmitterInterface; |
|
8
|
|
|
use HttpSoft\Router\Route; |
|
9
|
|
|
use HttpSoft\Router\RouteCollector; |
|
10
|
|
|
use HttpSoft\Runner\MiddlewarePipelineInterface; |
|
11
|
|
|
use HttpSoft\Runner\MiddlewareResolverInterface; |
|
12
|
|
|
use HttpSoft\Runner\ServerRequestRunner; |
|
13
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
14
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
15
|
|
|
|
|
16
|
|
|
final class Application |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var RouteCollector |
|
20
|
|
|
*/ |
|
21
|
|
|
private RouteCollector $router; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var EmitterInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private EmitterInterface $emitter; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var MiddlewarePipelineInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private MiddlewarePipelineInterface $pipeline; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var MiddlewareResolverInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
private MiddlewareResolverInterface $resolver; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var RequestHandlerInterface|null |
|
40
|
|
|
*/ |
|
41
|
|
|
private ?RequestHandlerInterface $default; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param RouteCollector $router |
|
45
|
|
|
* @param EmitterInterface $emitter |
|
46
|
|
|
* @param MiddlewarePipelineInterface $pipeline |
|
47
|
|
|
* @param MiddlewareResolverInterface $resolver |
|
48
|
|
|
* @param RequestHandlerInterface|null $default |
|
49
|
|
|
*/ |
|
50
|
93 |
|
public function __construct( |
|
51
|
|
|
RouteCollector $router, |
|
52
|
|
|
EmitterInterface $emitter, |
|
53
|
|
|
MiddlewarePipelineInterface $pipeline, |
|
54
|
|
|
MiddlewareResolverInterface $resolver, |
|
55
|
|
|
?RequestHandlerInterface $default = null |
|
56
|
|
|
) { |
|
57
|
93 |
|
$this->router = $router; |
|
58
|
93 |
|
$this->emitter = $emitter; |
|
59
|
93 |
|
$this->pipeline = $pipeline; |
|
60
|
93 |
|
$this->resolver = $resolver; |
|
61
|
93 |
|
$this->default = $default; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Run the application. |
|
66
|
|
|
* |
|
67
|
|
|
* Proxies to the `ServerRequestRunner::run()` method. |
|
68
|
|
|
* |
|
69
|
|
|
* @param ServerRequestInterface $request |
|
70
|
|
|
* @param RequestHandlerInterface|null $defaultHandler |
|
71
|
|
|
*/ |
|
72
|
75 |
|
public function run(ServerRequestInterface $request, ?RequestHandlerInterface $defaultHandler = null): void |
|
73
|
|
|
{ |
|
74
|
75 |
|
(new ServerRequestRunner($this->pipeline, $this->emitter))->run($request, $defaultHandler ?? $this->default); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Adds a middleware to the pipeline. |
|
79
|
|
|
* |
|
80
|
|
|
* Wrapper over the `MiddlewarePipelineInterface::pipe()` method. |
|
81
|
|
|
* |
|
82
|
|
|
* @param mixed $middleware any valid value for converting it to `Psr\Http\Server\MiddlewareInterface` instance. |
|
83
|
|
|
* @param string|null $path path prefix from the root to which the middleware is attached. |
|
84
|
|
|
*/ |
|
85
|
75 |
|
public function pipe($middleware, ?string $path = null): void |
|
86
|
|
|
{ |
|
87
|
75 |
|
$this->pipeline->pipe($this->resolver->resolve($middleware), $path); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Creates a route group with a common prefix. |
|
92
|
|
|
* |
|
93
|
|
|
* Proxies to the `RouteCollector::group()` method. |
|
94
|
|
|
* |
|
95
|
|
|
* The callback can take a `HttpSoft\Router\RouteCollector` instance as a parameter. |
|
96
|
|
|
* All routes created in the passed callback will have the given group prefix prepended |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $prefix common path prefix for the route group. |
|
99
|
|
|
* @param callable $callback callback that will add routes with a common path prefix. |
|
100
|
|
|
*/ |
|
101
|
1 |
|
public function group(string $prefix, callable $callback): void |
|
102
|
|
|
{ |
|
103
|
1 |
|
$this->router->group($prefix, $callback); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Adds a route and returns it. |
|
108
|
|
|
* |
|
109
|
|
|
* Proxies to the `RouteCollector::add()` method. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $name route name. |
|
112
|
|
|
* @param string $pattern path pattern with parameters. |
|
113
|
|
|
* @param mixed $handler action, controller, callable, closure, etc. |
|
114
|
|
|
* @param array $methods allowed request methods of the route. |
|
115
|
|
|
* @return Route |
|
116
|
|
|
*/ |
|
117
|
1 |
|
public function add(string $name, string $pattern, $handler, array $methods = []): Route |
|
118
|
|
|
{ |
|
119
|
1 |
|
return $this->router->add($name, $pattern, $handler, $methods); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Adds a generic route for any request methods and returns it. |
|
124
|
|
|
* |
|
125
|
|
|
* Proxies to the `RouteCollector::any()` method. |
|
126
|
|
|
* |
|
127
|
|
|
* @param string $name route name. |
|
128
|
|
|
* @param string $pattern path pattern with parameters. |
|
129
|
|
|
* @param mixed $handler action, controller, callable, closure, etc. |
|
130
|
|
|
* @return Route |
|
131
|
|
|
*/ |
|
132
|
1 |
|
public function any(string $name, string $pattern, $handler): Route |
|
133
|
|
|
{ |
|
134
|
1 |
|
return $this->router->any($name, $pattern, $handler); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Adds a GET route and returns it. |
|
139
|
|
|
* |
|
140
|
|
|
* Proxies to the `RouteCollector::get()` method. |
|
141
|
|
|
* |
|
142
|
|
|
* @param string $name route name. |
|
143
|
|
|
* @param string $pattern path pattern with parameters. |
|
144
|
|
|
* @param mixed $handler action, controller, callable, closure, etc. |
|
145
|
|
|
* @return Route |
|
146
|
|
|
*/ |
|
147
|
68 |
|
public function get(string $name, string $pattern, $handler): Route |
|
148
|
|
|
{ |
|
149
|
68 |
|
return $this->router->get($name, $pattern, $handler); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Adds a POST route and returns it. |
|
154
|
|
|
* |
|
155
|
|
|
* Proxies to the `RouteCollector::post()` method. |
|
156
|
|
|
* |
|
157
|
|
|
* @param string $name route name. |
|
158
|
|
|
* @param string $pattern path pattern with parameters. |
|
159
|
|
|
* @param mixed $handler action, controller, callable, closure, etc. |
|
160
|
|
|
* @return Route |
|
161
|
|
|
*/ |
|
162
|
2 |
|
public function post(string $name, string $pattern, $handler): Route |
|
163
|
|
|
{ |
|
164
|
2 |
|
return $this->router->post($name, $pattern, $handler); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Adds a PUT route and returns it. |
|
169
|
|
|
* |
|
170
|
|
|
* Proxies to the `RouteCollector::put()` method. |
|
171
|
|
|
* |
|
172
|
|
|
* @param string $name route name. |
|
173
|
|
|
* @param string $pattern path pattern with parameters. |
|
174
|
|
|
* @param mixed $handler action, controller, callable, closure, etc. |
|
175
|
|
|
* @return Route |
|
176
|
|
|
*/ |
|
177
|
2 |
|
public function put(string $name, string $pattern, $handler): Route |
|
178
|
|
|
{ |
|
179
|
2 |
|
return $this->router->put($name, $pattern, $handler); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Adds a PATCH route and returns it. |
|
184
|
|
|
* |
|
185
|
|
|
* Proxies to the `RouteCollector::patch()` method. |
|
186
|
|
|
* |
|
187
|
|
|
* @param string $name route name. |
|
188
|
|
|
* @param string $pattern path pattern with parameters. |
|
189
|
|
|
* @param mixed $handler action, controller, callable, closure, etc. |
|
190
|
|
|
* @return Route |
|
191
|
|
|
*/ |
|
192
|
2 |
|
public function patch(string $name, string $pattern, $handler): Route |
|
193
|
|
|
{ |
|
194
|
2 |
|
return $this->router->patch($name, $pattern, $handler); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Adds a DELETE route and returns it. |
|
199
|
|
|
* |
|
200
|
|
|
* Proxies to the `RouteCollector::delete()` method. |
|
201
|
|
|
* |
|
202
|
|
|
* @param string $name route name. |
|
203
|
|
|
* @param string $pattern path pattern with parameters. |
|
204
|
|
|
* @param mixed $handler action, controller, callable, closure, etc. |
|
205
|
|
|
* @return Route |
|
206
|
|
|
*/ |
|
207
|
2 |
|
public function delete(string $name, string $pattern, $handler): Route |
|
208
|
|
|
{ |
|
209
|
2 |
|
return $this->router->delete($name, $pattern, $handler); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Adds a HEAD route and returns it. |
|
214
|
|
|
* |
|
215
|
|
|
* Proxies to the `RouteCollector::head()` method. |
|
216
|
|
|
* |
|
217
|
|
|
* @param string $name route name. |
|
218
|
|
|
* @param string $pattern path pattern with parameters. |
|
219
|
|
|
* @param mixed $handler action, controller, callable, closure, etc. |
|
220
|
|
|
* @return Route |
|
221
|
|
|
*/ |
|
222
|
2 |
|
public function head(string $name, string $pattern, $handler): Route |
|
223
|
|
|
{ |
|
224
|
2 |
|
return $this->router->head($name, $pattern, $handler); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Adds a OPTIONS route and returns it. |
|
229
|
|
|
* |
|
230
|
|
|
* Proxies to the `RouteCollector::options()` method. |
|
231
|
|
|
* |
|
232
|
|
|
* @param string $name route name. |
|
233
|
|
|
* @param string $pattern path pattern with parameters. |
|
234
|
|
|
* @param mixed $handler action, controller, callable, closure, etc. |
|
235
|
|
|
* @return Route |
|
236
|
|
|
*/ |
|
237
|
2 |
|
public function options(string $name, string $pattern, $handler): Route |
|
238
|
|
|
{ |
|
239
|
2 |
|
return $this->router->options($name, $pattern, $handler); |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|