Application::handleRequest()   B
last analyzed

Complexity

Conditions 8
Paths 15

Size

Total Lines 38
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
eloc 26
nc 15
nop 1
dl 0
loc 38
ccs 0
cts 24
cp 0
crap 72
rs 8.4444
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link https://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license https://www.yiiframework.com/license/
6
 */
7
8
namespace yii\web;
9
10
use Yii;
11
use yii\base\InvalidRouteException;
12
use yii\helpers\Url;
13
14
/**
15
 * Application is the base class for all web application classes.
16
 *
17
 * For more details and usage information on Application, see the [guide article on applications](guide:structure-applications).
18
 *
19
 * @property-read ErrorHandler $errorHandler The error handler application component.
20
 * @property string $homeUrl The homepage URL.
21
 * @property-read Request $request The request component.
22
 * @property-read Response $response The response component.
23
 * @property-read Session $session The session component.
24
 * @property-read User $user The user component.
25
 *
26
 * @author Qiang Xue <[email protected]>
27
 * @since 2.0
28
 */
29
class Application extends \yii\base\Application
30
{
31
    /**
32
     * @var string the default route of this application. Defaults to 'site'.
33
     */
34
    public $defaultRoute = 'site';
35
    /**
36
     * @var array|null the configuration specifying a controller action which should handle
37
     * all user requests. This is mainly used when the application is in maintenance mode
38
     * and needs to handle all incoming requests via a single action.
39
     * The configuration is an array whose first element specifies the route of the action.
40
     * The rest of the array elements (key-value pairs) specify the parameters to be bound
41
     * to the action. For example,
42
     *
43
     * ```php
44
     * [
45
     *     'offline/notice',
46
     *     'param1' => 'value1',
47
     *     'param2' => 'value2',
48
     * ]
49
     * ```
50
     *
51
     * Defaults to null, meaning catch-all is not used.
52
     */
53
    public $catchAll;
54
    /**
55
     * @var Controller the currently active controller instance
56
     */
57
    public $controller;
58
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 474
    protected function bootstrap()
64
    {
65 474
        $request = $this->getRequest();
66 474
        Yii::setAlias('@webroot', dirname($request->getScriptFile()));
67 474
        Yii::setAlias('@web', $request->getBaseUrl());
68
69 474
        parent::bootstrap();
70
    }
71
72
    /**
73
     * Handles the specified request.
74
     * @param Request $request the request to be handled
75
     * @return Response the resulting response
76
     * @throws NotFoundHttpException if the requested route is invalid
77
     */
78
    public function handleRequest($request)
79
    {
80
        if (empty($this->catchAll)) {
81
            try {
82
                list($route, $params) = $request->resolve();
83
            } catch (UrlNormalizerRedirectException $e) {
84
                $url = $e->url;
85
                if (is_array($url)) {
86
                    if (isset($url[0])) {
87
                        // ensure the route is absolute
88
                        $url[0] = '/' . ltrim($url[0], '/');
89
                    }
90
                    $url += $request->getQueryParams();
91
                }
92
93
                return $this->getResponse()->redirect(Url::to($url, $e->scheme), $e->statusCode);
94
            }
95
        } else {
96
            $route = $this->catchAll[0];
97
            $params = $this->catchAll;
98
            unset($params[0]);
99
        }
100
        try {
101
            Yii::debug("Route requested: '$route'", __METHOD__);
102
            $this->requestedRoute = $route;
103
            $result = $this->runAction($route, $params);
104
            if ($result instanceof Response) {
105
                return $result;
106
            }
107
108
            $response = $this->getResponse();
109
            if ($result !== null) {
110
                $response->data = $result;
111
            }
112
113
            return $response;
114
        } catch (InvalidRouteException $e) {
115
            throw new NotFoundHttpException(Yii::t('yii', 'Page not found.'), $e->getCode(), $e);
116
        }
117
    }
118
119
    private $_homeUrl;
120
121
    /**
122
     * @return string the homepage URL
123
     */
124 4
    public function getHomeUrl()
125
    {
126 4
        if ($this->_homeUrl === null) {
127 4
            if ($this->getUrlManager()->showScriptName) {
128 4
                return $this->getRequest()->getScriptUrl();
129
            }
130
131
            return $this->getRequest()->getBaseUrl() . '/';
132
        }
133
134
        return $this->_homeUrl;
135
    }
136
137
    /**
138
     * @param string $value the homepage URL
139
     */
140
    public function setHomeUrl($value)
141
    {
142
        $this->_homeUrl = $value;
143
    }
144
145
    /**
146
     * Returns the error handler component.
147
     * @return ErrorHandler the error handler application component.
148
     */
149 24
    public function getErrorHandler()
150
    {
151 24
        return $this->get('errorHandler');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->get('errorHandler') also could return the type mixed which is incompatible with the documented return type yii\web\ErrorHandler.
Loading history...
152
    }
153
154
    /**
155
     * Returns the request component.
156
     * @return Request the request component.
157
     */
158 474
    public function getRequest()
159
    {
160 474
        return $this->get('request');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->get('request') also could return the type mixed which is incompatible with the documented return type yii\web\Request.
Loading history...
161
    }
162
163
    /**
164
     * Returns the response component.
165
     * @return Response the response component.
166
     */
167 181
    public function getResponse()
168
    {
169 181
        return $this->get('response');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->get('response') also could return the type mixed which is incompatible with the documented return type yii\web\Response.
Loading history...
170
    }
171
172
    /**
173
     * Returns the session component.
174
     * @return Session the session component.
175
     */
176 88
    public function getSession()
177
    {
178 88
        return $this->get('session');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->get('session') also could return the type mixed which is incompatible with the documented return type yii\web\Session.
Loading history...
179
    }
180
181
    /**
182
     * Returns the user component.
183
     * @return User the user component.
184
     */
185 80
    public function getUser()
186
    {
187 80
        return $this->get('user');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->get('user') also could return the type mixed which is incompatible with the documented return type yii\web\User.
Loading history...
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193 474
    public function coreComponents()
194
    {
195 474
        return array_merge(parent::coreComponents(), [
196 474
            'request' => ['class' => 'yii\web\Request'],
197 474
            'response' => ['class' => 'yii\web\Response'],
198 474
            'session' => ['class' => 'yii\web\Session'],
199 474
            'user' => ['class' => 'yii\web\User'],
200 474
            'errorHandler' => ['class' => 'yii\web\ErrorHandler'],
201 474
        ]);
202
    }
203
}
204