Completed
Pull Request — 2.1 (#11477)
by Angel
08:46
created

Application::setTimeZone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 4
rs 10
ccs 3
cts 3
cp 1
cc 1
eloc 2
nc 1
nop 1
crap 1
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
 * @property \yii\web\AssetManager $assetManager The asset manager application component. This property is
16
 * read-only.
17
 * @property \yii\rbac\ManagerInterface $authManager The auth manager application component. Null is returned
18
 * if auth manager is not configured. This property is read-only.
19
 * @property string $basePath The root directory of the application.
20
 * @property \yii\caching\Cache $cache The cache application component. Null if the component is not enabled.
21
 * This property is read-only.
22
 * @property \yii\db\Connection $db The database connection. This property is read-only.
23
 * @property \yii\web\ErrorHandler|\yii\console\ErrorHandler $errorHandler The error handler application
24
 * component. This property is read-only.
25
 * @property \yii\i18n\Formatter $formatter The formatter application component. This property is read-only.
26
 * @property \yii\i18n\I18N $i18n The internationalization application component. This property is read-only.
27
 * @property \yii\log\Dispatcher $log The log dispatcher application component. This property is read-only.
28
 * @property \yii\mail\MailerInterface $mailer The mailer application component. This property is read-only.
29
 * @property \yii\web\Request|\yii\console\Request $request The request component. This property is read-only.
30
 * @property \yii\web\Response|\yii\console\Response $response The response component. This property is
31
 * read-only.
32
 * @property string $runtimePath The directory that stores runtime files. Defaults to the "runtime"
33
 * subdirectory under [[basePath]].
34
 * @property \yii\base\Security $security The security application component. This property is read-only.
35
 * @property string $timeZone The time zone used by this application.
36
 * @property string $uniqueId The unique ID of the module. This property is read-only.
37
 * @property \yii\web\UrlManager $urlManager The URL manager for this application. This property is read-only.
38
 * @property string $vendorPath The directory that stores vendor files. Defaults to "vendor" directory under
39
 * [[basePath]].
40
 * @property View|\yii\web\View $view The view application component that is used to render various view
41
 * files. This property is read-only.
42
 *
43
 * @author Qiang Xue <[email protected]>
44
 * @since 2.0
45
 */
46
abstract class Application extends Module
47
{
48
    /**
49
     * @event Event an event raised before the application starts to handle a request.
50
     */
51
    const EVENT_BEFORE_REQUEST = 'beforeRequest';
52
    /**
53
     * @event Event an event raised after the application successfully handles a request (before the response is sent out).
54
     */
55
    const EVENT_AFTER_REQUEST = 'afterRequest';
56
    /**
57
     * Application state used by [[state]]: application just started.
58
     */
59
    const STATE_BEGIN = 0;
60
    /**
61
     * Application state used by [[state]]: application is initializing.
62
     */
63
    const STATE_INIT = 1;
64
    /**
65
     * Application state used by [[state]]: application is triggering [[EVENT_BEFORE_REQUEST]].
66
     */
67
    const STATE_BEFORE_REQUEST = 2;
68
    /**
69
     * Application state used by [[state]]: application is handling the request.
70
     */
71
    const STATE_HANDLING_REQUEST = 3;
72
    /**
73
     * Application state used by [[state]]: application is triggering [[EVENT_AFTER_REQUEST]]..
74
     */
75
    const STATE_AFTER_REQUEST = 4;
76
    /**
77
     * Application state used by [[state]]: application is about to send response.
78
     */
79
    const STATE_SENDING_RESPONSE = 5;
80
    /**
81
     * Application state used by [[state]]: application has ended.
82
     */
83
    const STATE_END = 6;
84
85
    /**
86
     * @var string the namespace that controller classes are located in.
87
     * This namespace will be used to load controller classes by prepending it to the controller class name.
88
     * The default namespace is `app\controllers`.
89
     *
90
     * Please refer to the [guide about class autoloading](guide:concept-autoloading.md) for more details.
91
     */
92
    public $controllerNamespace = 'app\\controllers';
93
    /**
94
     * @var string the application name.
95
     */
96
    public $name = 'My Application';
97
    /**
98
     * @var string the version of this application.
99
     */
100
    public $version = '1.0';
101
    /**
102
     * @var string the charset currently used for the application.
103
     */
104
    public $charset = 'UTF-8';
105
    /**
106
     * @var string the language that is meant to be used for end users. It is recommended that you
107
     * use [IETF language tags](http://en.wikipedia.org/wiki/IETF_language_tag). For example, `en` stands
108
     * for English, while `en-US` stands for English (United States).
109
     * @see sourceLanguage
110
     */
111
    public $language = 'en-US';
112
    /**
113
     * @var string the language that the application is written in. This mainly refers to
114
     * the language that the messages and view files are written in.
115
     * @see language
116
     */
117
    public $sourceLanguage = 'en-US';
118
    /**
119
     * @var Controller the currently active controller instance
120
     */
121
    public $controller;
122
    /**
123
     * @var string|boolean the layout that should be applied for views in this application. Defaults to 'main'.
124
     * If this is false, layout will be disabled.
125
     */
126
    public $layout = 'main';
127
    /**
128
     * @var string the requested route
129
     */
130
    public $requestedRoute;
131
    /**
132
     * @var Action the requested Action. If null, it means the request cannot be resolved into an action.
133
     */
134
    public $requestedAction;
135
    /**
136
     * @var array the parameters supplied to the requested action.
137
     */
138
    public $requestedParams;
139
    /**
140
     * @var array list of installed Yii extensions. Each array element represents a single extension
141
     * with the following structure:
142
     *
143
     * ```php
144
     * [
145
     *     'name' => 'extension name',
146
     *     'version' => 'version number',
147
     *     'bootstrap' => 'BootstrapClassName',  // optional, may also be a configuration array
148
     *     'alias' => [
149
     *         '@alias1' => 'to/path1',
150
     *         '@alias2' => 'to/path2',
151
     *     ],
152
     * ]
153
     * ```
154
     *
155
     * The "bootstrap" class listed above will be instantiated during the application
156
     * [[bootstrap()|bootstrapping process]]. If the class implements [[BootstrapInterface]],
157
     * its [[BootstrapInterface::bootstrap()|bootstrap()]] method will be also be called.
158
     *
159
     * If not set explicitly in the application config, this property will be populated with the contents of
160
     * `@vendor/yiisoft/extensions.php`.
161
     */
162
    public $extensions;
163
    /**
164
     * @var array list of components that should be run during the application [[bootstrap()|bootstrapping process]].
165
     *
166
     * Each component may be specified in one of the following formats:
167
     *
168
     * - an application component ID as specified via [[components]].
169
     * - a module ID as specified via [[modules]].
170
     * - a class name.
171
     * - a configuration array.
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 integer 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 1928
    public function __construct($config = [])
196
    {
197 1928
        Yii::$app = $this;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this of type this<yii\base\Application> is incompatible with the declared type object<yii\console\Appli...ct<yii\web\Application> of property $app.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
198 1928
        static::setInstance($this);
199
200 1928
        $this->state = self::STATE_BEGIN;
201
202 1928
        $this->preInit($config);
203
204 1928
        $this->registerErrorHandler($config);
205
206 1928
        Component::__construct($config);
207 1928
    }
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 1928
    public function preInit(&$config)
218
    {
219 1928
        if (!isset($config['id'])) {
220
            throw new InvalidConfigException('The "id" configuration for the Application is required.');
221
        }
222 1928
        if (isset($config['basePath'])) {
223 1928
            $this->setBasePath($config['basePath']);
224 1928
            unset($config['basePath']);
225 1928
        } else {
226
            throw new InvalidConfigException('The "basePath" configuration for the Application is required.');
227
        }
228
229 1928
        if (isset($config['vendorPath'])) {
230 1928
            $this->setVendorPath($config['vendorPath']);
231 1928
            unset($config['vendorPath']);
232 1928
        } else {
233
            // set "@vendor"
234
            $this->getVendorPath();
235
        }
236 1928
        if (isset($config['runtimePath'])) {
237
            $this->setRuntimePath($config['runtimePath']);
238
            unset($config['runtimePath']);
239
        } else {
240
            // set "@runtime"
241 1928
            $this->getRuntimePath();
242
        }
243
244 1928
        if (isset($config['timeZone'])) {
245 360
            $this->setTimeZone($config['timeZone']);
246 360
            unset($config['timeZone']);
247 1928
        } elseif (!ini_get('date.timezone')) {
248
            $this->setTimeZone('UTC');
249
        }
250
251
        // merge core components with custom components
252 1928
        foreach ($this->coreComponents() as $id => $component) {
253 1928
            if (!isset($config['components'][$id])) {
254 1928
                $config['components'][$id] = $component;
255 1928
            } elseif (is_array($config['components'][$id]) && !isset($config['components'][$id]['class'])) {
256 95
                $config['components'][$id]['class'] = $component['class'];
257 95
            }
258 1928
        }
259 1928
    }
260
261
    /**
262
     * @inheritdoc
263
     */
264 1928
    public function init()
265
    {
266 1928
        $this->state = self::STATE_INIT;
267 1928
        $this->bootstrap();
268 1928
    }
269
270
    /**
271
     * Initializes extensions and executes bootstrap components.
272
     * This method is called by [[init()]] after the application has been fully configured.
273
     * If you override this method, make sure you also call the parent implementation.
274
     */
275 1928
    protected function bootstrap()
276
    {
277 1928
        if ($this->extensions === null) {
278 1928
            $file = Yii::getAlias('@vendor/yiisoft/extensions.php');
279 1928
            $this->extensions = is_file($file) ? include($file) : [];
280 1928
        }
281 1928
        foreach ($this->extensions as $extension) {
282
            if (!empty($extension['alias'])) {
283
                foreach ($extension['alias'] as $name => $path) {
284
                    Yii::setAlias($name, $path);
285
                }
286
            }
287
            if (isset($extension['bootstrap'])) {
288
                $component = Yii::createObject($extension['bootstrap']);
289
                if ($component instanceof BootstrapInterface) {
290
                    Yii::trace('Bootstrap with ' . get_class($component) . '::bootstrap()', __METHOD__);
291
                    $component->bootstrap($this);
292
                } else {
293
                    Yii::trace('Bootstrap with ' . get_class($component), __METHOD__);
294
                }
295
            }
296 1928
        }
297
298 1928
        foreach ($this->bootstrap as $class) {
299
            $component = null;
300
            if (is_string($class)) {
301
                if ($this->has($class)) {
302
                    $component = $this->get($class);
303
                } elseif ($this->hasModule($class)) {
304
                    $component = $this->getModule($class);
305
                } elseif (strpos($class, '\\') === false) {
306
                    throw new InvalidConfigException("Unknown bootstrapping component ID: $class");
307
                }
308
            }
309
            if (!isset($component)) {
310
                $component = Yii::createObject($class);
311
            }
312
313
            if ($component instanceof BootstrapInterface) {
314
                Yii::trace('Bootstrap with ' . get_class($component) . '::bootstrap()', __METHOD__);
315
                $component->bootstrap($this);
316
            } else {
317
                Yii::trace('Bootstrap with ' . get_class($component), __METHOD__);
318
            }
319 1928
        }
320 1928
    }
321
322
    /**
323
     * Registers the errorHandler component as a PHP error handler.
324
     * @param array $config application config
325
     */
326 1928
    protected function registerErrorHandler(&$config)
327
    {
328 1928
        if (YII_ENABLE_ERROR_HANDLER) {
329
            if (!isset($config['components']['errorHandler']['class'])) {
330
                echo "Error: no errorHandler component is configured.\n";
331
                exit(1);
332
            }
333
            $this->set('errorHandler', $config['components']['errorHandler']);
334
            unset($config['components']['errorHandler']);
335
            $this->getErrorHandler()->register();
336
        }
337 1928
    }
338
339
    /**
340
     * Returns an ID that uniquely identifies this module among all modules within the current application.
341
     * Since this is an application instance, it will always return an empty string.
342
     * @return string the unique ID of the module.
343
     */
344
    public function getUniqueId()
345
    {
346
        return '';
347
    }
348
349
    /**
350
     * Sets the root directory of the application and the @app alias.
351
     * This method can only be invoked at the beginning of the constructor.
352
     * @param string $path the root directory of the application.
353
     * @property string the root directory of the application.
354
     * @throws InvalidParamException if the directory does not exist.
355
     */
356 1928
    public function setBasePath($path)
357
    {
358 1928
        parent::setBasePath($path);
359 1928
        Yii::setAlias('@app', $this->getBasePath());
360 1928
    }
361
362
    /**
363
     * Runs the application.
364
     * This is the main entrance of an application.
365
     * @return integer the exit status (0 means normal, non-zero values mean abnormal)
366
     */
367
    public function run()
368
    {
369
        try {
370
            $this->state = self::STATE_BEFORE_REQUEST;
371
            $this->trigger(self::EVENT_BEFORE_REQUEST);
372
373
            $this->state = self::STATE_HANDLING_REQUEST;
374
            $response = $this->handleRequest($this->getRequest());
0 ignored issues
show
Documentation introduced by
$this->getRequest() is of type object|null, but the function expects a object<yii\base\Request>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
375
376
            $this->state = self::STATE_AFTER_REQUEST;
377
            $this->trigger(self::EVENT_AFTER_REQUEST);
378
379
            $this->state = self::STATE_SENDING_RESPONSE;
380
            $response->send();
381
382
            $this->state = self::STATE_END;
383
384
            return $response->exitStatus;
385
        } catch (ExitException $e) {
386
            $this->end($e->statusCode, isset($response) ? $response : null);
387
            return $e->statusCode;
388
        }
389
    }
390
391
    /**
392
     * Handles the specified request.
393
     *
394
     * This method should return an instance of [[Response]] or its child class
395
     * which represents the handling result of the request.
396
     *
397
     * @param Request $request the request to be handled
398
     * @return Response the resulting response
399
     */
400
    abstract public function handleRequest($request);
401
402
    private $_runtimePath;
403
404
    /**
405
     * Returns the directory that stores runtime files.
406
     * @return string the directory that stores runtime files.
407
     * Defaults to the "runtime" subdirectory under [[basePath]].
408
     */
409 1928
    public function getRuntimePath()
410
    {
411 1928
        if ($this->_runtimePath === null) {
412 1928
            $this->setRuntimePath($this->getBasePath() . DIRECTORY_SEPARATOR . 'runtime');
413 1928
        }
414
415 1928
        return $this->_runtimePath;
416
    }
417
418
    /**
419
     * Sets the directory that stores runtime files.
420
     * @param string $path the directory that stores runtime files.
421
     */
422 1928
    public function setRuntimePath($path)
423
    {
424 1928
        $this->_runtimePath = Yii::getAlias($path);
425 1928
        Yii::setAlias('@runtime', $this->_runtimePath);
426 1928
    }
427
428
    private $_vendorPath;
429
430
    /**
431
     * Returns the directory that stores vendor files.
432
     * @return string the directory that stores vendor files.
433
     * Defaults to "vendor" directory under [[basePath]].
434
     */
435
    public function getVendorPath()
436
    {
437
        if ($this->_vendorPath === null) {
438
            $this->setVendorPath($this->getBasePath() . DIRECTORY_SEPARATOR . 'vendor');
439
        }
440
441
        return $this->_vendorPath;
442
    }
443
444
    /**
445
     * Sets the directory that stores vendor files.
446
     * @param string $path the directory that stores vendor files.
447
     */
448 1928
    public function setVendorPath($path)
449
    {
450 1928
        $this->_vendorPath = Yii::getAlias($path);
451 1928
        Yii::setAlias('@vendor', $this->_vendorPath);
452 1928
        Yii::setAlias('@bower', $this->_vendorPath . DIRECTORY_SEPARATOR . 'bower');
453 1928
        Yii::setAlias('@npm', $this->_vendorPath . DIRECTORY_SEPARATOR . 'npm');
454 1928
    }
455
456
    /**
457
     * Returns the time zone used by this application.
458
     * This is a simple wrapper of PHP function date_default_timezone_get().
459
     * If time zone is not configured in php.ini or application config,
460
     * it will be set to UTC by default.
461
     * @return string the time zone used by this application.
462
     * @see http://php.net/manual/en/function.date-default-timezone-get.php
463
     */
464 247
    public function getTimeZone()
465
    {
466 247
        return date_default_timezone_get();
467
    }
468
469
    /**
470
     * Sets the time zone used by this application.
471
     * This is a simple wrapper of PHP function date_default_timezone_set().
472
     * Refer to the [php manual](http://www.php.net/manual/en/timezones.php) for available timezones.
473
     * @param string $value the time zone used by this application.
474
     * @see http://php.net/manual/en/function.date-default-timezone-set.php
475
     */
476 360
    public function setTimeZone($value)
477
    {
478 360
        date_default_timezone_set($value);
479 360
    }
480
481
    /**
482
     * Returns the database connection component.
483
     * @return \yii\db\Connection the database connection.
484
     */
485 22
    public function getDb()
486
    {
487 22
        return $this->get('db');
488
    }
489
490
    /**
491
     * Returns the log dispatcher component.
492
     * @return \yii\log\Dispatcher the log dispatcher application component.
493
     */
494
    public function getLog()
495
    {
496
        return $this->get('log');
497
    }
498
499
    /**
500
     * Returns the error handler component.
501
     * @return \yii\web\ErrorHandler|\yii\console\ErrorHandler the error handler application component.
502
     */
503
    public function getErrorHandler()
504
    {
505
        return $this->get('errorHandler');
506
    }
507
508
    /**
509
     * Returns the cache component.
510
     * @return \yii\caching\Cache the cache application component. Null if the component is not enabled.
511
     */
512
    public function getCache()
513
    {
514
        return $this->get('cache', false);
515
    }
516
517
    /**
518
     * Returns the formatter component.
519
     * @return \yii\i18n\Formatter the formatter application component.
520
     */
521 6
    public function getFormatter()
522
    {
523 6
        return $this->get('formatter');
524
    }
525
526
    /**
527
     * Returns the request component.
528
     * @return \yii\web\Request|\yii\console\Request the request component.
529
     */
530 139
    public function getRequest()
531
    {
532 139
        return $this->get('request');
533
    }
534
535
    /**
536
     * Returns the response component.
537
     * @return \yii\web\Response|\yii\console\Response the response component.
538
     */
539 47
    public function getResponse()
540
    {
541 47
        return $this->get('response');
542
    }
543
544
    /**
545
     * Returns the view object.
546
     * @return View|\yii\web\View the view application component that is used to render various view files.
547
     */
548 12
    public function getView()
549
    {
550 12
        return $this->get('view');
551
    }
552
553
    /**
554
     * Returns the URL manager for this application.
555
     * @return \yii\web\UrlManager the URL manager for this application.
556
     */
557 16
    public function getUrlManager()
558
    {
559 16
        return $this->get('urlManager');
560
    }
561
562
    /**
563
     * Returns the internationalization (i18n) component
564
     * @return \yii\i18n\I18N the internationalization application component.
565
     */
566 530
    public function getI18n()
567
    {
568 530
        return $this->get('i18n');
569
    }
570
571
    /**
572
     * Returns the mailer component.
573
     * @return \yii\mail\MailerInterface the mailer application component.
574
     */
575
    public function getMailer()
576
    {
577
        return $this->get('mailer');
578
    }
579
580
    /**
581
     * Returns the auth manager for this application.
582
     * @return \yii\rbac\ManagerInterface the auth manager application component.
583
     * Null is returned if auth manager is not configured.
584
     */
585
    public function getAuthManager()
586
    {
587
        return $this->get('authManager', false);
588
    }
589
590
    /**
591
     * Returns the asset manager.
592
     * @return \yii\web\AssetManager the asset manager application component.
593
     */
594 2
    public function getAssetManager()
595
    {
596 2
        return $this->get('assetManager');
597
    }
598
599
    /**
600
     * Returns the security component.
601
     * @return \yii\base\Security the security application component.
602
     */
603 22
    public function getSecurity()
604
    {
605 22
        return $this->get('security');
606
    }
607
608
    /**
609
     * Returns the configuration of core application components.
610
     * @see set()
611
     */
612 1928
    public function coreComponents()
613
    {
614
        return [
615 1928
            'security' => ['class' => Security::class],
616 1928
            'formatter' => ['class' => \yii\i18n\Formatter::class],
617 1928
            'i18n' => ['class' => \yii\i18n\I18N::class],
618 1928
            'log' => ['class' => \yii\log\Dispatcher::class],
619 1928
            'mailer' => ['class' => \yii\swiftmailer\Mailer::class],
620 1928
            'assetManager' => ['class' => \yii\web\AssetManager::class],
621 1928
            'urlManager' => ['class' => \yii\web\UrlManager::class],
622 1928
            'view' => ['class' => \yii\web\View::class],
623 1928
        ];
624
    }
625
626
    /**
627
     * Terminates the application.
628
     * This method replaces the `exit()` function by ensuring the application life cycle is completed
629
     * before terminating the application.
630
     * @param integer $status the exit status (value 0 means normal exit while other values mean abnormal exit).
631
     * @param Response $response the response to be sent. If not set, the default application [[response]] component will be used.
632
     * @throws ExitException if the application is in testing mode
633
     */
634
    public function end($status = 0, $response = null)
635
    {
636
        if ($this->state === self::STATE_BEFORE_REQUEST || $this->state === self::STATE_HANDLING_REQUEST) {
637
            $this->state = self::STATE_AFTER_REQUEST;
638
            $this->trigger(self::EVENT_AFTER_REQUEST);
639
        }
640
641
        if ($this->state !== self::STATE_SENDING_RESPONSE && $this->state !== self::STATE_END) {
642
            $this->state = self::STATE_END;
643
            $response = $response ? : $this->getResponse();
644
            $response->send();
645
        }
646
647
        if (YII_ENV_TEST) {
648
            throw new ExitException($status);
649
        } else {
650
            exit($status);
651
        }
652
    }
653
}
654