GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( c73de3...97c43c )
by Robert
13:22
created

Application::getView()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
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
 * 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\Cache $cache The cache application component. Null if the component is not enabled.
23
 * 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
     *
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 2401
    public function __construct($config = [])
195
    {
196 2401
        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...
197 2401
        static::setInstance($this);
198
199 2401
        $this->state = self::STATE_BEGIN;
200
201 2401
        $this->preInit($config);
202
203 2401
        $this->registerErrorHandler($config);
204
205 2401
        Component::__construct($config);
206 2401
    }
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 2401
    public function preInit(&$config)
217
    {
218 2401
        if (!isset($config['id'])) {
219
            throw new InvalidConfigException('The "id" configuration for the Application is required.');
220
        }
221 2401
        if (isset($config['basePath'])) {
222 2401
            $this->setBasePath($config['basePath']);
223 2401
            unset($config['basePath']);
224 2401
        } else {
225
            throw new InvalidConfigException('The "basePath" configuration for the Application is required.');
226
        }
227
228 2401
        if (isset($config['vendorPath'])) {
229 2401
            $this->setVendorPath($config['vendorPath']);
230 2401
            unset($config['vendorPath']);
231 2401
        } else {
232
            // set "@vendor"
233 4
            $this->getVendorPath();
234
        }
235 2401
        if (isset($config['runtimePath'])) {
236
            $this->setRuntimePath($config['runtimePath']);
237
            unset($config['runtimePath']);
238
        } else {
239
            // set "@runtime"
240 2401
            $this->getRuntimePath();
241
        }
242
243 2401
        if (isset($config['timeZone'])) {
244 364
            $this->setTimeZone($config['timeZone']);
245 364
            unset($config['timeZone']);
246 2401
        } elseif (!ini_get('date.timezone')) {
247
            $this->setTimeZone('UTC');
248
        }
249
250 2401
        if (isset($config['container'])) {
251 1
            $this->setContainer($config['container']);
252
253 1
            unset($config['container']);
254 1
        }
255
256
        // merge core components with custom components
257 2401
        foreach ($this->coreComponents() as $id => $component) {
258 2401
            if (!isset($config['components'][$id])) {
259 2401
                $config['components'][$id] = $component;
260 2401
            } elseif (is_array($config['components'][$id]) && !isset($config['components'][$id]['class'])) {
261 174
                $config['components'][$id]['class'] = $component['class'];
262 174
            }
263 2401
        }
264 2401
    }
265
266
    /**
267
     * @inheritdoc
268
     */
269 2401
    public function init()
270
    {
271 2401
        $this->state = self::STATE_INIT;
272 2401
        $this->bootstrap();
273 2401
    }
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 2401
    protected function bootstrap()
281
    {
282 2401
        if ($this->extensions === null) {
283 2401
            $file = Yii::getAlias('@vendor/yiisoft/extensions.php');
284 2401
            $this->extensions = is_file($file) ? include($file) : [];
285 2401
        }
286 2401
        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::trace('Bootstrap with ' . get_class($component) . '::bootstrap()', __METHOD__);
296
                    $component->bootstrap($this);
297
                } else {
298
                    Yii::trace('Bootstrap with ' . get_class($component), __METHOD__);
299
                }
300
            }
301 2401
        }
302
303 2401
        foreach ($this->bootstrap as $class) {
304 1
            $component = null;
305 1
            if (is_string($class)) {
306 1
                if ($this->has($class)) {
307 1
                    $component = $this->get($class);
308 1
                } elseif ($this->hasModule($class)) {
309
                    $component = $this->getModule($class);
310
                } elseif (strpos($class, '\\') === false) {
311
                    throw new InvalidConfigException("Unknown bootstrapping component ID: $class");
312
                }
313 1
            }
314 1
            if (!isset($component)) {
315
                $component = Yii::createObject($class);
316
            }
317
318 1
            if ($component instanceof BootstrapInterface) {
319
                Yii::trace('Bootstrap with ' . get_class($component) . '::bootstrap()', __METHOD__);
320
                $component->bootstrap($this);
321
            } else {
322 1
                Yii::trace('Bootstrap with ' . get_class($component), __METHOD__);
323
            }
324 2401
        }
