Passed
Branch 3.1 (41cce1)
by huang
02:56
created

Application   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 93.06%

Importance

Changes 0
Metric Value
dl 0
loc 190
ccs 67
cts 72
cp 0.9306
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 12

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A getName() 0 4 1
A isBooted() 0 4 1
A getPath() 0 4 1
A bootstrap() 0 13 2
A registerServicesProviders() 0 7 2
A handleRequest() 0 9 2
A handleResponse() 0 4 1
A run() 0 10 1
A shutdown() 0 15 2
B handleException() 0 30 4
1
<?php
2
/**
3
 * @author    jan huang <[email protected]>
4
 * @copyright 2016
5
 *
6
 * @see      https://www.github.com/janhuang
7
 * @see      http://www.fast-d.cn/
8
 */
9
10
namespace FastD;
11
12
use Exception;
13
use FastD\Config\Config;
14
use FastD\Container\Container;
15
use FastD\Container\Exceptions\ServiceNotFoundException;
16
use FastD\Container\ServiceProviderInterface;
17
use FastD\Http\HttpException;
18
use FastD\Http\Response;
19
use FastD\Http\ServerRequest;
20
use FastD\ServiceProvider\ConfigServiceProvider;
21
use Psr\Http\Message\ResponseInterface;
22
use Psr\Http\Message\ServerRequestInterface;
23
24
/**
25
 * Class Application.
26
 */
27
class Application extends Container
28
{
29
    /**
30
     * The FastD version.
31
     *
32
     * @const string
33
     */
34
    const VERSION = '3.1.0 (release candidate)';
35
36
    /**
37
     * @var Application
38
     */
39
    public static $app;
40
41
    /**
42
     * @var string
43
     */
44
    protected $path;
45
46
    /**
47
     * @var string
48
     */
49
    protected $name;
50
51
    /**
52
     * @var bool
53
     */
54
    protected $booted = false;
55
56
    /**
57
     * AppKernel constructor.
58
     *
59
     * @param $path
60
     */
61 54
    public function __construct($path)
62
    {
63 54
        $this->path = $path;
64
65 54
        static::$app = $this;
66
67 54
        $this->add('app', $this);
68
69 54
        $this->bootstrap();
70 54
    }
71
72
    /**
73
     * @return string
74
     */
75 46
    public function getName()
76
    {
77 46
        return $this->name;
78
    }
79
80
    /**
81
     * @return bool
82
     */
83 5
    public function isBooted()
84
    {
85 5
        return $this->booted;
86
    }
87
88
    /**
89
     * @return string
90
     */
91 56
    public function getPath()
92
    {
93 56
        return $this->path;
94
    }
95
96 54
    public function bootstrap()
97
    {
98 54
        if (!$this->booted) {
99 54
            $config = load($this->path.'/config/app.php');
100
101 54
            $this->name = $config['name'];
102
103 54
            $this->add('config', new Config($config));
104 54
            $this->registerServicesProviders($config['services']);
105 54
            unset($config);
106 54
            $this->booted = true;
107 54
        }
108 54
    }
109
110
    /**
111
     * @param ServiceProviderInterface[] $services
112
     */
113 54
    protected function registerServicesProviders(array $services)
114
    {
115 54
        $this->register(new ConfigServiceProvider());
116 54
        foreach ($services as $service) {
117 54
            $this->register(new $service());
118 54
        }
119 54
    }
120
121
    /**
122
     * @param ServerRequestInterface $request
123
     *
124
     * @return Response
125
     */
126 16
    public function handleRequest(ServerRequestInterface $request)
127
    {
128 16
        $this->add('request', $request);
129
        try {
130 16
            return $this->get('dispatcher')->dispatch($request);
131 5
        } catch (Exception $exception) {
132 5
            return $this->handleException($request, $exception);
133
        }
134
    }
135
136
    /**
137
     * @param Response $response
138
     */
139 5
    public function handleResponse(Response $response)
140
    {
141 5
        $response->send();
142 5
    }
143
144
    /**
145
     * @param ServerRequestInterface $request
146
     * @param Exception              $e
147
     *
148
     * @return Http\JsonResponse
149
     */
150 10
    public function handleException(ServerRequestInterface $request, Exception $e)
151
    {
152 10
        $statusCode = ($e instanceof HttpException) ? $e->getStatusCode() : $e->getCode();
153
154 10
        if (!array_key_exists($statusCode, Response::$statusTexts)) {
155 5
            $statusCode = 502;
156 5
        }
157
158 10
        $handle = config()->get('exception.handle');
159
160 10
        $response = json($handle($e), $statusCode);
161
162
        try {
163 10
            logger()->error($request->getMethod().' '.$request->getUri()->getPath(), [
164 8
                'ip' => get_local_ip(),
165 8
                'status' => $response->getStatusCode(),
166 8
                'get' => $request->getQueryParams(),
167 8
                'post' => $request->getParsedBody(),
168 8
                'msg' => $e->getMessage(),
169 8
                'code' => $e->getCode(),
170 8
                'file' => $e->getFile(),
171 8
                'line' => $e->getLine(),
172 8
                'trace' => explode("\n", $e->getTraceAsString()),
173 8
            ]);
174
175 8
            return $response;
176 2
        } catch (ServiceNotFoundException $e) {
177 2
            return $response;
178
        }
179
    }
180
181
    /**
182
     * @return int
183
     */
184
    public function run()
185
    {
186
        $request = ServerRequest::createServerRequestFromGlobals();
187
188
        $response = $this->handleRequest($request);
189
190
        $this->handleResponse($response);
191
192
        return $this->shutdown($request, $response);
193
    }
194
195
    /**
196
     * @param ServerRequestInterface $request
197
     * @param ResponseInterface      $response
198
     *
199
     * @return int
200
     */
201 5
    public function shutdown(ServerRequestInterface $request, ResponseInterface $response)
202
    {
203
        try {
204 5
            logger()->info($request->getMethod().' '.$request->getUri()->getPath(), [
205 4
                'ip' => get_local_ip(),
206 4
                'status' => $response->getStatusCode(),
207 4
                'get' => $request->getQueryParams(),
208 4
                'post' => $request->getParsedBody(),
209 4
            ]);
210
211 4
            return 0;
212 1
        } catch (ServiceNotFoundException $e) {
213 1
            return 0;
214
        }
215
    }
216
}
217