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