Passed
Push — master ( 9dbdd9...d5a428 )
by Alexander
04:15
created

framework/base/Application.php (2 issues)

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-read \yii\web\AssetManager $assetManager The asset manager application component. This property
18
 * is read-only.
19
 * @property-read \yii\rbac\ManagerInterface $authManager The auth manager application component. Null is
20
 * returned if auth manager is not configured. This property is read-only.
21
 * @property string $basePath The root directory of the application.
22
 * @property-read \yii\caching\CacheInterface $cache The cache application component. Null if the component is
23
 * not enabled. This property is read-only.
24
 * @property-write array $container Values given in terms of name-value pairs. This property is write-only.
25
 * @property-read \yii\db\Connection $db The database connection. This property is read-only.
26
 * @property-read \yii\web\ErrorHandler|\yii\console\ErrorHandler $errorHandler The error handler application
27
 * component. This property is read-only.
28
 * @property-read \yii\i18n\Formatter $formatter The formatter application component. This property is
29
 * read-only.
30
 * @property-read \yii\i18n\I18N $i18n The internationalization application component. This property is
31
 * read-only.
32
 * @property-read \yii\log\Dispatcher $log The log dispatcher application component. This property is
33
 * read-only.
34
 * @property-read \yii\mail\MailerInterface $mailer The mailer application component. This property is
35
 * read-only.
36
 * @property-read \yii\web\Request|\yii\console\Request $request The request component. This property is
37
 * read-only.
38
 * @property-read \yii\web\Response|\yii\console\Response $response The response component. This property is
39
 * read-only.
40
 * @property string $runtimePath The directory that stores runtime files. Defaults to the "runtime"
41
 * subdirectory under [[basePath]].
42
 * @property-read \yii\base\Security $security The security application component. This property is read-only.
43
 * @property string $timeZone The time zone used by this application.
44
 * @property-read string $uniqueId The unique ID of the module. This property is read-only.
45
 * @property-read \yii\web\UrlManager $urlManager The URL manager for this application. This property is
46
 * read-only.
47
 * @property string $vendorPath The directory that stores vendor files. Defaults to "vendor" directory under
48
 * [[basePath]].
49
 * @property-read View|\yii\web\View $view The view application component that is used to render various view
50
 * files. This property is read-only.
51
 *
52
 * @author Qiang Xue <[email protected]>
53
 * @since 2.0
54
 */
