|
1
|
|
|
<?php
|
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Http\Kernel;
|
|
4
|
|
|
|
|
5
|
|
|
use Exception;
|
|
6
|
|
|
use Nip\Application;
|
|
7
|
|
|
use Nip\Application\ApplicationInterface;
|
|
8
|
|
|
use Nip\Dispatcher\ActionDispatcherMiddleware;
|
|
9
|
|
|
use Nip\Http\Response\Response;
|
|
10
|
|
|
use Nip\Http\Response\ResponseFactory;
|
|
11
|
|
|
use Nip\Http\ServerMiddleware\Dispatcher;
|
|
12
|
|
|
use Nip\Http\ServerMiddleware\Traits\HasServerMiddleware;
|
|
13
|
|
|
use Nip\Request;
|
|
14
|
|
|
use Nip\Router\Middleware\RouteResolverMiddleware;
|
|
15
|
|
|
use Nip\Router\Router;
|
|
16
|
|
|
use Nip\Session\Middleware\StartSession;
|
|
17
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
18
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
19
|
|
|
use Symfony\Component\Debug\Exception\FatalThrowableError;
|
|
20
|
|
|
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
|
|
21
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
22
|
|
|
use Throwable;
|
|
23
|
|
|
use Whoops\Handler\PrettyPageHandler;
|
|
24
|
|
|
use Whoops\Run as WhoopsRun;
|
|
25
|
|
|
|
|
26
|
|
|
/**
|
|
27
|
|
|
* Class Kernel
|
|
28
|
|
|
* @package Nip\Http\Kernel
|
|
29
|
|
|
*/
|
|
30
|
|
|
class Kernel implements KernelInterface
|
|
31
|
|
|
{
|
|
32
|
|
|
use HasServerMiddleware;
|
|
33
|
|
|
|
|
34
|
|
|
/**
|
|
35
|
|
|
* The application implementation.
|
|
36
|
|
|
*
|
|
37
|
|
|
* @var Application
|
|
38
|
|
|
*/
|
|
39
|
|
|
protected $app;
|
|
40
|
|
|
|
|
41
|
|
|
/**
|
|
42
|
|
|
* The router instance.
|
|
43
|
|
|
*
|
|
44
|
|
|
* @var Router
|
|
45
|
|
|
*/
|
|
46
|
|
|
protected $router;
|
|
47
|
|
|
|
|
48
|
|
|
/**
|
|
49
|
|
|
* The application's route middleware groups.
|
|
50
|
|
|
*
|
|
51
|
|
|
* @var array
|
|
52
|
|
|
*/
|
|
53
|
|
|
protected $middlewareGroups = [];
|
|
54
|
|
|
|
|
55
|
|
|
/**
|
|
56
|
|
|
* The application's route middleware.
|
|
57
|
|
|
*
|
|
58
|
|
|
* @var array
|
|
59
|
|
|
*/
|
|
60
|
|
|
protected $routeMiddleware = [];
|
|
61
|
|
|
|
|
62
|
|
|
/**
|
|
63
|
|
|
* Create a new HTTP kernel instance.
|
|
64
|
|
|
*
|
|
65
|
|
|
* @param ApplicationInterface $app
|
|
66
|
|
|
* @param Router $router
|
|
67
|
|
|
*/
|
|
68
|
|
|
public function __construct(ApplicationInterface $app, Router $router)
|
|
69
|
|
|
{
|
|
70
|
|
|
$this->app = $app;
|
|
|
|
|
|
|
71
|
|
|
$this->router = $router;
|
|
72
|
|
|
|
|
73
|
|
|
$this->pushMiddleware(StartSession::class);
|
|
74
|
|
|
$this->pushMiddleware(RouteResolverMiddleware::class);
|
|
75
|
|
|
$this->pushMiddleware(ActionDispatcherMiddleware::class);
|
|
76
|
|
|
}
|
|
77
|
|
|
|
|
78
|
|
|
/**
|
|
79
|
|
|
* Handle an incoming HTTP request.
|
|
80
|
|
|
*
|
|
81
|
|
|
* @param SymfonyRequest|ServerRequestInterface $request
|
|
82
|
|
|
* @param int $type
|
|
83
|
|
|
* @param bool $catch
|
|
84
|
|
|
* @return ResponseInterface
|
|
85
|
|
|
*/
|
|
86
|
|
|
public function handle(SymfonyRequest $request, $type = self::MASTER_REQUEST, $catch = true)
|
|
87
|
|
|
{
|
|
88
|
|
|
try {
|
|
89
|
|
|
$this->getApplication()->share('request', $request);
|
|
90
|
|
|
return $this->handleRaw($request, $type);
|
|
|
|
|
|
|
91
|
|
|
} catch (Exception $e) {
|
|
92
|
|
|
$this->reportException($e);
|
|
93
|
|
|
$response = $this->renderException($request, $e);
|
|
|
|
|
|
|
94
|
|
|
} catch (Throwable $e) {
|
|
95
|
|
|
$this->reportException($e = new FatalThrowableError($e));
|
|
96
|
|
|
$response = $this->renderException($request, $e);
|
|
|
|
|
|
|
97
|
|
|
}
|
|
98
|
|
|
// event(new Events\RequestHandled($request, $response));
|
|
|
|
|
|
|
99
|
|
|
return $response;
|
|
|
|
|
|
|
100
|
|
|
}
|
|
101
|
|
|
|
|
102
|
|
|
/**
|
|
103
|
|
|
* Get the application instance.
|
|
104
|
|
|
*
|
|
105
|
|
|
* @return Application
|
|
106
|
|
|
*/
|
|
107
|
|
|
public function getApplication()
|
|
108
|
|
|
{
|
|
109
|
|
|
return $this->app;
|
|
110
|
|
|
}
|
|
111
|
|
|
|
|
112
|
|
|
/**
|
|
113
|
|
|
* Handles a request to convert it to a response.
|
|
114
|
|
|
*
|
|
115
|
|
|
* @param SymfonyRequest|ServerRequestInterface $request A Request instance
|
|
116
|
|
|
* @param int $type The type of the request
|
|
117
|
|
|
*
|
|
118
|
|
|
* @return ResponseInterface A Response instance
|
|
119
|
|
|
*
|
|
120
|
|
|
* @throws \LogicException If one of the listener does not behave as expected
|
|
121
|
|
|
* @throws NotFoundHttpException When controller cannot be found
|
|
122
|
|
|
*/
|
|
123
|
|
|
protected function handleRaw(ServerRequestInterface $request, $type = self::MASTER_REQUEST)
|
|
|
|
|
|
|
124
|
|
|
{
|
|
125
|
|
|
return (
|
|
126
|
|
|
new Dispatcher($this->middleware, $this->getApplication()->getContainer())
|
|
127
|
|
|
)->dispatch($request);
|
|
128
|
|
|
}
|
|
129
|
|
|
|
|
130
|
|
|
/**
|
|
131
|
|
|
* Report the exception to the exception handler.
|
|
132
|
|
|
*
|
|
133
|
|
|
* @param Exception $e
|
|
134
|
|
|
* @return void
|
|
135
|
|
|
*/
|
|
136
|
|
|
protected function reportException(Exception $e)
|
|
137
|
|
|
{
|
|
138
|
|
|
app('log')->error($e);
|
|
139
|
|
|
}
|
|
140
|
|
|
|
|
141
|
|
|
/**
|
|
142
|
|
|
* @param Request|ServerRequestInterface $request
|
|
143
|
|
|
* @param Exception $e
|
|
144
|
|
|
* @return Response|ResponseInterface
|
|
145
|
|
|
*/
|
|
146
|
|
|
protected function renderException($request, Exception $e)
|
|
147
|
|
|
{
|
|
148
|
|
|
if (config('app.debug') === false) {
|
|
149
|
|
|
$request->setControllerName('error')->setActionName('index');
|
|
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
return (
|
|
152
|
|
|
new Dispatcher(
|
|
153
|
|
|
[
|
|
154
|
|
|
\Nip\Dispatcher\ActionDispatcherMiddleware::class
|
|
155
|
|
|
],
|
|
156
|
|
|
$this->getApplication()->getContainer()
|
|
157
|
|
|
)
|
|
158
|
|
|
)->dispatch($request);
|
|
159
|
|
|
} else {
|
|
160
|
|
|
$whoops = new WhoopsRun;
|
|
161
|
|
|
$whoops->allowQuit(false);
|
|
162
|
|
|
$whoops->writeToOutput(false);
|
|
163
|
|
|
$whoops->pushHandler(new PrettyPageHandler());
|
|
|
|
|
|
|
164
|
|
|
|
|
165
|
|
|
return ResponseFactory::make($whoops->handleException($e));
|
|
|
|
|
|
|
166
|
|
|
}
|
|
167
|
|
|
}
|
|
168
|
|
|
|
|
169
|
|
|
/**
|
|
170
|
|
|
* @param Request $request
|
|
171
|
|
|
* @param Response $response
|
|
172
|
|
|
*/
|
|
173
|
|
|
public function terminate(Request $request, Response $response)
|
|
174
|
|
|
{
|
|
175
|
|
|
$this->terminateMiddleware($request, $response);
|
|
176
|
|
|
$this->getApplication()->terminate();
|
|
177
|
|
|
}
|
|
178
|
|
|
|
|
179
|
|
|
// /**
|
|
|
|
|
|
|
180
|
|
|
// * @param Request $request
|
|
181
|
|
|
// * @return bool
|
|
182
|
|
|
// */
|
|
183
|
|
|
// protected function isValidRequest($request)
|
|
184
|
|
|
// {
|
|
185
|
|
|
// if ($request->isMalicious()) {
|
|
186
|
|
|
// return false;
|
|
187
|
|
|
// }
|
|
188
|
|
|
//
|
|
189
|
|
|
// return true;
|
|
190
|
|
|
// }
|
|
191
|
|
|
|
|
192
|
|
|
public function postRouting()
|
|
193
|
|
|
{
|
|
194
|
|
|
}
|
|
195
|
|
|
|
|
196
|
|
|
/**
|
|
197
|
|
|
* @param Exception $e
|
|
198
|
|
|
* @param Request|ServerRequestInterface $request
|
|
199
|
|
|
* @return Response
|
|
200
|
|
|
*/
|
|
201
|
|
|
protected function handleException($request, Exception $e)
|
|
202
|
|
|
{
|
|
203
|
|
|
$this->reportException($e);
|
|
204
|
|
|
|
|
205
|
|
|
return $this->renderException($request, $e);
|
|
206
|
|
|
}
|
|
207
|
|
|
}
|
|
208
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.