-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathApplication.php
517 lines (470 loc) · 12.1 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
<?php
namespace tourze\workerman\yii2;
use tourze\workerman\yii2\web\ErrorHandler;
use tourze\workerman\yii2\web\Request;
use tourze\workerman\yii2\web\Response;
use tourze\workerman\yii2\web\Session;
use tourze\workerman\yii2\web\User;
use tourze\workerman\yii2\web\View;
use Workerman\Connection\ConnectionInterface;
use Workerman\Worker;
use Yii;
use yii\base\BootstrapInterface;
use yii\base\Controller;
use yii\base\Event;
use yii\base\InvalidConfigException;
use yii\base\Widget;
/**
* Yii2 Application 类
*
* @property string rootPath
*/
class Application extends \yii\web\Application
{
/**
* @var array 全局配置信息
*/
public static $_globalConfig = [];
/**
* 设置全局配置信息
*
* @param array $config
*/
public static function setGlobalConfig($config)
{
static::$_globalConfig = $config;
}
/**
* 获取全局配置信息
*
* @return array
*/
public static function getGlobalConfig()
{
return static::$_globalConfig;
}
/**
* @var static 当前进行中的$app实例, 存放的是一个通用的, 可以供复制的app实例
*/
public static $workerApp = null;
/**
* @var Worker 当前运行中的服务器实例
*/
protected $_server;
/**
* @return Worker
*/
public function getServer()
{
return $this->_server;
}
/**
* @param Worker $server
*/
public function setServer($server)
{
$this->_server = $server;
}
/**
* @var ConnectionInterface 当前连接
*/
protected $_connection;
/**
* @return ConnectionInterface
*/
public function getConnection()
{
return $this->_connection;
}
/**
* @param ConnectionInterface $connection
*/
public function setConnection($connection)
{
$this->_connection = $connection;
}
/**
* @var string
*/
protected $_rootPath;
/**
* @return string
*/
public function getRootPath()
{
return $this->_rootPath;
}
/**
* @param string $rootPath
*/
public function setRootPath($rootPath)
{
$this->_rootPath = $rootPath;
}
/**
* @var array
*/
public $bootstrapRefresh = [];
/**
* @var array 扩展缓存
*/
public static $defaultExtensionCache = null;
/**
* 获取默认的扩展
*
* @return array|mixed
*/
public function getDefaultExtensions()
{
if (static::$defaultExtensionCache === null)
{
$file = Yii::getAlias('@vendor/yiisoft/extensions.php');
static::$defaultExtensionCache = is_file($file) ? include($file) : [];
}
return static::$defaultExtensionCache;
}
/**
* @var bool
*/
public static $webAliasInit = false;
/**
* 初始化流程
*
* @throws \yii\base\InvalidConfigException
*/
public function bootstrap()
{
if ( ! static::$webAliasInit)
{
$request = $this->getRequest();
Yii::setAlias('@webroot', dirname($request->getScriptFile()));
Yii::setAlias('@web', $request->getBaseUrl());
static::$webAliasInit = true;
}
$this->extensionBootstrap();
$this->moduleBootstrap();
}
/**
* 自动加载扩展的初始化
*
* @throws \yii\base\InvalidConfigException
*/
public function extensionBootstrap()
{
if ( ! $this->extensions)
{
$this->extensions = $this->getDefaultExtensions();
}
foreach ($this->extensions as $k => $extension)
{
if ( ! empty($extension['alias']))
{
foreach ($extension['alias'] as $name => $path)
{
Yii::setAlias($name, $path);
}
}
if (isset($extension['bootstrap']))
{
$this->bootstrap[] = $extension['bootstrap'];
Yii::trace('Push extension bootstrap to module bootstrap list', __METHOD__);
}
}
}
/**
* 自动加载模块的初始化
*
* @throws \yii\base\InvalidConfigException
*/
public function moduleBootstrap()
{
foreach ($this->bootstrap as $k => $class)
{
$component = null;
if (is_string($class))
{
if ($this->has($class))
{
$component = $this->get($class);
}
elseif ($this->hasModule($class))
{
$component = $this->getModule($class);
}
elseif (strpos($class, '\\') === false)
{
throw new InvalidConfigException("Unknown bootstrapping component ID: $class");
}
}
if ( ! isset($component))
{
$component = Yii::createObject($class);
}
if ($component instanceof BootstrapInterface)
{
Yii::trace('Bootstrap with ' . get_class($component) . '::bootstrap()', __METHOD__);
$this->bootstrap[$k] = $component;
$component->bootstrap($this);
$this->bootstrap[$k] = $component;
}
else
{
Yii::trace('Bootstrap with ' . get_class($component), __METHOD__);
}
}
}
/**
* @param $errorHandler
* @throws \yii\base\InvalidConfigException
*/
public function setErrorHandler($errorHandler)
{
$this->set('errorHandler', $errorHandler);
}
/**
* 返回一个异常处理器
*
* @return ErrorHandler
*/
public function getErrorHandler()
{
return parent::getErrorHandler();
}
/**
* 复制一个request对象
*
* @param Request $request
* @throws \yii\base\InvalidConfigException
*/
public function setRequest($request)
{
$this->set('request', $request);
}
/**
* 返回当前request对象
*
* @return Request|\yii\web\Request
*/
public function getRequest()
{
return parent::getRequest();
}
/**
* 复制一个response对象
*
* @param Response $response
* @throws \yii\base\InvalidConfigException
*/
public function setResponse($response)
{
$this->set('response', $response);
}
/**
* 返回当前response对象
*
* @return Response
*/
public function getResponse()
{
return parent::getResponse();
}
/**
* 复制一个view对象
*
* @param View|\yii\web\View $view
* @throws \yii\base\InvalidConfigException
*/
public function setView($view)
{
$this->set('view', $view);
}
/**
* 返回当前view对象
*
* @return View
*/
public function getView()
{
return parent::getView();
}
/**
* 创建会话
*
* @param Session $session
* @throws \yii\base\InvalidConfigException
*/
public function setSession($session)
{
$this->set('session', $session);
}
/**
* 返回当前session对象
*
* @return Session
*/
public function getSession()
{
return parent::getSession();
}
/**
* @return User
*/
public function getUser()
{
return parent::getUser();
}
/**
* @param $user
* @throws \yii\base\InvalidConfigException
*/
public function setUser($user)
{
$this->set('user', $user);
}
/**
* 预热一些可以浅复制的对象
*
* @throws \yii\base\InvalidConfigException
*/
public function prepare()
{
$this->getLog()->setLogger(Yii::getLogger());
$this->getSecurity();
$this->getUrlManager();
$this->getRequest()->setBaseUrl('');
$this->getRequest()->setScriptUrl('/index.php');
$this->getRequest()->setScriptFile('/index.php');
$this->getRequest()->setUrl(null);
$this->getResponse();
foreach ($this->getResponse()->formatters as $type => $class)
{
$this->getResponse()->formatters[$type] = Yii::createObject($class);
}
$this->getSession();
$this->getAssetManager();
$this->getView();
$this->getDb();
$this->getUser();
$this->getMailer();
}
/**
* run之前先准备上下文信息
*/
public function beforeRun()
{
Event::offAll();
// widget计数器等要清空
Widget::$counter = 0;
Widget::$stack = [];
$this->getErrorHandler()->setConnection($this->getConnection());
$this->getRequest()->setConnection($this->getConnection());
$this->getRequest()->setHostInfo('http://' . $_SERVER['HTTP_HOST']);
$this->getRequest()->setPathInfo($_SERVER['ORIG_PATH_INFO']);
$this->getResponse()->setConnection($this->getConnection());
foreach ($this->bootstrap as $k => $component)
{
if ( ! is_object($component))
{
if ($this->has($component))
{
$component = $this->get($component);
}
elseif ($this->hasModule($component))
{
$component = $this->getModule($component);
}
}
if (in_array(get_class($component), $this->bootstrapRefresh))
{
/** @var BootstrapInterface $component */
$component->bootstrap($this);
}
elseif ($component instanceof Refreshable)
{
$component->refresh();
}
}
}
/**
* @inheritdoc
*/
public function run()
{
if ( ! Application::$workerApp)
{
return parent::run();
}
$this->beforeRun();
return parent::run();
}
/**
* 阻止默认的exit执行
*
* @param int $status
* @param mixed $response
* @return int|void
*/
public function end($status = 0, $response = null)
{
if ( ! Application::$workerApp)
{
return parent::run();
}
if ($this->state === self::STATE_BEFORE_REQUEST || $this->state === self::STATE_HANDLING_REQUEST)
{
$this->state = self::STATE_AFTER_REQUEST;
$this->trigger(self::EVENT_AFTER_REQUEST);
}
if ($this->state !== self::STATE_SENDING_RESPONSE && $this->state !== self::STATE_END)
{
$this->state = self::STATE_END;
$response = $response ? : $this->getResponse();
$response->send();
}
return 0;
}
/**
* 用于收尾
*/
public function afterRun()
{
Yii::getLogger()->flush();
$this->getSession()->close();
}
/**
* @var array 保存 id => controller 的实例缓存
*/
public static $controllerIdCache = [];
/**
* 保存控制器实例缓存, 减少一次创建请求的开销
* 能提升些少性能.
* 这里要求控制器在实现时, 业务逻辑尽量不要写在构造函数中
*
* @inheritdoc
*/
public function createControllerByID($id)
{
if ( ! Application::$workerApp)
{
return parent::createControllerByID($id);
}
if ( ! isset(self::$controllerIdCache[$id]))
{
$controller = parent::createControllerByID($id);
if ( ! $controller)
{
return $controller;
}
// 清空id和module的引用
$controller->id = null;
$controller->module = null;
self::$controllerIdCache[$id] = clone $controller;
}
/** @var Controller $controller */
$controller = clone self::$controllerIdCache[$id];
$controller->id = $id;
$controller->module = $this;
$controller->init();
return $controller;
}
}