1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @link https://www.yiiframework.com/ |
5
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
6
|
|
|
* @license https://www.yiiframework.com/license/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace yii\base; |
10
|
|
|
|
11
|
|
|
use Yii; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Application is the base class for all application classes. |
15
|
|
|
* |
16
|
|
|
* For more details and usage information on Application, see the [guide article on applications](guide:structure-applications). |
17
|
|
|
* |
18
|
|
|
* @property-read \yii\web\AssetManager $assetManager The asset manager application component. |
19
|
|
|
* @property-read \yii\rbac\ManagerInterface|null $authManager The auth manager application component or null |
20
|
|
|
* if it's not configured. |
21
|
|
|
* @property string $basePath The root directory of the application. |
22
|
|
|
* @property-read \yii\caching\CacheInterface|null $cache The cache application component. Null if the |
23
|
|
|
* component is not enabled. |
24
|
|
|
* @property-write array $container Values given in terms of name-value pairs. |
25
|
|
|
* @property-read \yii\db\Connection $db The database connection. |
26
|
|
|
* @property-read \yii\web\ErrorHandler|\yii\console\ErrorHandler $errorHandler The error handler application |
27
|
|
|
* component. |
28
|
|
|
* @property-read \yii\i18n\Formatter $formatter The formatter application component. |
29
|
|
|
* @property-read \yii\i18n\I18N $i18n The internationalization application component. |
30
|
|
|
* @property-read \yii\log\Dispatcher $log The log dispatcher application component. |
31
|
|
|
* @property-read \yii\mail\MailerInterface $mailer The mailer application component. |
32
|
|
|
* @property-read \yii\web\Request|\yii\console\Request $request The request component. |
33
|
|
|
* @property-read \yii\web\Response|\yii\console\Response $response The response component. |
34
|
|
|
* @property string $runtimePath The directory that stores runtime files. Defaults to the "runtime" |
35
|
|
|
* subdirectory under [[basePath]]. |
36
|
|
|
* @property-read \yii\base\Security $security The security application component. |
37
|
|
|
* @property string $timeZone The time zone used by this application. |
38
|
|
|
* @property-read string $uniqueId The unique ID of the module. |
39
|
|
|
* @property-read \yii\web\UrlManager $urlManager The URL manager for this application. |
40
|
|
|
* @property string $vendorPath The directory that stores vendor files. Defaults to "vendor" directory under |
41
|
|
|
* [[basePath]]. |
42
|
|
|
* @property-read View|\yii\web\View $view The view application component that is used to render various view |
43
|
|
|
* files. |
44
|
|
|
* |
45
|
|
|
* @author Qiang Xue <[email protected]> |
46
|
|
|
* @since 2.0 |
47
|
|
|
*/ |
48
|
|
|
abstract class Application extends Module |
49
|
|
|
{ |
50
|
|
|
/** |
51
|
|
|
* @event Event an event raised before the application starts to handle a request. |
52
|
|
|
*/ |
53
|
|
|
const EVENT_BEFORE_REQUEST = 'beforeRequest'; |
54
|
|
|
/** |
55
|
|
|
* @event Event an event raised after the application successfully handles a request (before the response is sent out). |
56
|
|
|
*/ |
57
|
|
|
const EVENT_AFTER_REQUEST = 'afterRequest'; |
58
|
|
|
/** |
59
|
|
|
* Application state used by [[state]]: application just started. |
60
|
|
|
*/ |
61
|
|
|
const STATE_BEGIN = 0; |
62
|
|
|
/** |
63
|
|
|
* Application state used by [[state]]: application is initializing. |
64
|
|
|
*/ |
65
|
|
|
const STATE_INIT = 1; |
66
|
|
|
/** |
67
|
|
|
* Application state used by [[state]]: application is triggering [[EVENT_BEFORE_REQUEST]]. |
68
|
|
|
*/ |
69
|
|
|
const STATE_BEFORE_REQUEST = 2; |
70
|
|
|
/** |
71
|
|
|
* Application state used by [[state]]: application is handling the request. |
72
|
|
|
*/ |
73
|
|
|
const STATE_HANDLING_REQUEST = 3; |
74
|
|
|
/** |
75
|
|
|
* Application state used by [[state]]: application is triggering [[EVENT_AFTER_REQUEST]].. |
76
|
|
|
*/ |
77
|
|
|
const STATE_AFTER_REQUEST = 4; |
78
|
|
|
/** |
79
|
|
|
* Application state used by [[state]]: application is about to send response. |
80
|
|
|
*/ |
81
|
|
|
const STATE_SENDING_RESPONSE = 5; |
82
|
|
|
/** |
83
|
|
|
* Application state used by [[state]]: application has ended. |
84
|
|
|
*/ |
85
|
|
|
const STATE_END = 6; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @var string the namespace that controller classes are located in. |
89
|
|
|
* This namespace will be used to load controller classes by prepending it to the controller class name. |
90
|
|
|
* The default namespace is `app\controllers`. |
91
|
|
|
* |
92
|
|
|
* Please refer to the [guide about class autoloading](guide:concept-autoloading.md) for more details. |
93
|
|
|
*/ |
94
|
|
|
public $controllerNamespace = 'app\\controllers'; |
95
|
|
|
/** |
96
|
|
|
* @var string the application name. |
97
|
|
|
*/ |
98
|
|
|
public $name = 'My Application'; |
99
|
|
|
/** |
100
|
|
|
* @var string the charset currently used for the application. |
101
|
|
|
*/ |
102
|
|
|
public $charset = 'UTF-8'; |
103
|
|
|
/** |
104
|
|
|
* @var string the language that is meant to be used for end users. It is recommended that you |
105
|
|
|
* use [IETF language tags](https://en.wikipedia.org/wiki/IETF_language_tag). For example, `en` stands |
106
|
|
|
* for English, while `en-US` stands for English (United States). |
107
|
|
|
* @see sourceLanguage |
108
|
|
|
*/ |
109
|
|
|
public $language = 'en-US'; |
110
|
|
|
/** |
111
|
|
|
* @var string the language that the application is written in. This mainly refers to |
112
|
|
|
* the language that the messages and view files are written in. |
113
|
|
|
* @see language |
114
|
|
|
*/ |
115
|
|
|
public $sourceLanguage = 'en-US'; |
116
|
|
|
/** |
117
|
|
|
* @var Controller the currently active controller instance |
118
|
|
|
*/ |
119
|
|
|
public $controller; |
120
|
|
|
/** |
121
|
|
|
* @var string|bool the layout that should be applied for views in this application. Defaults to 'main'. |
122
|
|
|
* If this is false, layout will be disabled. |
123
|
|
|
*/ |
124
|
|
|
public $layout = 'main'; |
125
|
|
|
/** |
126
|
|
|
* @var string the requested route |
127
|
|
|
*/ |
128
|
|
|
public $requestedRoute; |
129
|
|
|
/** |
130
|
|
|
* @var Action|null the requested Action. If null, it means the request cannot be resolved into an action. |
131
|
|
|
*/ |
132
|
|
|
public $requestedAction; |
133
|
|
|
/** |
134
|
|
|
* @var array the parameters supplied to the requested action. |
135
|
|
|
*/ |
136
|
|
|
public $requestedParams; |
137
|
|
|
/** |
138
|
|
|
* @var array|null list of installed Yii extensions. Each array element represents a single extension |
139
|
|
|
* with the following structure: |
140
|
|
|
* |
141
|
|
|
* ```php |
142
|
|
|
* [ |
143
|
|
|
* 'name' => 'extension name', |
144
|
|
|
* 'version' => 'version number', |
145
|
|
|
* 'bootstrap' => 'BootstrapClassName', // optional, may also be a configuration array |
146
|
|
|
* 'alias' => [ |
147
|
|
|
* '@alias1' => 'to/path1', |
148
|
|
|
* '@alias2' => 'to/path2', |
149
|
|
|
* ], |
150
|
|
|
* ] |
151
|
|
|
* ``` |
152
|
|
|
* |
153
|
|
|
* The "bootstrap" class listed above will be instantiated during the application |
154
|
|
|
* [[bootstrap()|bootstrapping process]]. If the class implements [[BootstrapInterface]], |
155
|
|
|
* its [[BootstrapInterface::bootstrap()|bootstrap()]] method will be also be called. |
156
|
|
|
* |
157
|
|
|
* If not set explicitly in the application config, this property will be populated with the contents of |
158
|
|
|
* `@vendor/yiisoft/extensions.php`. |
159
|
|
|
*/ |
160
|
|
|
public $extensions; |
161
|
|
|
/** |
162
|
|
|
* @var array list of components that should be run during the application [[bootstrap()|bootstrapping process]]. |
163
|
|
|
* |
164
|
|
|
* Each component may be specified in one of the following formats: |
165
|
|
|
* |
166
|
|
|
* - an application component ID as specified via [[components]]. |
167
|
|
|
* - a module ID as specified via [[modules]]. |
168
|
|
|
* - a class name. |
169
|
|
|
* - a configuration array. |
170
|
|
|
* - a Closure |
171
|
|
|
* |
172
|
|
|
* During the bootstrapping process, each component will be instantiated. If the component class |
173
|
|
|
* implements [[BootstrapInterface]], its [[BootstrapInterface::bootstrap()|bootstrap()]] method |
174
|
|
|
* will be also be called. |
175
|
|
|
*/ |
176
|
|
|
public $bootstrap = []; |
177
|
|
|
/** |
178
|
|
|
* @var int the current application state during a request handling life cycle. |
179
|
|
|
* This property is managed by the application. Do not modify this property. |
180
|
|
|
*/ |
181
|
|
|
public $state; |
182
|
|
|
/** |
183
|
|
|
* @var array list of loaded modules indexed by their class names. |
184
|
|
|
*/ |
185
|
|
|
public $loadedModules = []; |
186
|
|
|
|
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Constructor. |
190
|
|
|
* @param array $config name-value pairs that will be used to initialize the object properties. |
191
|
|
|
* Note that the configuration must contain both [[id]] and [[basePath]]. |
192
|
|
|
* @throws InvalidConfigException if either [[id]] or [[basePath]] configuration is missing. |
193
|
|
|
*/ |
194
|
4509 |
|
public function __construct($config = []) |
195
|
|
|
{ |
196
|
4509 |
|
Yii::$app = $this; |
|
|
|
|
197
|
4509 |
|
static::setInstance($this); |
198
|
|
|
|
199
|
4509 |
|
$this->state = self::STATE_BEGIN; |
200
|
|
|
|
201
|
4509 |
|
$this->preInit($config); |
202
|
|
|
|
203
|
4509 |
|
$this->registerErrorHandler($config); |
204
|
|
|
|
205
|
4509 |
|
Component::__construct($config); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Pre-initializes the application. |
210
|
|
|
* This method is called at the beginning of the application constructor. |
211
|
|
|
* It initializes several important application properties. |
212
|
|
|
* If you override this method, please make sure you call the parent implementation. |
213
|
|
|
* @param array $config the application configuration |
214
|
|
|
* @throws InvalidConfigException if either [[id]] or [[basePath]] configuration is missing. |
215
|
|
|
*/ |
216
|
4509 |
|
public function preInit(&$config) |
217
|
|
|
{ |
218
|
4509 |
|
if (!isset($config['id'])) { |
219
|
|
|
throw new InvalidConfigException('The "id" configuration for the Application is required.'); |
220
|
|
|
} |
221
|
4509 |
|
if (isset($config['basePath'])) { |
222
|
4509 |
|
$this->setBasePath($config['basePath']); |
223
|
4509 |
|
unset($config['basePath']); |
224
|
|
|
} else { |
225
|
|
|
throw new InvalidConfigException('The "basePath" configuration for the Application is required.'); |
226
|
|
|
} |
227
|
|
|
|
228
|
4509 |
|
if (isset($config['vendorPath'])) { |
229
|
4502 |
|
$this->setVendorPath($config['vendorPath']); |
230
|
4502 |
|
unset($config['vendorPath']); |
231
|
|
|
} else { |
232
|
|
|
// set "@vendor" |
233
|
24 |
|
$this->getVendorPath(); |
234
|
|
|
} |
235
|
4509 |
|
if (isset($config['runtimePath'])) { |
236
|
|
|
$this->setRuntimePath($config['runtimePath']); |
237
|
|
|
unset($config['runtimePath']); |
238
|
|
|
} else { |
239
|
|
|
// set "@runtime" |
240
|
4509 |
|
$this->getRuntimePath(); |
241
|
|
|
} |
242
|
|
|
|
243
|
4509 |
|
if (isset($config['timeZone'])) { |
244
|
800 |
|
$this->setTimeZone($config['timeZone']); |
245
|
800 |
|
unset($config['timeZone']); |
246
|
3715 |
|
} elseif (!ini_get('date.timezone')) { |
247
|
|
|
$this->setTimeZone('UTC'); |
248
|
|
|
} |
249
|
|
|
|
250
|
4509 |
|
if (isset($config['container'])) { |
251
|
9 |
|
$this->setContainer($config['container']); |
252
|
|
|
|
253
|
9 |
|
unset($config['container']); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
// merge core components with custom components |
257
|
4509 |
|
foreach ($this->coreComponents() as $id => $component) { |
258
|
4509 |
|
if (!isset($config['components'][$id])) { |
259
|
4509 |
|
$config['components'][$id] = $component; |
260
|
675 |
|
} elseif (is_array($config['components'][$id]) && !isset($config['components'][$id]['class'])) { |
261
|
465 |
|
$config['components'][$id]['class'] = $component['class']; |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* {@inheritdoc} |
268
|
|
|
*/ |
269
|
4509 |
|
public function init() |
270
|
|
|
{ |
271
|
4509 |
|
$this->state = self::STATE_INIT; |
272
|
4509 |
|
$this->bootstrap(); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Initializes extensions and executes bootstrap components. |
277
|
|
|
* This method is called by [[init()]] after the application has been fully configured. |
278
|
|
|
* If you override this method, make sure you also call the parent implementation. |
279
|
|
|
*/ |
280
|
4509 |
|
protected function bootstrap() |
281
|
|
|
{ |
282
|
4509 |
|
if ($this->extensions === null) { |
283
|
4509 |
|
$file = Yii::getAlias('@vendor/yiisoft/extensions.php'); |
284
|
4509 |
|
$this->extensions = is_file($file) ? include $file : []; |
|
|
|
|
285
|
|
|
} |
286
|
4509 |
|
foreach ($this->extensions as $extension) { |
287
|
|
|
if (!empty($extension['alias'])) { |
288
|
|
|
foreach ($extension['alias'] as $name => $path) { |
289
|
|
|
Yii::setAlias($name, $path); |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
if (isset($extension['bootstrap'])) { |
293
|
|
|
$component = Yii::createObject($extension['bootstrap']); |
294
|
|
|
if ($component instanceof BootstrapInterface) { |
295
|
|
|
Yii::debug('Bootstrap with ' . get_class($component) . '::bootstrap()', __METHOD__); |
296
|
|
|
$component->bootstrap($this); |
297
|
|
|
} else { |
298
|
|
|
Yii::debug('Bootstrap with ' . get_class($component), __METHOD__); |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
|
303
|
4509 |
|
foreach ($this->bootstrap as $mixed) { |
304
|
2 |
|
$component = null; |
305
|
2 |
|
if ($mixed instanceof \Closure) { |
306
|
1 |
|
Yii::debug('Bootstrap with Closure', __METHOD__); |
307
|
1 |
|
if (!$component = call_user_func($mixed, $this)) { |
308
|
1 |
|
continue; |
309
|
|
|
} |
310
|
2 |
|
} elseif (is_string($mixed)) { |
311
|
2 |
|
if ($this->has($mixed)) { |
312
|
2 |
|
$component = $this->get($mixed); |
313
|
1 |
|
} elseif ($this->hasModule($mixed)) { |
314
|
1 |
|
$component = $this->getModule($mixed); |
315
|
|
|
} elseif (strpos($mixed, '\\') === false) { |
316
|
|
|
throw new InvalidConfigException("Unknown bootstrapping component ID: $mixed"); |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
|
320
|
2 |
|
if (!isset($component)) { |
321
|
|
|
$component = Yii::createObject($mixed); |
322
|
|
|
} |
323
|
|
|
|
324
|
2 |
|
if ($component instanceof BootstrapInterface) { |
325
|
1 |
|
Yii::debug('Bootstrap with ' . get_class($component) . '::bootstrap()', __METHOD__); |
326
|
1 |
|
$component->bootstrap($this); |
327
|
|
|
} else { |
328
|
2 |
|
Yii::debug('Bootstrap with ' . get_class($component), __METHOD__); |
|
|
|
|
329
|
|
|
} |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Registers the errorHandler component as a PHP error handler. |
335
|
|
|
* @param array $config application config |
336
|
|
|
*/ |
337
|
4509 |
|
protected function registerErrorHandler(&$config) |
338
|
|
|
{ |
339
|
4509 |
|
if (YII_ENABLE_ERROR_HANDLER) { |
340
|
|
|
if (!isset($config['components']['errorHandler']['class'])) { |
341
|
|
|
echo "Error: no errorHandler component is configured.\n"; |
342
|
|
|
exit(1); |
|
|
|
|
343
|
|
|
} |
344
|
|
|
$this->set('errorHandler', $config['components']['errorHandler']); |
345
|
|
|
unset($config['components']['errorHandler']); |
346
|
|
|
$this->getErrorHandler()->register(); |
347
|
|
|
} |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Returns an ID that uniquely identifies this module among all modules within the current application. |
352
|
|
|
* Since this is an application instance, it will always return an empty string. |
353
|
|
|
* @return string the unique ID of the module. |
354
|
|
|
*/ |
355
|
2 |
|
public function getUniqueId() |
356
|
|
|
{ |
357
|
2 |
|
return ''; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Sets the root directory of the application and the @app alias. |
362
|
|
|
* This method can only be invoked at the beginning of the constructor. |
363
|
|
|
* @param string $path the root directory of the application. |
364
|
|
|
* @property string the root directory of the application. |
365
|
|
|
* @throws InvalidArgumentException if the directory does not exist. |
366
|
|
|
*/ |
367
|
4509 |
|
public function setBasePath($path) |
368
|
|
|
{ |
369
|
4509 |
|
parent::setBasePath($path); |
370
|
4509 |
|
Yii::setAlias('@app', $this->getBasePath()); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* Runs the application. |
375
|
|
|
* This is the main entrance of an application. |
376
|
|
|
* @return int the exit status (0 means normal, non-zero values mean abnormal) |
377
|
|
|
*/ |
378
|
|
|
public function run() |
379
|
|
|
{ |
380
|
|
|
try { |
381
|
|
|
$this->state = self::STATE_BEFORE_REQUEST; |
382
|
|
|
$this->trigger(self::EVENT_BEFORE_REQUEST); |
383
|
|
|
|
384
|
|
|
$this->state = self::STATE_HANDLING_REQUEST; |
385
|
|
|
$response = $this->handleRequest($this->getRequest()); |
386
|
|
|
|
387
|
|
|
$this->state = self::STATE_AFTER_REQUEST; |
388
|
|
|
$this->trigger(self::EVENT_AFTER_REQUEST); |
389
|
|
|
|
390
|
|
|
$this->state = self::STATE_SENDING_RESPONSE; |
391
|
|
|
$response->send(); |
392
|
|
|
|
393
|
|
|
$this->state = self::STATE_END; |
394
|
|
|
|
395
|
|
|
return $response->exitStatus; |
396
|
|
|
} catch (ExitException $e) { |
397
|
|
|
$this->end($e->statusCode, isset($response) ? $response : null); |
398
|
|
|
return $e->statusCode; |
399
|
|
|
} |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* Handles the specified request. |
404
|
|
|
* |
405
|
|
|
* This method should return an instance of [[Response]] or its child class |
406
|
|
|
* which represents the handling result of the request. |
407
|
|
|
* |
408
|
|
|
* @param Request $request the request to be handled |
409
|
|
|
* @return Response the resulting response |
410
|
|
|
*/ |
411
|
|
|
abstract public function handleRequest($request); |
412
|
|
|
|
413
|
|
|
private $_runtimePath; |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* Returns the directory that stores runtime files. |
417
|
|
|
* @return string the directory that stores runtime files. |
418
|
|
|
* Defaults to the "runtime" subdirectory under [[basePath]]. |
419
|
|
|
*/ |
420
|
4509 |
|
public function getRuntimePath() |
421
|
|
|
{ |
422
|
4509 |
|
if ($this->_runtimePath === null) { |
423
|
4509 |
|
$this->setRuntimePath($this->getBasePath() . DIRECTORY_SEPARATOR . 'runtime'); |
424
|
|
|
} |
425
|
|
|
|
426
|
4509 |
|
return $this->_runtimePath; |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* Sets the directory that stores runtime files. |
431
|
|
|
* @param string $path the directory that stores runtime files. |
432
|
|
|
*/ |
433
|
4509 |
|
public function setRuntimePath($path) |
434
|
|
|
{ |
435
|
4509 |
|
$this->_runtimePath = Yii::getAlias($path); |
436
|
4509 |
|
Yii::setAlias('@runtime', $this->_runtimePath); |
|
|
|
|
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
private $_vendorPath; |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* Returns the directory that stores vendor files. |
443
|
|
|
* @return string the directory that stores vendor files. |
444
|
|
|
* Defaults to "vendor" directory under [[basePath]]. |
445
|
|
|
*/ |
446
|
24 |
|
public function getVendorPath() |
447
|
|
|
{ |
448
|
24 |
|
if ($this->_vendorPath === null) { |
449
|
24 |
|
$this->setVendorPath($this->getBasePath() . DIRECTORY_SEPARATOR . 'vendor'); |
450
|
|
|
} |
451
|
|
|
|
452
|
24 |
|
return $this->_vendorPath; |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* Sets the directory that stores vendor files. |
457
|
|
|
* @param string $path the directory that stores vendor files. |
458
|
|
|
*/ |
459
|
4509 |
|
public function setVendorPath($path) |
460
|
|
|
{ |
461
|
4509 |
|
$this->_vendorPath = Yii::getAlias($path); |
462
|
4509 |
|
Yii::setAlias('@vendor', $this->_vendorPath); |
|
|
|
|
463
|
4509 |
|
Yii::setAlias('@bower', $this->_vendorPath . DIRECTORY_SEPARATOR . 'bower'); |
|
|
|
|
464
|
4509 |
|
Yii::setAlias('@npm', $this->_vendorPath . DIRECTORY_SEPARATOR . 'npm'); |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
/** |
468
|
|
|
* Returns the time zone used by this application. |
469
|
|
|
* This is a simple wrapper of PHP function date_default_timezone_get(). |
470
|
|
|
* If time zone is not configured in php.ini or application config, |
471
|
|
|
* it will be set to UTC by default. |
472
|
|
|
* @return string the time zone used by this application. |
473
|
|
|
* @see https://www.php.net/manual/en/function.date-default-timezone-get.php |
474
|
|
|
*/ |
475
|
414 |
|
public function getTimeZone() |
476
|
|
|
{ |
477
|
414 |
|
return date_default_timezone_get(); |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
/** |
481
|
|
|
* Sets the time zone used by this application. |
482
|
|
|
* This is a simple wrapper of PHP function date_default_timezone_set(). |
483
|
|
|
* Refer to the [php manual](https://www.php.net/manual/en/timezones.php) for available timezones. |
484
|
|
|
* @param string $value the time zone used by this application. |
485
|
|
|
* @see https://www.php.net/manual/en/function.date-default-timezone-set.php |
486
|
|
|
*/ |
487
|
800 |
|
public function setTimeZone($value) |
488
|
|
|
{ |
489
|
800 |
|
date_default_timezone_set($value); |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
/** |
493
|
|
|
* Returns the database connection component. |
494
|
|
|
* @return \yii\db\Connection the database connection. |
495
|
|
|
*/ |
496
|
112 |
|
public function getDb() |
497
|
|
|
{ |
498
|
112 |
|
return $this->get('db'); |
|
|
|
|
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
/** |
502
|
|
|
* Returns the log dispatcher component. |
503
|
|
|
* @return \yii\log\Dispatcher the log dispatcher application component. |
504
|
|
|
*/ |
505
|
6 |
|
public function getLog() |
506
|
|
|
{ |
507
|
6 |
|
return $this->get('log'); |
|
|
|
|
508
|
|
|
} |
509
|
|
|
|
510
|
|
|
/** |
511
|
|
|
* Returns the error handler component. |
512
|
|
|
* @return \yii\web\ErrorHandler|\yii\console\ErrorHandler the error handler application component. |
513
|
|
|
*/ |
514
|
|
|
public function getErrorHandler() |
515
|
|
|
{ |
516
|
|
|
return $this->get('errorHandler'); |
|
|
|
|
517
|
|
|
} |
518
|
|
|
|
519
|
|
|
/** |
520
|
|
|
* Returns the cache component. |
521
|
|
|
* @return \yii\caching\CacheInterface|null the cache application component. Null if the component is not enabled. |
522
|
|
|
*/ |
523
|
|
|
public function getCache() |
524
|
|
|
{ |
525
|
|
|
return $this->get('cache', false); |
|
|
|
|
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
/** |
529
|
|
|
* Returns the formatter component. |
530
|
|
|
* @return \yii\i18n\Formatter the formatter application component. |
531
|
|
|
*/ |
532
|
26 |
|
public function getFormatter() |
533
|
|
|
{ |
534
|
26 |
|
return $this->get('formatter'); |
|
|
|
|
535
|
|
|
} |
536
|
|
|
|
537
|
|
|
/** |
538
|
|
|
* Returns the request component. |
539
|
|
|
* @return \yii\web\Request|\yii\console\Request the request component. |
540
|
|
|
*/ |
541
|
|
|
public function getRequest() |
542
|
|
|
{ |
543
|
|
|
return $this->get('request'); |
|
|
|
|
544
|
|
|
} |
545
|
|
|
|
546
|
|
|
/** |
547
|
|
|
* Returns the response component. |
548
|
|
|
* @return \yii\web\Response|\yii\console\Response the response component. |
549
|
|
|
*/ |
550
|
|
|
public function getResponse() |
551
|
|
|
{ |
552
|
|
|
return $this->get('response'); |
|
|
|
|
553
|
|
|
} |
554
|
|
|
|
555
|
|
|
/** |
556
|
|
|
* Returns the view object. |
557
|
|
|
* @return View|\yii\web\View the view application component that is used to render various view files. |
558
|
|
|
*/ |
559
|
124 |
|
public function getView() |
560
|
|
|
{ |
561
|
124 |
|
return $this->get('view'); |
|
|
|
|
562
|
|
|
} |
563
|
|
|
|
564
|
|
|
/** |
565
|
|
|
* Returns the URL manager for this application. |
566
|
|
|
* @return \yii\web\UrlManager the URL manager for this application. |
567
|
|
|
*/ |
568
|
60 |
|
public function getUrlManager() |
569
|
|
|
{ |
570
|
60 |
|
return $this->get('urlManager'); |
|
|
|
|
571
|
|
|
} |
572
|
|
|
|
573
|
|
|
/** |
574
|
|
|
* Returns the internationalization (i18n) component. |
575
|
|
|
* @return \yii\i18n\I18N the internationalization application component. |
576
|
|
|
*/ |
577
|
765 |
|
public function getI18n() |
578
|
|
|
{ |
579
|
765 |
|
return $this->get('i18n'); |
|
|
|
|
580
|
|
|
} |
581
|
|
|
|
582
|
|
|
/** |
583
|
|
|
* Returns the mailer component. |
584
|
|
|
* @return \yii\mail\MailerInterface the mailer application component. |
585
|
|
|
* @throws InvalidConfigException If this component is not configured. |
586
|
|
|
*/ |
587
|
|
|
public function getMailer() |
588
|
|
|
{ |
589
|
|
|
return $this->get('mailer'); |
|
|
|
|
590
|
|
|
} |
591
|
|
|
|
592
|
|
|
/** |
593
|
|
|
* Returns the auth manager for this application. |
594
|
|
|
* @return \yii\rbac\ManagerInterface|null the auth manager application component or null if it's not configured. |
595
|
|
|
*/ |
596
|
2 |
|
public function getAuthManager() |
597
|
|
|
{ |
598
|
2 |
|
return $this->get('authManager', false); |
|
|
|
|
599
|
|
|
} |
600
|
|
|
|
601
|
|
|
/** |
602
|
|
|
* Returns the asset manager. |
603
|
|
|
* @return \yii\web\AssetManager the asset manager application component. |
604
|
|
|
*/ |
605
|
13 |
|
public function getAssetManager() |
606
|
|
|
{ |
607
|
13 |
|
return $this->get('assetManager'); |
|
|
|
|
608
|
|
|
} |
609
|
|
|
|
610
|
|
|
/** |
611
|
|
|
* Returns the security component. |
612
|
|
|
* @return \yii\base\Security the security application component. |
613
|
|
|
*/ |
614
|
94 |
|
public function getSecurity() |
615
|
|
|
{ |
616
|
94 |
|
return $this->get('security'); |
|
|
|
|
617
|
|
|
} |
618
|
|
|
|
619
|
|
|
/** |
620
|
|
|
* Returns the configuration of core application components. |
621
|
|
|
* @return array |
622
|
|
|
* @see set() |
623
|
|
|
*/ |
624
|
4509 |
|
public function coreComponents() |
625
|
|
|
{ |
626
|
4509 |
|
$components = [ |
627
|
4509 |
|
'log' => ['class' => 'yii\log\Dispatcher'], |
628
|
4509 |
|
'view' => ['class' => 'yii\web\View'], |
629
|
4509 |
|
'formatter' => ['class' => 'yii\i18n\Formatter'], |
630
|
4509 |
|
'i18n' => ['class' => 'yii\i18n\I18N'], |
631
|
4509 |
|
'urlManager' => ['class' => 'yii\web\UrlManager'], |
632
|
4509 |
|
'assetManager' => ['class' => 'yii\web\AssetManager'], |
633
|
4509 |
|
'security' => ['class' => 'yii\base\Security'], |
634
|
4509 |
|
]; |
635
|
4509 |
|
if (class_exists('yii\swiftmailer\Mailer')) { |
636
|
|
|
$components['mailer'] = ['class' => 'yii\swiftmailer\Mailer']; |
637
|
|
|
} |
638
|
|
|
|
639
|
4509 |
|
return $components; |
640
|
|
|
} |
641
|
|
|
|
642
|
|
|
/** |
643
|
|
|
* Terminates the application. |
644
|
|
|
* This method replaces the `exit()` function by ensuring the application life cycle is completed |
645
|
|
|
* before terminating the application. |
646
|
|
|
* @param int $status the exit status (value 0 means normal exit while other values mean abnormal exit). |
647
|
|
|
* @param Response|null $response the response to be sent. If not set, the default application [[response]] component will be used. |
648
|
|
|
* @throws ExitException if the application is in testing mode |
649
|
|
|
*/ |
650
|
3 |
|
public function end($status = 0, $response = null) |
651
|
|
|
{ |
652
|
3 |
|
if ($this->state === self::STATE_BEFORE_REQUEST || $this->state === self::STATE_HANDLING_REQUEST) { |
653
|
|
|
$this->state = self::STATE_AFTER_REQUEST; |
654
|
|
|
$this->trigger(self::EVENT_AFTER_REQUEST); |
655
|
|
|
} |
656
|
|
|
|
657
|
3 |
|
if ($this->state !== self::STATE_SENDING_RESPONSE && $this->state !== self::STATE_END) { |
658
|
3 |
|
$this->state = self::STATE_END; |
659
|
3 |
|
$response = $response ?: $this->getResponse(); |
660
|
3 |
|
$response->send(); |
661
|
|
|
} |
662
|
|
|
|
663
|
3 |
|
if (YII_ENV_TEST) { |
664
|
3 |
|
throw new ExitException($status); |
665
|
|
|
} |
666
|
|
|
|
667
|
|
|
exit($status); |
|
|
|
|
668
|
|
|
} |
669
|
|
|
|
670
|
|
|
/** |
671
|
|
|
* Configures [[Yii::$container]] with the $config. |
672
|
|
|
* |
673
|
|
|
* @param array $config values given in terms of name-value pairs |
674
|
|
|
* @since 2.0.11 |
675
|
|
|
*/ |
676
|
9 |
|
public function setContainer($config) |
677
|
|
|
{ |
678
|
9 |
|
Yii::configure(Yii::$container, $config); |
679
|
|
|
} |
680
|
|
|
} |
681
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.