325 2401
    }
326
327
    /**
328
     * Registers the errorHandler component as a PHP error handler.
329
     * @param array $config application config
330
     */
331 2401
    protected function registerErrorHandler(&$config)
332
    {
333 2401
        if (YII_ENABLE_ERROR_HANDLER) {
334
            if (!isset($config['components']['errorHandler']['class'])) {
335
                echo "Error: no errorHandler component is configured.\n";
336
                exit(1);
337
            }
338
            $this->set('errorHandler', $config['components']['errorHandler']);
339
            unset($config['components']['errorHandler']);
340
            $this->getErrorHandler()->register();
341
        }
342 2401
    }
343
344
    /**
345
     * Returns an ID that uniquely identifies this module among all modules within the current application.
346
     * Since this is an application instance, it will always return an empty string.
347
     * @return string the unique ID of the module.
348
     */
349
    public function getUniqueId()
350
    {
351
        return '';
352
    }
353
354
    /**
355
     * Sets the root directory of the application and the @app alias.
356
     * This method can only be invoked at the beginning of the constructor.
357
     * @param string $path the root directory of the application.
358
     * @property string the root directory of the application.
359
     * @throws InvalidParamException if the directory does not exist.
360
     */
361 2401
    public function setBasePath($path)
362
    {
363 2401
        parent::setBasePath($path);
364 2401
        Yii::setAlias('@app', $this->getBasePath());
365 2401
    }
366
367
    /**
368
     * Runs the application.
369
     * This is the main entrance of an application.
370
     * @return int the exit status (0 means normal, non-zero values mean abnormal)
371
     */
372
    public function run()
373
    {
374
        try {
375
376
            $this->state = self::STATE_BEFORE_REQUEST;
377
            $this->trigger(self::EVENT_BEFORE_REQUEST);
378
379
            $this->state = self::STATE_HANDLING_REQUEST;
380
            $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...
381
382
            $this->state = self::STATE_AFTER_REQUEST;
383
            $this->trigger(self::EVENT_AFTER_REQUEST);
384
385
            $this->state = self::STATE_SENDING_RESPONSE;
386
            $response->send();
387
388
            $this->state = self::STATE_END;
389
390
            return $response->exitStatus;
391
392
        } catch (ExitException $e) {
393
394
            $this->end($e->statusCode, isset($response) ? $response : null);
395
            return $e->statusCode;
396
397
        }
398
    }
399
400
    /**
401
     * Handles the specified request.
402
     *
403
     * This method should return an instance of [[Response]] or its child class
404
     * which represents the handling result of the request.
405
     *
406
     * @param Request $request the request to be handled
407
     * @return Response the resulting response
408
     */
409
    abstract public function handleRequest($request);
410
411
    private $_runtimePath;
412
413
    /**
414
     * Returns the directory that stores runtime files.
415
     * @return string the directory that stores runtime files.
416
     * Defaults to the "runtime" subdirectory under [[basePath]].
417
     */
418 2401
    public function getRuntimePath()
419
    {
420 2401
        if ($this->_runtimePath === null) {
421 2401
            $this->setRuntimePath($this->getBasePath() . DIRECTORY_SEPARATOR . 'runtime');
422 2401
        }
423
424 2401
        return $this->_runtimePath;
425
    }
426
427
    /**
428
     * Sets the directory that stores runtime files.
429
     * @param string $path the directory that stores runtime files.
430
     */
431 2401
    public function setRuntimePath($path)
432
    {
433 2401
        $this->_runtimePath = Yii::getAlias($path);
434 2401
        Yii::setAlias('@runtime', $this->_runtimePath);
0 ignored issues
show
Bug introduced by
It seems like $this->_runtimePath can also be of type boolean; however, yii\BaseYii::setAlias() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
435 2401
    }
