Completed
Push — fixes-14366-upgrade-php-72alph... ( 5b72b9 )
by
unknown
04:39
created

Application::run()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 3
eloc 15
nc 5
nop 0
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 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
    public function __construct($config = [])
195
    {
196
        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
        static::setInstance($this);
198
199
        $this->state = self::STATE_BEGIN;
200
201
        $this->preInit($config);
202
203
        $this->registerErrorHandler($config);
204
205
        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
    public function preInit(&$config)
217
    {
218
        if (!isset($config['id'])) {
219
            throw new InvalidConfigException('The "id" configuration for the Application is required.');
220
        }
221
        if (isset($config['basePath'])) {
222
            $this->setBasePath($config['basePath']);
223
            unset($config['basePath']);
224
        } else {
225
            throw new InvalidConfigException('The "basePath" configuration for the Application is required.');
226
        }
227
228
        if (isset($config['vendorPath'])) {
229
            $this->setVendorPath($config['vendorPath']);
230
            unset($config['vendorPath']);
231
        } else {
232
            // set "@vendor"
233
            $this->getVendorPath();
234
        }
235
        if (isset($config['runtimePath'])) {
236
            $this->setRuntimePath($config['runtimePath']);
237
            unset($config['runtimePath']);
238
        } else {
239
            // set "@runtime"
240
            $this->getRuntimePath();
241
        }
242
243
        if (isset($config['timeZone'])) {
244
            $this->setTimeZone($config['timeZone']);
245
            unset($config['timeZone']);
246
        } elseif (!ini_get('date.timezone')) {
247
            $this->setTimeZone('UTC');
248
        }
249
250
        if (isset($config['container'])) {
251
            $this->setContainer($config['container']);
252
253
            unset($config['container']);
254
        }
255
256
        // merge core components with custom components
257
        foreach ($this->coreComponents() as $id => $component) {
258
            if (!isset($config['components'][$id])) {
259
                $config['components'][$id] = $component;
260
            } elseif (is_array($config['components'][$id]) && !isset($config['components'][$id]['class'])) {
261
                $config['components'][$id]['class'] = $component['class'];
262
            }
263
        }
264
    }
265
266
    /**
267
     * @inheritdoc
268
     */
269
    public function init()
270
    {
271
        $this->state = self::STATE_INIT;
272
        $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
    protected function bootstrap()
281
    {
282
        if ($this->extensions === null) {
283
            $file = Yii::getAlias('@vendor/yiisoft/extensions.php');
284
            $this->extensions = is_file($file) ? include $file : [];
285
        }
286
        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
        }
302
303
        foreach ($this->bootstrap as $mixed) {
304
            $component = null;
305
            if($mixed instanceof \Closure){
306
                if(!$component = call_user_func($mixed, $this)){
307
                    continue;
308
                }
309
            }else if (is_string($mixed)) {
310
                if ($this->has($mixed)) {
311
                    $component = $this->get($mixed);
312
                } elseif ($this->hasModule($mixed)) {
313
                    $component = $this->getModule($mixed);
314
                } elseif (strpos($mixed, '\\') === false) {
315
                    throw new InvalidConfigException("Unknown bootstrapping component ID: $mixed");
316
                }
317
            }
318
            if (!isset($component)) {
319
                $component = Yii::createObject($mixed);
320
            }
321
322
            if ($component instanceof BootstrapInterface) {
323
                Yii::trace('Bootstrap with ' . get_class($component) . '::bootstrap()', __METHOD__);
324
                $component->bootstrap($this);
325
            } else {
326
                Yii::trace('Bootstrap with ' . get_class($component), __METHOD__);
327
            }
328
        }
329
    }
330
331
    /**
332
     * Registers the errorHandler component as a PHP error handler.
333
     * @param array $config application config
334
     */
335
    protected function registerErrorHandler(&$config)
336
    {
337
        if (YII_ENABLE_ERROR_HANDLER) {
338
            if (!isset($config['components']['errorHandler']['class'])) {
339
                echo "Error: no errorHandler component is configured.\n";
340
                exit(1);
341
            }
342
            $this->set('errorHandler', $config['components']['errorHandler']);
343
            unset($config['components']['errorHandler']);
344
            $this->getErrorHandler()->register();
345
        }
346
    }
347
348
    /**
349
     * Returns an ID that uniquely identifies this module among all modules within the current application.
350
     * Since this is an application instance, it will always return an empty string.
351
     * @return string the unique ID of the module.
352
     */
353
    public function getUniqueId()
354
    {
355
        return '';
356
    }
357
358
    /**
359
     * Sets the root directory of the application and the @app alias.
360
     * This method can only be invoked at the beginning of the constructor.
361
     * @param string $path the root directory of the application.
362
     * @property string the root directory of the application.
363
     * @throws InvalidParamException if the directory does not exist.
364
     */
365
    public function setBasePath($path)
366
    {
367
        parent::setBasePath($path);
368
        Yii::setAlias('@app', $this->getBasePath());
369
    }
370
371
    /**
372
     * Runs the application.
373
     * This is the main entrance of an application.
374
     * @return int the exit status (0 means normal, non-zero values mean abnormal)
375
     */
376
    public function run()
377
    {
378
        try {
379
            $this->state = self::STATE_BEFORE_REQUEST;
380
            $this->trigger(self::EVENT_BEFORE_REQUEST);
381
382
            $this->state = self::STATE_HANDLING_REQUEST;
383
            $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...
384
385
            $this->state = self::STATE_AFTER_REQUEST;
386
            $this->trigger(self::EVENT_AFTER_REQUEST);
387
388
            $this->state = self::STATE_SENDING_RESPONSE;
389
            $response->send();
390
391
            $this->state = self::STATE_END;
392
393
            return $response->exitStatus;
394
        } catch (ExitException $e) {
395
            $this->end($e->statusCode, isset($response) ? $response : null);
396
            return $e->statusCode;
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
    public function getRuntimePath()
419
    {
420
        if ($this->_runtimePath === null) {
421
            $this->setRuntimePath($this->getBasePath() . DIRECTORY_SEPARATOR . 'runtime');
422
        }
423
424
        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
    public function setRuntimePath($path)
432
    {
433
        $this->_runtimePath = Yii::getAlias($path);
434
        Yii::setAlias('@runtime', $this->_runtimePath);
435
    }
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
    public function getVendorPath()
445
    {
446
        if ($this->_vendorPath === null) {
447
            $this->setVendorPath($this->getBasePath() . DIRECTORY_SEPARATOR . 'vendor');
448
        }
449
450
        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
    public function setVendorPath($path)
458
    {
459
        $this->_vendorPath = Yii::getAlias($path);
460
        Yii::setAlias('@vendor', $this->_vendorPath);
461
        Yii::setAlias('@bower', $this->_vendorPath . DIRECTORY_SEPARATOR . 'bower');
462
        Yii::setAlias('@npm', $this->_vendorPath . DIRECTORY_SEPARATOR . 'npm');
463
    }
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
    public function getTimeZone()
474
    {
475
        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
    public function setTimeZone($value)
486
    {
487
        date_default_timezone_set($value);
488
    }
489
490
    /**
491
     * Returns the database connection component.
492
     * @return \yii\db\Connection the database connection.
493
     */
494
    public function getDb()
495
    {
496
        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\CacheInterface 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
    public function getFormatter()
531
    {
532
        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
    public function getView()
558
    {
559
        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
    public function getUrlManager()
567
    {
568
        return $this->get('urlManager');
569
    }
570
571
    /**
572
     * Returns the internationalization (i18n) component
573
     * @return \yii\i18n\I18N the internationalization application component.
574
     */
575
    public function getI18n()
576
    {
577
        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
    public function getAssetManager()
604
    {
605
        return $this->get('assetManager');
606
    }
607
608
    /**
609
     * Returns the security component.
610
     * @return \yii\base\Security the security application component.
611
     */
612
    public function getSecurity()
613
    {
614
        return $this->get('security');
615
    }
616
617
    /**
618
     * Returns the configuration of core application components.
619
     * @see set()
620
     */
621
    public function coreComponents()
622
    {
623
        return [
624
            'log' => ['class' => 'yii\log\Dispatcher'],
625
            'view' => ['class' => 'yii\web\View'],
626
            'formatter' => ['class' => 'yii\i18n\Formatter'],
627
            'i18n' => ['class' => 'yii\i18n\I18N'],
628
            'mailer' => ['class' => 'yii\swiftmailer\Mailer'],
629
            'urlManager' => ['class' => 'yii\web\UrlManager'],
630
            'assetManager' => ['class' => 'yii\web\AssetManager'],
631
            'security' => ['class' => 'yii\base\Security'],
632
        ];
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
    public function end($status = 0, $response = null)
644
    {
645
        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
        if ($this->state !== self::STATE_SENDING_RESPONSE && $this->state !== self::STATE_END) {
651
            $this->state = self::STATE_END;
652
            $response = $response ?: $this->getResponse();
653
            $response->send();
654
        }
655
656
        if (YII_ENV_TEST) {
657
            throw new ExitException($status);
658
        }
659
660
        exit($status);
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
    public function setContainer($config)
670
    {
671
        Yii::configure(Yii::$container, $config);
672
    }
673
}
674