|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\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\Http\Event\AfterEmit; |
|
13
|
|
|
use Yiisoft\Yii\Http\Event\AfterRequest; |
|
14
|
|
|
use Yiisoft\Yii\Http\Event\ApplicationShutdown; |
|
15
|
|
|
use Yiisoft\Yii\Http\Event\ApplicationStartup; |
|
16
|
|
|
use Yiisoft\Yii\Http\Event\BeforeRequest; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Application 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
|
|
|
* @link https://github.com/yiisoft/docs/blob/master/guide/en/structure/application.md |
|
24
|
|
|
*/ |
|
25
|
|
|
final class Application |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @param MiddlewareDispatcher $dispatcher The middleware dispatcher instance. |
|
29
|
|
|
* @param EventDispatcherInterface $eventDispatcher The event dispatcher instance. |
|
30
|
|
|
* @param RequestHandlerInterface $fallbackHandler The fallback handler that will be called |
|
31
|
|
|
* if no response was returned during request handling. |
|
32
|
|
|
*/ |
|
33
|
8 |
|
public function __construct(private MiddlewareDispatcher $dispatcher, private EventDispatcherInterface $eventDispatcher, private RequestHandlerInterface $fallbackHandler) |
|
34
|
|
|
{ |
|
35
|
8 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Dispatches an event {@see ApplicationStartup} to all relevant listeners for processing. |
|
39
|
|
|
*/ |
|
40
|
1 |
|
public function start(): void |
|
41
|
|
|
{ |
|
42
|
1 |
|
$this->eventDispatcher->dispatch(new ApplicationStartup()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Dispatches an event {@see ApplicationShutdown} to all relevant listeners for processing. |
|
47
|
|
|
*/ |
|
48
|
1 |
|
public function shutdown(): void |
|
49
|
|
|
{ |
|
50
|
1 |
|
$this->eventDispatcher->dispatch(new ApplicationShutdown()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Dispatches an event {@see AfterEmit} to all relevant listeners for processing. |
|
55
|
|
|
* |
|
56
|
|
|
* @param ResponseInterface|null $response Response instance or null if response generation failed due to an error. |
|
57
|
|
|
*/ |
|
58
|
2 |
|
public function afterEmit(?ResponseInterface $response): void |
|
59
|
|
|
{ |
|
60
|
2 |
|
$this->eventDispatcher->dispatch(new AfterEmit($response)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Handles a request by passing it through the middleware stack {@see MiddlewareDispatcher} and returns a response. |
|
65
|
|
|
* |
|
66
|
|
|
* Dispatches {@see BeforeRequest} and {@see AfterRequest} events to all relevant listeners for processing. |
|
67
|
|
|
* |
|
68
|
|
|
* @param ServerRequestInterface $request The request instance to handle. |
|
69
|
|
|
* |
|
70
|
|
|
* @return ResponseInterface The resulting instance of the response. |
|
71
|
|
|
*/ |
|
72
|
4 |
|
public function handle(ServerRequestInterface $request): ResponseInterface |
|
73
|
|
|
{ |
|
74
|
4 |
|
$this->eventDispatcher->dispatch(new BeforeRequest($request)); |
|
75
|
|
|
|
|
76
|
|
|
try { |
|
77
|
4 |
|
return $response = $this->dispatcher->dispatch($request, $this->fallbackHandler); |
|
78
|
|
|
} finally { |
|
79
|
4 |
|
$this->eventDispatcher->dispatch(new AfterRequest($response ?? null)); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|