436
437
    private $_vendorPath;
438
439
    /**
440
     * Returns the directory that stores vendor files.
441
     * @return string the directory that stores vendor files.
442
     * Defaults to "vendor" directory under [[basePath]].
443
     */
444 4
    public function getVendorPath()
445
    {
446 4
        if ($this->_vendorPath === null) {
447 4
            $this->setVendorPath($this->getBasePath() . DIRECTORY_SEPARATOR . 'vendor');
448 4
        }
449
450 4
        return $this->_vendorPath;
451
    }
452
453
    /**
454
     * Sets the directory that stores vendor files.
455
     * @param string $path the directory that stores vendor files.
456
     */
457 2401
    public function setVendorPath($path)
458
    {
459 2401
        $this->_vendorPath = Yii::getAlias($path);
460 2401
        Yii::setAlias('@vendor', $this->_vendorPath);
0 ignored issues
show
Bug introduced by
It seems like $this->_vendorPath can also be of type boolean; however, yii\BaseYii::setAlias() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
461 2401
        Yii::setAlias('@bower', $this->_vendorPath . DIRECTORY_SEPARATOR . 'bower');
462 2401
        Yii::setAlias('@npm', $this->_vendorPath . DIRECTORY_SEPARATOR . 'npm');
463 2401
    }
464
465
    /**
466
     * Returns the time zone used by this application.
467
     * This is a simple wrapper of PHP function date_default_timezone_get().
468
     * If time zone is not configured in php.ini or application config,
469
     * it will be set to UTC by default.
470
     * @return string the time zone used by this application.
471
     * @see http://php.net/manual/en/function.date-default-timezone-get.php
472
     */
473 260
    public function getTimeZone()
474
    {
475 260
        return date_default_timezone_get();
476
    }
477
478
    /**
479
     * Sets the time zone used by this application.
480
     * This is a simple wrapper of PHP function date_default_timezone_set().
481
     * Refer to the [php manual](http://www.php.net/manual/en/timezones.php) for available timezones.
482
     * @param string $value the time zone used by this application.
483
     * @see http://php.net/manual/en/function.date-default-timezone-set.php
484
     */
485 364
    public function setTimeZone($value)
486
    {
487 364
        date_default_timezone_set($value);
488 364
    }
489
490
    /**
491
     * Returns the database connection component.
492
     * @return \yii\db\Connection the database connection.
493
     */
494 40
    public function getDb()
495
    {
496 40
        return $this->get('db');
497
    }
498
499
    /**
500
     * Returns the log dispatcher component.
501
     * @return \yii\log\Dispatcher the log dispatcher application component.
502
     */
503
    public function getLog()
504
    {
505
        return $this->get('log');
506
    }
507
508
    /**
509
     * Returns the error handler component.
510
     * @return \yii\web\ErrorHandler|\yii\console\ErrorHandler the error handler application component.
511
     */
512
    public function getErrorHandler()
513
    {
514
        return $this->get('errorHandler');
515
    }
516
517
    /**
518
     * Returns the cache component.
519
     * @return \yii\caching\Cache the cache application component. Null if the component is not enabled.
520
     */
521
    public function getCache()
522
    {
523
        return $this->get('cache', false);
524
    }
525
526
    /**
527
     * Returns the formatter component.
528
     * @return \yii\i18n\Formatter the formatter application component.
529
     */
530 15
    public function getFormatter()
531
    {
532 15
        return $this->get('formatter');
533
    }
534
535
    /**
536
     * Returns the request component.
537
     * @return \yii\web\Request|\yii\console\Request the request component.
538
     */
539
    public function getRequest()
540
    {
541
        return $this->get('request');
542
    }
543
544
    /**
545
     * Returns the response component.
546
     * @return \yii\web\Response|\yii\console\Response the response component.
547
     */
548
    public function getResponse()
549
    {
550
        return $this->get('response');
551
    }
