|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Runner; |
|
6
|
|
|
|
|
7
|
|
|
use App\Handler\ThrowableHandler; |
|
8
|
|
|
use ErrorException; |
|
9
|
|
|
use Psr\Container\ContainerInterface; |
|
10
|
|
|
use Psr\Http\Message\RequestInterface; |
|
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
12
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
13
|
|
|
use Throwable; |
|
14
|
|
|
use Yiisoft\Config\Config; |
|
15
|
|
|
use Yiisoft\Di\Container; |
|
16
|
|
|
use Yiisoft\ErrorHandler\ErrorHandler; |
|
17
|
|
|
use Yiisoft\ErrorHandler\Middleware\ErrorCatcher; |
|
18
|
|
|
use Yiisoft\ErrorHandler\Renderer\HtmlRenderer; |
|
19
|
|
|
use Yiisoft\Definitions\Exception\CircularReferenceException; |
|
20
|
|
|
use Yiisoft\Definitions\Exception\InvalidConfigException; |
|
21
|
|
|
use Yiisoft\Definitions\Exception\NotFoundException; |
|
22
|
|
|
use Yiisoft\Definitions\Exception\NotInstantiableException; |
|
23
|
|
|
use Yiisoft\Http\Method; |
|
24
|
|
|
use Yiisoft\Log\Logger; |
|
25
|
|
|
use Yiisoft\Log\Target\File\FileTarget; |
|
26
|
|
|
use Yiisoft\Yii\Event\ListenerConfigurationChecker; |
|
27
|
|
|
use Yiisoft\Yii\Web\Application; |
|
28
|
|
|
use Yiisoft\Yii\Web\Exception\HeadersHaveBeenSentException; |
|
29
|
|
|
use Yiisoft\Yii\Web\SapiEmitter; |
|
30
|
|
|
use Yiisoft\Yii\Web\ServerRequestFactory; |
|
31
|
|
|
|
|
32
|
|
|
use function dirname; |
|
33
|
|
|
use function microtime; |
|
34
|
|
|
|
|
35
|
|
|
final class WebApplicationRunner |
|
36
|
|
|
{ |
|
37
|
|
|
private bool $debug; |
|
38
|
|
|
private ?string $environment; |
|
39
|
|
|
|
|
40
|
2 |
|
public function __construct(bool $debug, ?string $environment) |
|
41
|
|
|
{ |
|
42
|
2 |
|
$this->debug = $debug; |
|
43
|
2 |
|
$this->environment = $environment; |
|
44
|
2 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @throws CircularReferenceException|ErrorException|HeadersHaveBeenSentException|InvalidConfigException |
|
48
|
|
|
* @throws NotFoundException|NotInstantiableException| |
|
49
|
|
|
*/ |
|
50
|
2 |
|
public function run(): void |
|
51
|
|
|
{ |
|
52
|
2 |
|
$startTime = microtime(true); |
|
53
|
|
|
|
|
54
|
|
|
// Register temporary error handler to catch error while container is building. |
|
55
|
2 |
|
$errorHandler = $this->createTemporaryErrorHandler(); |
|
56
|
2 |
|
$this->registerErrorHandler($errorHandler); |
|
57
|
|
|
|
|
58
|
2 |
|
$config = new Config( |
|
59
|
2 |
|
dirname(__DIR__, 2), |
|
60
|
2 |
|
'/config/packages', // Configs path. |
|
61
|
2 |
|
$this->environment, |
|
62
|
|
|
[ |
|
63
|
2 |
|
'params', |
|
64
|
|
|
'events', |
|
65
|
|
|
'events-web', |
|
66
|
|
|
'events-console', |
|
67
|
|
|
], |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
2 |
|
$container = new Container($config->get('web'), $config->get('providers-web'), [], $this->debug); |
|
71
|
|
|
|
|
72
|
|
|
// Register error handler with real container-configured dependencies. |
|
73
|
2 |
|
$this->registerErrorHandler($container->get(ErrorHandler::class), $errorHandler); |
|
74
|
|
|
|
|
75
|
|
|
// Run bootstrap |
|
76
|
2 |
|
$this->runBootstrap($container, $config->get('bootstrap-web')); |
|
77
|
|
|
|
|
78
|
2 |
|
$container = $container->get(ContainerInterface::class); |
|
79
|
|
|
|
|
80
|
2 |
|
if ($this->debug) { |
|
81
|
|
|
/** @psalm-suppress MixedMethodCall */ |
|
82
|
2 |
|
$container->get(ListenerConfigurationChecker::class)->check($config->get('events-web')); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** @var Application */ |
|
86
|
2 |
|
$application = $container->get(Application::class); |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @var ServerRequestInterface |
|
90
|
|
|
* @psalm-suppress MixedMethodCall |
|
91
|
|
|
*/ |
|
92
|
2 |
|
$serverRequest = $container->get(ServerRequestFactory::class)->createFromGlobals(); |
|
93
|
2 |
|
$request = $serverRequest->withAttribute('applicationStartTime', $startTime); |
|
94
|
|
|
|
|
95
|
|
|
try { |
|
96
|
2 |
|
$application->start(); |
|
97
|
2 |
|
$response = $application->handle($request); |
|
98
|
2 |
|
$this->emit($request, $response); |
|
99
|
|
|
} catch (Throwable $throwable) { |
|
100
|
|
|
$handler = new ThrowableHandler($throwable); |
|
101
|
|
|
/** |
|
102
|
|
|
* @var ResponseInterface |
|
103
|
|
|
* @psalm-suppress MixedMethodCall |
|
104
|
|
|
*/ |
|
105
|
|
|
$response = $container->get(ErrorCatcher::class)->process($request, $handler); |
|
106
|
|
|
$this->emit($request, $response); |
|
107
|
2 |
|
} finally { |
|
108
|
2 |
|
$application->afterEmit($response ?? null); |
|
109
|
2 |
|
$application->shutdown(); |
|
110
|
|
|
} |
|
111
|
2 |
|
} |
|
112
|
|
|
|
|
113
|
2 |
|
private function createTemporaryErrorHandler(): ErrorHandler |
|
114
|
|
|
{ |
|
115
|
2 |
|
$logger = new Logger([new FileTarget(dirname(__DIR__) . '/runtime/logs/app.log')]); |
|
116
|
2 |
|
return new ErrorHandler($logger, new HtmlRenderer()); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @throws HeadersHaveBeenSentException |
|
121
|
|
|
*/ |
|
122
|
2 |
|
private function emit(RequestInterface $request, ResponseInterface $response): void |
|
123
|
|
|
{ |
|
124
|
2 |
|
(new SapiEmitter())->emit($response, $request->getMethod() === Method::HEAD); |
|
125
|
2 |
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @throws ErrorException |
|
129
|
|
|
*/ |
|
130
|
2 |
|
private function registerErrorHandler(ErrorHandler $registered, ErrorHandler $unregistered = null): void |
|
131
|
|
|
{ |
|
132
|
2 |
|
if ($unregistered !== null) { |
|
133
|
2 |
|
$unregistered->unregister(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
2 |
|
if ($this->debug) { |
|
137
|
2 |
|
$registered->debug(); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
2 |
|
$registered->register(); |
|
141
|
2 |
|
} |
|
142
|
|
|
|
|
143
|
2 |
|
private function runBootstrap(Container $container, array $bootstrapList): void |
|
144
|
|
|
{ |
|
145
|
2 |
|
(new BootstrapRunner($container, $bootstrapList))->run(); |
|
146
|
2 |
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|