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