|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Runner\Http; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
|
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
10
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
11
|
|
|
use Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher; |
|
12
|
|
|
use Yiisoft\Yii\Runner\Http\Event\AfterEmit; |
|
13
|
|
|
use Yiisoft\Yii\Runner\Http\Event\AfterRequest; |
|
14
|
|
|
use Yiisoft\Yii\Runner\Http\Event\ApplicationShutdown; |
|
15
|
|
|
use Yiisoft\Yii\Runner\Http\Event\ApplicationStartup; |
|
16
|
|
|
use Yiisoft\Yii\Runner\Http\Event\BeforeRequest; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* ServerRequestHandler is the entry point for an HTTP application. |
|
20
|
|
|
* |
|
21
|
|
|
* For more details and usage information on Application, see the guide article on applications: |
|
22
|
|
|
* |
|
23
|
|
|
* @see https://github.com/yiisoft/docs/blob/master/guide/en/structure/application.md. |
|
24
|
|
|
*/ |
|
25
|
|
|
final class ServerRequestHandler |
|
26
|
|
|
{ |
|
27
|
|
|
private MiddlewareDispatcher $dispatcher; |
|
28
|
|
|
private EventDispatcherInterface $eventDispatcher; |
|
29
|
|
|
private RequestHandlerInterface $fallbackHandler; |
|
30
|
|
|
|
|
31
|
12 |
|
public function __construct( |
|
32
|
|
|
MiddlewareDispatcher $dispatcher, |
|
33
|
|
|
EventDispatcherInterface $eventDispatcher, |
|
34
|
|
|
RequestHandlerInterface $fallbackHandler |
|
35
|
|
|
) { |
|
36
|
12 |
|
$this->dispatcher = $dispatcher; |
|
37
|
12 |
|
$this->eventDispatcher = $eventDispatcher; |
|
38
|
12 |
|
$this->fallbackHandler = $fallbackHandler; |
|
39
|
12 |
|
} |
|
40
|
|
|
|
|
41
|
5 |
|
public function start(): void |
|
42
|
|
|
{ |
|
43
|
5 |
|
$this->eventDispatcher->dispatch(new ApplicationStartup()); |
|
44
|
5 |
|
} |
|
45
|
|
|
|
|
46
|
5 |
|
public function shutdown(): void |
|
47
|
|
|
{ |
|
48
|
5 |
|
$this->eventDispatcher->dispatch(new ApplicationShutdown()); |
|
49
|
5 |
|
} |
|
50
|
|
|
|
|
51
|
6 |
|
public function afterEmit(?ResponseInterface $response): void |
|
52
|
|
|
{ |
|
53
|
6 |
|
$this->eventDispatcher->dispatch(new AfterEmit($response)); |
|
54
|
6 |
|
} |
|
55
|
|
|
|
|
56
|
8 |
|
public function handle(ServerRequestInterface $request): ResponseInterface |
|
57
|
|
|
{ |
|
58
|
8 |
|
$this->eventDispatcher->dispatch(new BeforeRequest($request)); |
|
59
|
|
|
|
|
60
|
|
|
try { |
|
61
|
8 |
|
return $response = $this->dispatcher->dispatch($request, $this->fallbackHandler); |
|
62
|
|
|
} finally { |
|
63
|
8 |
|
$this->eventDispatcher->dispatch(new AfterRequest($response ?? null)); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|