55
abstract class Application extends Module
56
{
57
    /**
58
     * @event Event an event raised before the application starts to handle a request.
59
     */
60
    const EVENT_BEFORE_REQUEST = 'beforeRequest';
61
    /**
62
     * @event Event an event raised after the application successfully handles a request (before the response is sent out).
63
     */
64
    const EVENT_AFTER_REQUEST = 'afterRequest';
65
    /**
66
     * Application state used by [[state]]: application just started.
67
     */
68
    const STATE_BEGIN = 0;
69
    /**
70
     * Application state used by [[state]]: application is initializing.
71
     */
72
    const STATE_INIT = 1;
73
    /**
74
     * Application state used by [[state]]: application is triggering [[EVENT_BEFORE_REQUEST]].
75
     */
76
    const STATE_BEFORE_REQUEST = 2;
77
    /**
78
     * Application state used by [[state]]: application is handling the request.
79
     */
80
    const STATE_HANDLING_REQUEST = 3;
81
    /**
82
     * Application state used by [[state]]: application is triggering [[EVENT_AFTER_REQUEST]]..
83
     */
84
    const STATE_AFTER_REQUEST = 4;
85
    /**
86
     * Application state used by [[state]]: application is about to send response.
87
     */
88
    const STATE_SENDING_RESPONSE = 5;
89
    /**
90
     * Application state used by [[state]]: application has ended.
91
     */
92
    const STATE_END = 6;
93
94
    /**
95
     * @var string the namespace that controller classes are located in.
96
     * This namespace will be used to load controller classes by prepending it to the controller class name.
97
     * The default namespace is `app\controllers`.
98
     *
99
     * Please refer to the [guide about class autoloading](guide:concept-autoloading.md) for more details.
100
     */
101
    public $controllerNamespace = 'app\\controllers';
102
    /**
103
     * @var string the application name.
104
     */
105
    public $name = 'My Application';
106
    /**
107
     * @var string the charset currently used for the application.
108
     */
109
    public $charset = 'UTF-8';
110
    /**
111
     * @var string the language that is meant to be used for end users. It is recommended that you
112
     * use [IETF language tags](http://en.wikipedia.org/wiki/IETF_language_tag). For example, `en` stands
113
     * for English, while `en-US` stands for English (United States).
114
     * @see sourceLanguage
115
     */
116
    public $language = 'en-US';
117
    /**
118
     * @var string the language that the application is written in. This mainly refers to
119
     * the language that the messages and view files are written in.
120
     * @see language
121
     */
122
    public $sourceLanguage = 'en-US';
123
    /**
124
     * @var Controller the currently active controller instance
125
     */
126
    public $controller;
127
    /**
128
     * @var string|bool the layout that should be applied for views in this application. Defaults to 'main'.
129
     * If this is false, layout will be disabled.
130
     */
131
    public $layout = 'main';
132
    /**
133
     * @var string the requested route
134
     */
135
    public $requestedRoute;
136
    /**
137
     * @var Action the requested Action. If null, it means the request cannot be resolved into an action.
138
     */
139
    public $requestedAction;
140
    /**
141
     * @var array the parameters supplied to the requested action.
142
     */
143
    public $requestedParams;
144
    /**
145
     * @var array list of installed Yii extensions. Each array element represents a single extension
146
     * with the following structure:
147
     *
148
     * ```php
149
     * [
150
     *     'name' => 'extension name',
151
     *     'version' => 'version number',
152
     *     'bootstrap' => 'BootstrapClassName',  // optional, may also be a configuration array
153
     *     'alias' => [
154
     *         '@alias1' => 'to/path1',
155
     *         '@alias2' => 'to/path2',
156
     *     ],
157
     * ]
158
     * ```
159
     *
160
     * The "bootstrap" class listed above will be instantiated during the application
161
     * [[bootstrap()|bootstrapping process]]. If the class implements [[BootstrapInterface]],
162
     * its [[BootstrapInterface::bootstrap()|bootstrap()]] method will be also be called.
163
     *
164
     * If not set explicitly in the application config, this property will be populated with the contents of
165
     * `@vendor/yiisoft/extensions.php`.
166
     */
167
    public $extensions;
168
    /**
169
     * @var array list of components that should be run during the application [[bootstrap()|bootstrapping process]].
170
     *
171
     * Each component may be specified in one of the following formats:
172
     *
173
     * - an application component ID as specified via [[components]].
174
     * - a module ID as specified via [[modules]].
175
     * - a class name.
176
     * - a configuration array.
177
     * - a Closure
178
     *
179
     * During the bootstrapping process, each component will be instantiated. If the component class
180
     * implements [[BootstrapInterface]], its [[BootstrapInterface::bootstrap()|bootstrap()]] method
181
     * will be also be called.
182
     */
183
    public $bootstrap = [];
184
    /**
185
     * @var int the current application state during a request handling life cycle.
186
     * This property is managed by the application. Do not modify this property.
187
     */
188
    public $state;
189
    /**
190
     * @var array list of loaded modules indexed by their class names.
191
     */
192
    public $loadedModules = [];
193
194
195
    /**
196
     * Constructor.
197
     * @param array $config name-value pairs that will be used to initialize the object properties.
198
     * Note that the configuration must contain both [[id]] and [[basePath]].
199
     * @throws InvalidConfigException if either [[id]] or [[basePath]] configuration is missing.
200
     */
201 5341
    public function __construct($config = [])
202
    {
203 5341
        Yii::$app = $this;
204 5341
        static::setInstance($this);
205
206 5341
        $this->state = self::STATE_BEGIN;
207
208 5341
        $this->preInit($config);
209
210 5341
        $this->registerErrorHandler($config);
211
212 5341
        Component::__construct($config);
213 5341
    }
214
215
    /**
216
     * Pre-initializes the application.
217
     * This method is called at the beginning of the application constructor.
218
     * It initializes several important application properties.
219
     * If you override this method, please make sure you call the parent implementation.
220
     * @param array $config the application configuration
221
     * @throws InvalidConfigException if either [[id]] or [[basePath]] configuration is missing.
222
     */
223 5341
    public function preInit(&$config)
224
    {
225 5341
        if (!isset($config['id'])) {
226
            throw new InvalidConfigException('The "id" configuration for the Application is required.');
227
        }
228 5341
        if (isset($config['basePath'])) {
229 5341
            $this->setBasePath($config['basePath']);
230 5341
            unset($config['basePath']);
231
        } else {
232
            throw new InvalidConfigException('The "basePath" configuration for the Application is required.');
233
        }
234
235 5341
        if (isset($config['vendorPath'])) {
236 5334
            $this->setVendorPath($config['vendorPath']);
237 5334
            unset($config['vendorPath']);
238
        } else {
239
            // set "@vendor"
240 23
            $this->getVendorPath();
241
        }
242 5341
        if (isset($config['runtimePath'])) {
243
            $this->setRuntimePath($config['runtimePath']);
244
            unset($config['runtimePath']);
245
        } else {
246
            // set "@runtime"
247 5341
            $this->getRuntimePath();
248
        }
249
250 5341
        if (isset($config['timeZone'])) {
251 459
            $this->setTimeZone($config['timeZone']);
252 459
            unset($config['timeZone']);
253 4888
        } elseif (!ini_get('date.timezone')) {
254
            $this->setTimeZone('UTC');
255
        }
256
257 5341
        if (isset($config['container'])) {
258 9
            $this->setContainer($config['container']);
259
260 9
            unset($config['container']);
261
        }
262
263
        // merge core components with custom components
264 5341
        foreach ($this->coreComponents() as $id => $component) {
265 5341
            if (!isset($config['components'][$id])) {
266 5341
                $config['components'][$id] = $component;
267 614
            } elseif (is_array($config['components'][$id]) && !isset($config['components'][$id]['class'])) {
268 5341
                $config['components'][$id]['class'] = $component['class'];
269
            }
270
        }
271 5341
    }
272
273
    /**
274
     * {@inheritdoc}
275
     */
276 5341
    public function init()
277
    {
278 5341
        $this->state = self::STATE_INIT;
279 5341
        $this->bootstrap();
280 5341
    }
281
282
    /**
283
     * Initializes extensions and executes bootstrap components.
284
     * This method is called by [[init()]] after the application has been fully configured.
285
     * If you override this method, make sure you also call the parent implementation.
286
     */
287 5341
    protected function bootstrap()
288
    {
289 5341
        if ($this->extensions === null) {
290 5341
            $file = Yii::getAlias('@vendor/yiisoft/extensions.php');
291 5341
            $this->extensions = is_file($file) ? include $file : [];
292
        }
293 5341
        foreach ($this->extensions as $extension) {
294
            if (!empty($extension['alias'])) {
295
                foreach ($extension['alias'] as $name => $path) {
296
                    Yii::setAlias($name, $path);
297
                }
298
            }
299
            if (isset($extension['bootstrap'])) {
300
                $component = Yii::createObject($extension['bootstrap']);
301
                if ($component instanceof BootstrapInterface) {
302
                    Yii::debug('Bootstrap with ' . get_class($component) . '::bootstrap()', __METHOD__);
303
                    $component->bootstrap($this);
304
                } else {
305
                    Yii::debug('Bootstrap with ' . get_class($component), __METHOD__);
306
                }
307
            }
308
        }
309
310 5341
        foreach ($this->bootstrap as $mixed) {
311 2
            $component = null;
312 2
            if ($mixed instanceof \Closure) {
313 1
                Yii::debug('Bootstrap with Closure', __METHOD__);
314 1
                if (!$component = call_user_func($mixed, $this)) {
315 1
                    continue;
316
                }
317 2
            } elseif (is_string($mixed)) {
318 2
                if ($this->has($mixed)) {
319 2
                    $component = $this->get($mixed);
320 1
                } elseif ($this->hasModule($mixed)) {
321 1
                    $component = $this->getModule($mixed);
322
                } elseif (strpos($mixed, '\\') === false) {
323
                    throw new InvalidConfigException("Unknown bootstrapping component ID: $mixed");
324
                }
325
            }
326
327 2
            if (!isset($component)) {
328
                $component = Yii::createObject($mixed);
329
            }
330
331 2
            if ($component instanceof BootstrapInterface) {
332 1
                Yii::debug('Bootstrap with ' . get_class($component) . '::bootstrap()', __METHOD__);
333 1
                $component->bootstrap($this);
334
            } else {
335 2
                Yii::debug('Bootstrap with ' . get_class($component), __METHOD__);
336
            }
337
        }
338 5341
    }
339
340
    /**
341
     * Registers the errorHandler component as a PHP error handler.
342
     * @param array $config application config
343
     */
344 5341
    protected function registerErrorHandler(&$config)
345
    {
346 5341
        if (YII_ENABLE_ERROR_HANDLER) {
347
            if (!isset($config['components']['errorHandler']['class'])) {
348
                echo "Error: no errorHandler component is configured.\n";
349
                exit(1);
350
            }
351
            $this->set('errorHandler', $config['components']['errorHandler']);
352
            unset($config['components']['errorHandler']);
353
            $this->getErrorHandler()->register();
354
        }
355 5341
    }
356
357
    /**
358
     * Returns an ID that uniquely identifies this module among all modules within the current application.
359
     * Since this is an application instance, it will always return an empty string.
360
     * @return string the unique ID of the module.
361
     */
362 2
    public function getUniqueId()
363
    {
364 2
        return '';
365
    }
366
367
    /**
368
     * Sets the root directory of the application and the @app alias.
369
     * This method can only be invoked at the beginning of the constructor.
370
     * @param string $path the root directory of the application.
371
     * @property string the root directory of the application.
372
     * @throws InvalidArgumentException if the directory does not exist.
373
     */
374 5341
    public function setBasePath($path)
375
    {
376 5341
        parent::setBasePath($path);
377 5341
        Yii::setAlias('@app', $this->getBasePath());
378 5341
    }
379
380
    /**
381
     * Runs the application.
382
     * This is the main entrance of an application.
383
     * @return int the exit status (0 means normal, non-zero values mean abnormal)
384
     */
385
    public function run()
386
    {
387
        try {
388
            $this->state = self::STATE_BEFORE_REQUEST;
389
            $this->trigger(self::EVENT_BEFORE_REQUEST);
390
391
            $this->state = self::STATE_HANDLING_REQUEST;
392
            $response = $this->handleRequest($this->getRequest());
393
394
            $this->state = self::STATE_AFTER_REQUEST;
395
            $this->trigger(self::EVENT_AFTER_REQUEST);
396
397
            $this->state = self::STATE_SENDING_RESPONSE;
398
            $response->send();
399
400
            $this->state = self::STATE_END;
401
402
            return $response->exitStatus;
403
        } catch (ExitException $e) {
404
            $this->end($e->statusCode, isset($response) ? $response : null);
405
            return $e->statusCode;
406
        }
407
    }
408
409
    /**
410
     * Handles the specified request.
411
     *
412
     * This method should return an instance of [[Response]] or its child class
413
     * which represents the handling result of the request.
414
     *
415
     * @param Request $request the request to be handled
416
     * @return Response the resulting response
417
     */
418
    abstract public function handleRequest($request);
419
420
    private $_runtimePath;
421
422
    /**
423
     * Returns the directory that stores runtime files.
424
     * @return string the directory that stores runtime files.
425
     * Defaults to the "runtime" subdirectory under [[basePath]].
426
     */
427 5341
    public function getRuntimePath()
428
    {
429 5341
        if ($this->_runtimePath === null) {
430 5341
            $this->setRuntimePath($this->getBasePath() . DIRECTORY_SEPARATOR . 'runtime');
431
        }
432
433 5341
        return $this->_runtimePath;
434
    }
435
436
    /**
437
     * Sets the directory that stores runtime files.
438
     * @param string $path the directory that stores runtime files.
439
     */
440 5341
    public function setRuntimePath($path)
441
    {
442 5341
        $this->_runtimePath = Yii::getAlias($path);
443 5341
        Yii::setAlias('@runtime', $this->_runtimePath);
0 ignored issues
show
It seems like $this->_runtimePath can also be of type false; however, parameter $path of yii\BaseYii::setAlias() does only seem to accept string, 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 ignore-type  annotation

443
        Yii::setAlias('@runtime', /** @scrutinizer ignore-type */ $this->_runtimePath);
Loading history...
444 5341
    }
445
446
    private $_vendorPath;
447
448
    /**
449
     * Returns the directory that stores vendor files.
450
     * @return string the directory that stores vendor files.
451
     * Defaults to "vendor" directory under [[basePath]].
452
     */
453 23
    public function getVendorPath()
454
    {
455 23
        if ($this->_vendorPath === null) {
456 23
            $this->setVendorPath($this->getBasePath() . DIRECTORY_SEPARATOR . 'vendor');
457
        }
458
459 23
        return $this->_vendorPath;
460
    }
461
462
    /**
463
     * Sets the directory that stores vendor files.
464
     * @param string $path the directory that stores vendor files.
465
     */
466 5341
    public function setVendorPath($path)
467
    {
468 5341
        $this->_vendorPath = Yii::getAlias($path);
469 5341
        Yii::setAlias('@vendor', $this->_vendorPath);
0 ignored issues
show
It seems like $this->_vendorPath can also be of type false; however, parameter $path of yii\BaseYii::setAlias() does only seem to accept string, 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 ignore-type  annotation

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