1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of DivineNii opensource projects. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.4 and above required |
9
|
|
|
* |
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
11
|
|
|
* @copyright 2019 DivineNii (https://divinenii.com/) |
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace Rade; |
19
|
|
|
|
20
|
|
|
use Biurad\Http\{Interfaces\Psr17Interface, Request, Response, Response\HtmlResponse}; |
21
|
|
|
use Biurad\Http\Factory\Psr17Factory; |
22
|
|
|
use Flight\Routing\{Exceptions\RouteNotFoundException, Route, RouteCollection, Router}; |
23
|
|
|
use Fig\Http\Message\RequestMethodInterface; |
24
|
|
|
use Flight\Routing\Generator\GeneratedUri; |
25
|
|
|
use Flight\Routing\Interfaces\RouteMatcherInterface; |
26
|
|
|
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface}; |
27
|
|
|
use Psr\Http\Server\{MiddlewareInterface, RequestHandlerInterface}; |
28
|
|
|
use Laminas\Stratigility\Middleware\{CallableMiddlewareDecorator, RequestHandlerMiddleware}; |
29
|
|
|
use Laminas\{HttpHandlerRunner\Emitter\SapiStreamEmitter, Stratigility\Utils}; |
|
|
|
|
30
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
31
|
|
|
use Rade\DI\Definitions\{Reference, Statement}; |
32
|
|
|
use Symfony\Component\Console\Application as ConsoleApplication; |
33
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The Rade framework core class. |
37
|
|
|
* |
38
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
39
|
|
|
*/ |
40
|
|
|
class Application extends DI\Container implements RouterInterface, KernelInterface |
41
|
|
|
{ |
42
|
|
|
use Traits\HelperTrait; |
43
|
|
|
|
44
|
|
|
public const VERSION = '2.0.0-DEV'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Instantiate a new Application. |
48
|
|
|
*/ |
49
|
14 |
|
public function __construct(Psr17Interface $psr17Factory = null, EventDispatcherInterface $dispatcher = null, bool $debug = false) |
50
|
|
|
{ |
51
|
14 |
|
parent::__construct(); |
52
|
|
|
|
53
|
14 |
|
if (empty($this->methodsMap)) { |
54
|
14 |
|
$this->definitions['http.router'] = Router::withCollection(); |
55
|
14 |
|
$this->definitions['request_stack'] = new RequestStack(); |
56
|
14 |
|
$this->definitions['psr17.factory'] = $psr17Factory = ($psr17Factory ?? new Psr17Factory()); |
57
|
14 |
|
$this->definitions['events.dispatcher'] = $dispatcher = ($dispatcher ?? new Handler\EventHandler()); |
58
|
|
|
|
59
|
14 |
|
$this->types( |
60
|
|
|
[ |
61
|
14 |
|
'http.router' => [Router::class, RouteMatcherInterface::class], |
62
|
|
|
'request_stack' => [RequestStack::class], |
63
|
14 |
|
'psr17.factory' => DI\Resolver::autowireService($psr17Factory), |
64
|
14 |
|
'events.dispatcher' => DI\Resolver::autowireService($dispatcher), |
65
|
|
|
] |
66
|
|
|
); |
67
|
|
|
|
68
|
14 |
|
unset($psr17Factory, $dispatcher); |
69
|
|
|
} |
70
|
|
|
|
71
|
14 |
|
if (!isset($this->parameters['debug'])) { |
72
|
14 |
|
$this->parameters['debug'] = $debug; |
73
|
|
|
} |
74
|
14 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* If true, exception will be thrown on resolvable services with are not typed. |
78
|
|
|
*/ |
79
|
|
|
public function strictAutowiring(bool $boolean = true): void |
80
|
|
|
{ |
81
|
|
|
$this->resolver->setStrictAutowiring($boolean); |
82
|
|
|
} |
83
|
|
|
|
84
|
4 |
|
public function getDispatcher(): EventDispatcherInterface |
85
|
|
|
{ |
86
|
4 |
|
return $this->services['events.dispatcher'] ?? $this->get('events.dispatcher'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
* |
92
|
|
|
* @param MiddlewareInterface|RequestHandlerInterface|Reference|Statement|callable ...$middlewares |
93
|
|
|
*/ |
94
|
|
|
public function pipe(object ...$middlewares): void |
95
|
|
|
{ |
96
|
|
|
$this->get('http.router')->pipe(...$this->resolveMiddlewares($middlewares)); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
* |
102
|
|
|
* @param MiddlewareInterface|RequestHandlerInterface|Reference|Statement|callable ...$middlewares |
103
|
|
|
*/ |
104
|
|
|
public function pipes(string $named, object ...$middlewares): void |
105
|
|
|
{ |
106
|
|
|
$this->get('http.router')->pipes($named, ...$this->resolveMiddlewares($middlewares)); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
|
|
public function generateUri(string $routeName, array $parameters = []): GeneratedUri |
113
|
|
|
{ |
114
|
|
|
return $this->get('http.router')->generateUri($routeName, $parameters); |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
7 |
|
public function match(string $pattern, array $methods = Route::DEFAULT_METHODS, $to = null): Route |
121
|
|
|
{ |
122
|
7 |
|
return ($this->services['http.router'] ?? $this->get('http.router'))->getCollection()->addRoute($pattern, $methods, $to)->getRoute(); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
1 |
|
public function post(string $pattern, $to = null): Route |
129
|
|
|
{ |
130
|
1 |
|
return $this->match($pattern, [RequestMethodInterface::METHOD_POST], $to); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
1 |
|
public function put(string $pattern, $to = null): Route |
137
|
|
|
{ |
138
|
1 |
|
return $this->match($pattern, [RequestMethodInterface::METHOD_PUT], $to); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
1 |
|
public function delete(string $pattern, $to = null): Route |
145
|
|
|
{ |
146
|
1 |
|
return $this->match($pattern, [RequestMethodInterface::METHOD_DELETE], $to); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritdoc} |
151
|
|
|
*/ |
152
|
|
|
public function options(string $pattern, $to = null): Route |
153
|
|
|
{ |
154
|
|
|
return $this->match($pattern, [RequestMethodInterface::METHOD_OPTIONS], $to); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* {@inheritdoc} |
159
|
|
|
*/ |
160
|
1 |
|
public function patch(string $pattern, $to = null): Route |
161
|
|
|
{ |
162
|
1 |
|
return $this->match($pattern, [RequestMethodInterface::METHOD_PATCH], $to); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* {@inheritdoc} |
167
|
|
|
*/ |
168
|
1 |
|
public function group(string $prefix, $collection = null): RouteCollection |
169
|
|
|
{ |
170
|
1 |
|
return $this->get('http.router')->getCollection()->group($prefix, $collection); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Handles the request and delivers the response. |
175
|
|
|
* |
176
|
|
|
* @param ServerRequestInterface|null $request Request to process |
177
|
|
|
* |
178
|
|
|
* @throws \Throwable |
179
|
|
|
* |
180
|
|
|
* @return int|bool |
181
|
|
|
*/ |
182
|
|
|
public function run(ServerRequestInterface $request = null, bool $catch = true) |
183
|
|
|
{ |
184
|
|
|
if ($this->isRunningInConsole()) { |
185
|
|
|
$this->get(ConsoleApplication::class)->run(); |
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
if (null === $request) { |
189
|
|
|
$request = $this->get('psr17.factory')->fromGlobalRequest(); |
|
|
|
|
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
$response = $this->handle($request, $catch); |
193
|
|
|
|
194
|
|
|
if ($response instanceof Response) { |
195
|
|
|
$response->getResponse()->send(); |
196
|
|
|
|
197
|
|
|
return true; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
if (class_exists(SapiStreamEmitter::class)) { |
201
|
|
|
return (new SapiStreamEmitter())->emit($response); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
throw new \RuntimeException(\sprintf('Unable to emit response onto the browser. Try running "composer require laminas/laminas-httphandlerrunner".')); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Handles a request to convert it to a response. |
209
|
|
|
* |
210
|
|
|
* Exceptions are not caught. |
211
|
|
|
* |
212
|
|
|
* @param bool $catch Whether to catch exceptions or not |
213
|
|
|
*/ |
214
|
4 |
|
public function handle(ServerRequestInterface $request, bool $catch = true): ResponseInterface |
215
|
|
|
{ |
216
|
4 |
|
if (!$this->has(RequestHandlerInterface::class)) { |
217
|
4 |
|
$this->definitions[RequestHandlerInterface::class] = new Handler\RouteHandler($this); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
try { |
221
|
4 |
|
$response = $this->get('http.router')->process($request, $this->get(RequestHandlerInterface::class)); |
|
|
|
|
222
|
|
|
|
223
|
4 |
|
if ($request instanceof Request) { |
224
|
4 |
|
$request = $request->withRequest($this->get('request_stack')->getMainRequest()); |
|
|
|
|
225
|
|
|
} |
226
|
|
|
|
227
|
4 |
|
$this->getDispatcher()->dispatch($event = new Event\ResponseEvent($this, $request, $response)); |
|
|
|
|
228
|
|
|
} catch (\Throwable $e) { |
229
|
|
|
if (!$catch || $this->isRunningInConsole()) { |
230
|
|
|
throw $e; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return $this->handleThrowable($e, $request); |
|
|
|
|
234
|
4 |
|
} finally { |
235
|
4 |
|
$this->getDispatcher()->dispatch(new Event\TerminateEvent($this, $request)); |
|
|
|
|
236
|
|
|
} |
237
|
|
|
|
238
|
4 |
|
return $event->getResponse(); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Handle RouteNotFoundException for Flight Routing. |
243
|
|
|
* |
244
|
|
|
* @return RouteNotFoundException|ResponseInterface |
245
|
|
|
*/ |
246
|
|
|
protected function handleRouterException(RouteNotFoundException $e, ServerRequestInterface $request) |
247
|
|
|
{ |
248
|
|
|
if (empty($pathInfo = $request->getServerParams()['PATH_INFO'] ?? '')) { |
249
|
|
|
$pathInfo = $request->getUri()->getPath(); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
if ('/' === $pathInfo) { |
253
|
|
|
return $this->createWelcomeResponse(); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
$message = $e->getMessage(); |
257
|
|
|
|
258
|
|
|
if ('' !== $referer = $request->getHeaderLine('referer')) { |
259
|
|
|
$message .= \sprintf(' (from "%s")', $referer); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
return new RouteNotFoundException($message, 404); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Handles a throwable by trying to convert it to a Response. |
267
|
|
|
* |
268
|
|
|
* @throws \Throwable |
269
|
|
|
*/ |
270
|
|
|
protected function handleThrowable(\Throwable $e, ServerRequestInterface $request): ResponseInterface |
271
|
|
|
{ |
272
|
|
|
$this->getDispatcher()->dispatch($event = new Event\ExceptionEvent($this, $request, $e)); |
273
|
|
|
|
274
|
|
|
// a listener might have replaced the exception |
275
|
|
|
$e = $event->getThrowable(); |
276
|
|
|
|
277
|
|
|
if (null === $response = $event->getResponse()) { |
278
|
|
|
if ($e instanceof RouteNotFoundException) { |
279
|
|
|
$e = $this->handleRouterException($e, $request); |
280
|
|
|
|
281
|
|
|
if ($e instanceof ResponseInterface) { |
282
|
|
|
return $e; |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
throw $e; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
// ensure that we actually have an error response and keep the HTTP status code and headers |
290
|
|
|
if (!$event->isAllowingCustomResponseCode()) { |
291
|
|
|
$response = $response->withStatus(Utils::getStatusCode($e, $response)); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
return $response; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* The default welcome page for application. |
299
|
|
|
*/ |
300
|
|
|
protected function createWelcomeResponse(): ResponseInterface |
301
|
|
|
{ |
302
|
|
|
$debug = $this->parameters['debug']; |
303
|
|
|
$version = self::VERSION; |
304
|
|
|
$docVersion = $version[0] . '.x.x'; |
305
|
|
|
|
306
|
|
|
\ob_start(); |
307
|
|
|
|
308
|
|
|
include __DIR__ . '/Resources/welcome.phtml'; |
309
|
|
|
|
310
|
|
|
return new HtmlResponse((string) \ob_get_clean(), 404); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Resolve Middlewares. |
315
|
|
|
* |
316
|
|
|
* @param array<int,MiddlewareInterface|RequestHandlerInterface|Reference|Statement|callable> $middlewares |
317
|
|
|
* |
318
|
|
|
* @return array<int,MiddlewareInterface> |
319
|
|
|
*/ |
320
|
|
|
protected function resolveMiddlewares(array $middlewares): array |
321
|
|
|
{ |
322
|
|
|
foreach ($middlewares as $offset => $middleware) { |
323
|
|
|
if ($middleware instanceof RequestHandlerInterface) { |
324
|
|
|
$middlewares[$offset] = new RequestHandlerMiddleware($middleware); |
325
|
|
|
} elseif ($middleware instanceof Statement) { |
326
|
|
|
$middlewares[$offset] = $this->resolver->resolve($middleware->getValue(), $middleware->getArguments()); |
327
|
|
|
} elseif ($middleware instanceof Reference) { |
328
|
|
|
$middlewares[$offset] = $this->get((string) $middleware); |
329
|
|
|
} elseif (\is_callable($middleware)) { |
330
|
|
|
$middlewares[$offset] = new CallableMiddlewareDecorator($middleware); |
|
|
|
|
331
|
|
|
} |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
return $middlewares; |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
|