552
553
    /**
554
     * Returns the view object.
555
     * @return View|\yii\web\View the view application component that is used to render various view files.
556
     */
557 31
    public function getView()
558
    {
559 31
        return $this->get('view');
560
    }
561
562
    /**
563
     * Returns the URL manager for this application.
564
     * @return \yii\web\UrlManager the URL manager for this application.
565
     */
566 29
    public function getUrlManager()
567
    {
568 29
        return $this->get('urlManager');
569
    }
570
571
    /**
572
     * Returns the internationalization (i18n) component
573
     * @return \yii\i18n\I18N the internationalization application component.
574
     */
575 618
    public function getI18n()
576
    {
577 618
        return $this->get('i18n');
578
    }
579
580
    /**
581
     * Returns the mailer component.
582
     * @return \yii\mail\MailerInterface the mailer application component.
583
     */
584
    public function getMailer()
585
    {
586
        return $this->get('mailer');
587
    }
588
589
    /**
590
     * Returns the auth manager for this application.
591
     * @return \yii\rbac\ManagerInterface the auth manager application component.
592
     * Null is returned if auth manager is not configured.
593
     */
594
    public function getAuthManager()
595
    {
596
        return $this->get('authManager', false);
597
    }
598
599
    /**
600
     * Returns the asset manager.
601
     * @return \yii\web\AssetManager the asset manager application component.
602
     */
603 3
    public function getAssetManager()
604
    {
605 3
        return $this->get('assetManager');
606
    }
607
608
    /**
609
     * Returns the security component.
610
     * @return \yii\base\Security the security application component.
611
     */
612 30
    public function getSecurity()
613
    {
614 30
        return $this->get('security');
615
    }
616
617
    /**
618
     * Returns the configuration of core application components.
619
     * @see set()
620
     */
621 2401
    public function coreComponents()
622
    {
623
        return [
624 2401
            'log' => ['class' => 'yii\log\Dispatcher'],
625 2401
            'view' => ['class' => 'yii\web\View'],
626 2401
            'formatter' => ['class' => 'yii\i18n\Formatter'],
627 2401
            'i18n' => ['class' => 'yii\i18n\I18N'],
628 2401
            'mailer' => ['class' => 'yii\swiftmailer\Mailer'],
629 2401
            'urlManager' => ['class' => 'yii\web\UrlManager'],
630 2401
            'assetManager' => ['class' => 'yii\web\AssetManager'],
631 2401
            'security' => ['class' => 'yii\base\Security'],
632 2401
        ];
633
    }
634
635
    /**
636
     * Terminates the application.
637
     * This method replaces the `exit()` function by ensuring the application life cycle is completed
638
     * before terminating the application.
639
     * @param int $status the exit status (value 0 means normal exit while other values mean abnormal exit).
640
     * @param Response $response the response to be sent. If not set, the default application [[response]] component will be used.
641
     * @throws ExitException if the application is in testing mode
642
     */
643 3
    public function end($status = 0, $response = null)
644
    {
645 3
        if ($this->state === self::STATE_BEFORE_REQUEST || $this->state === self::STATE_HANDLING_REQUEST) {
646
            $this->state = self::STATE_AFTER_REQUEST;
647
            $this->trigger(self::EVENT_AFTER_REQUEST);
648
        }
649
650 3
        if ($this->state !== self::STATE_SENDING_RESPONSE && $this->state !== self::STATE_END) {
651 3
            $this->state = self::STATE_END;
652 3
            $response = $response ? : $this->getResponse();
653 3
            $response->send();
654 3
        }
655
656 3
        if (YII_ENV_TEST) {
657 3
            throw new ExitException($status);
658
        } else {
659
            exit($status);
660
        }
661
    }
662
663
    /**
664
     * Configures [[Yii::$container]] with the $config
665
     *
666
     * @param array $config values given in terms of name-value pairs
667
     * @since 2.0.11
668
     */
669 1
    public function setContainer($config)
670
    {
671 1
        Yii::configure(Yii::$container, $config);
672 1
    }
673
}
674