Completed
Push — bash-completion ( 164352...1454d3 )
by Carsten
86:55 queued 83:59
created

Application   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 45.9%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 6
dl 0
loc 174
ccs 28
cts 61
cp 0.459
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A bootstrap() 0 8 1
C handleRequest() 0 39 8
A getHomeUrl() 0 12 3
A setHomeUrl() 0 4 1
A getRequest() 0 4 1
A getResponse() 0 4 1
A getSession() 0 4 1
A getUser() 0 4 1
A coreComponents() 0 10 1
A getErrorHandler() 0 4 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\web;
9
10
use Yii;
11
use yii\helpers\Url;
12
use yii\base\InvalidRouteException;
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 ErrorHandler $errorHandler The error handler application component. This property is read-only.
20
 * @property string $homeUrl The homepage URL.
21
 * @property Request $request The request component. This property is read-only.
22
 * @property Response $response The response component. This property is read-only.
23
 * @property Session $session The session component. This property is read-only.
24
 * @property User $user The user component. This property is read-only.
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 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 144
    protected function bootstrap()
64
    {
65 144
        $request = $this->getRequest();
66 144
        Yii::setAlias('@webroot', dirname($request->getScriptFile()));
67 144
        Yii::setAlias('@web', $request->getBaseUrl());
68
69 144
        parent::bootstrap();
70 144
    }
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
                return $this->getResponse()->redirect(Url::to($url, $e->scheme), $e->statusCode);
93
            }
94
        } else {
95
            $route = $this->catchAll[0];
96
            $params = $this->catchAll;
97
            unset($params[0]);
98
        }
99
        try {
100
            Yii::trace("Route requested: '$route'", __METHOD__);
101
            $this->requestedRoute = $route;
102
            $result = $this->runAction($route, $params);
103
            if ($result instanceof Response) {
104
                return $result;
105
            } else {
106
                $response = $this->getResponse();
107
                if ($result !== null) {
108
                    $response->data = $result;
109
                }
110
111
                return $response;
112
            }
113
        } catch (InvalidRouteException $e) {
114
            throw new NotFoundHttpException(Yii::t('yii', 'Page not found.'), $e->getCode(), $e);
115
        }
116
    }
117
118
    private $_homeUrl;
119
120
    /**
121
     * @return string the homepage URL
122
     */
123 3
    public function getHomeUrl()
124
    {
125 3
        if ($this->_homeUrl === null) {
126 3
            if ($this->getUrlManager()->showScriptName) {
127 3
                return $this->getRequest()->getScriptUrl();
128
            } else {
129
                return $this->getRequest()->getBaseUrl() . '/';
130
            }
131
        } else {
132
            return $this->_homeUrl;
133
        }
134
    }
135
136
    /**
137
     * @param string $value the homepage URL
138
     */
139
    public function setHomeUrl($value)
140
    {
141
        $this->_homeUrl = $value;
142
    }
143
144
    /**
145
     * Returns the error handler component.
146
     * @return ErrorHandler the error handler application component.
147
     */
148 3
    public function getErrorHandler()
149
    {
150 3
        return $this->get('errorHandler');
151
    }
152
153
    /**
154
     * Returns the request component.
155
     * @return Request the request component.
156
     */
157 144
    public function getRequest()
158
    {
159 144
        return $this->get('request');
160
    }
161
162
    /**
163
     * Returns the response component.
164
     * @return Response the response component.
165
     */
166 68
    public function getResponse()
167
    {
168 68
        return $this->get('response');
169
    }
170
171
    /**
172
     * Returns the session component.
173
     * @return Session the session component.
174
     */
175 26
    public function getSession()
176
    {
177 26
        return $this->get('session');
178
    }
179
180
    /**
181
     * Returns the user component.
182
     * @return User the user component.
183
     */
184 24
    public function getUser()
185
    {
186 24
        return $this->get('user');
187
    }
188
189
    /**
190
     * @inheritdoc
191
     */
192 144
    public function coreComponents()
193
    {
194 144
        return array_merge(parent::coreComponents(), [
195 144
            'request' => ['class' => 'yii\web\Request'],
196 144
            'response' => ['class' => 'yii\web\Response'],
197 144
            'session' => ['class' => 'yii\web\Session'],
198 144
            'user' => ['class' => 'yii\web\User'],
199 144
            'errorHandler' => ['class' => 'yii\web\ErrorHandler'],
200 144
        ]);
201
    }
202
}
203