-
Notifications
You must be signed in to change notification settings - Fork 20
/
Application.php
285 lines (240 loc) · 7.84 KB
/
Application.php
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php declare(strict_types=1);
namespace yii\Psr7\web;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use ReflectionMethod;
use Yii;
use yii\base\Component;
use yii\Psr7\web\monitor\ConnectionMonitor;
use yii\Psr7\web\monitor\EventMonitor;
use yii\Psr7\web\Response;
/**
* A Yii2 compatible A PSR-15 RequestHandlerInterface Application component
*
* This class is a \yii\web\Application substitute for use with PSR-7 and PSR-15 middlewares
*/
class Application extends \yii\web\Application implements RequestHandlerInterface
{
/**
* @inheritdoc
*/
public $version = "0.0.2";
/**
* @var array The configuration
*/
private $config;
/**
* @var int $memoryLimit
*/
private $memoryLimit;
/**
* @var array $monitors
*/
private $monitors = [];
/**
* Overloaded constructor to persist configuration
*
* @param array $config
*/
public function __construct(array $config = [])
{
$this->config = $config;
// Set the environment aliases
Yii::setAlias('@webroot', \getenv('YII_ALIAS_WEBROOT'));
Yii::setAlias('@web', \getenv('YII_ALIAS_WEB'));
// This is necessary to get \yii\web\Session to work properly.
ini_set('use_cookies', 'false');
ini_set('use_only_cookies', 'true');
Yii::$app = $this;
static::setInstance($this);
$this->monitors = $this->monitors();
}
/**
* Sets up any monitors we want
*
* @return array
*/
public function monitors()
{
return [
new ConnectionMonitor,
new EventMonitor
];
}
/**
* Re-registers all components with the original configuration
*
* @return void
*/
protected function reset(ServerRequestInterface $request)
{
// Override YII_BEGIN_TIME if possible for yii2-debug
// and other modules that depend on it
if (\function_exists('uopz_redefine')) {
\uopz_redefine('YII_BEGIN_TIME', microtime(true));
}
foreach ($this->monitors as $monitor) {
$monitor->on();
}
$config = $this->config;
$config['components']['request']['psr7Request'] = $request;
$this->state = self::STATE_BEGIN;
$this->preInit($config);
// Deregister any existing error handler since `ErrorHandler::register()` allocates memory on each request
if ($this->has('errorHandler') && $this->getErrorHandler() !== null) {
$this->getErrorHandler()->unregister();
}
$this->registerErrorHandler($config);
Component::__construct($config);
// Session data has to be explicitly loaded before any bootstrapping occurs to ensure compatability
// with bootstrapped components (such as yii2-debug).
if (($session = $this->getSession()) !== null) {
// Close the session if it was open.
$session->close();
// If a session cookie is defined, load it into Yii::$app->session
if (isset($request->getCookieParams()[$session->getName()])) {
$session->setId($request->getCookieParams()[$session->getName()]);
}
}
// Open the session before any modules that need it are bootstrapped.
$this->ensureBehaviors();
$session->open();
$this->bootstrap();
// Once bootstrapping is done we can close the session.
// Accessing it in the future will re-open it.
$session->close();
}
/**
* {@inheritdoc}
*/
public function init()
{
$this->state = self::STATE_INIT;
}
/**
* {@inheritdoc}
*/
protected function bootstrap()
{
// Call the bootstrap method in \yii\base\Application instead of \yii\web\Application
$method = new ReflectionMethod(get_parent_class(get_parent_class($this)), 'bootstrap');
$method->setAccessible(true);
$method->invoke($this);
}
/**
* PSR-15 RequestHandlerInterface
*
* @param ServerRequestInterface $request
* @return ResponseInterface
*/
public function handle(ServerRequestInterface $request) : ResponseInterface
{
try {
$this->reset($request);
$this->state = self::STATE_BEFORE_REQUEST;
$this->trigger(self::EVENT_BEFORE_REQUEST);
$this->state = self::STATE_HANDLING_REQUEST;
$response = $this->handleRequest($this->getRequest());
$this->state = self::STATE_AFTER_REQUEST;
$this->trigger(self::EVENT_AFTER_REQUEST);
$this->state = self::STATE_END;
return $this->terminate($response->getPsr7Response());
} catch (\Exception $e) {
return $this->terminate($this->handleError($e));
} catch (\Throwable $e) {
return $this->terminate($this->handleError($e));
}
}
/**
* Terminates the application
*
* This method handles final log flushing and session termination
*
* @param ResponseInterface $response
* @return ResponseInterface
*/
protected function terminate(ResponseInterface $response) : ResponseInterface
{
// Handle any monitors that are attached
foreach ($this->monitors as $monitor) {
$monitor->shutdown();
}
// Reset fileuploads
\yii\web\UploadedFile::reset();
// Reset the logger
if (($logger = Yii::getLogger()) !== null) {
$logger->flush(true);
}
// Return the parent response
return $response;
}
/**
* Handles exceptions and errors thrown by the request handler
*
* @param \Throwable|\Exception $exception
* @return ResponseInterface
*/
private function handleError(\Throwable $exception) : ResponseInterface
{
$response = $this->getErrorHandler()->handleException($exception);
$this->trigger(self::EVENT_AFTER_REQUEST);
$this->state = self::STATE_END;
return $response->getPsr7Response();
}
/**
* {@inheritdoc}
*/
public function coreComponents()
{
return array_merge(
parent::coreComponents(),
[
'request' => ['class' => \yii\Psr7\web\Request::class],
'response' => ['class' => \yii\Psr7\web\Response::class],
'session' => ['class' => \yii\web\Session::class],
'user' => ['class' => \yii\web\User::class],
'errorHandler' => ['class' => \yii\Psr7\web\ErrorHandler::class],
]
);
}
/**
* Cleanup function to be called at the end of the script execution
* This will automatically run garbage collection, and if the script
* is within 5% of the memory limit will pre-maturely kill the worker
* forcing your task-runner to rebuild it.
*
* This is implemented to avoid requests failing due to memory exhaustion
*
* @return boolean
*/
public function clean()
{
gc_collect_cycles();
$limit = $this->getMemoryLimit();
$bound = $limit * .90;
$usage = memory_get_usage(true);
if ($usage >= $bound) {
return true;
}
return false;
}
/**
* Retrieves the current memory as integer bytes
*
* @return int
*/
private function getMemoryLimit() : int
{
if (!$this->memoryLimit) {
$limit = ini_get('memory_limit');
sscanf($limit, '%u%c', $number, $suffix);
if (isset($suffix)) {
$number = $number * pow(1024, strpos(' KMG', strtoupper($suffix)));
}
$this->memoryLimit = $number;
}
return (int)$this->memoryLimit;
}
}