|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Psr7Middlewares\Middleware; |
|
4
|
|
|
|
|
5
|
|
|
use Psr7Middlewares\Middleware; |
|
6
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
use Whoops\Run; |
|
9
|
|
|
use Whoops\Handler\PrettyPageHandler; |
|
10
|
|
|
use Whoops\Handler\PlainTextHandler; |
|
11
|
|
|
use Whoops\Handler\JsonResponseHandler; |
|
12
|
|
|
use Whoops\Handler\XmlResponseHandler; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Middleware to use whoops as error handler. |
|
16
|
|
|
*/ |
|
17
|
|
|
class Whoops |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var Run|null To handle errors using whoops |
|
21
|
|
|
*/ |
|
22
|
|
|
private $whoops; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var bool Whether catch errors or not |
|
26
|
|
|
*/ |
|
27
|
|
|
private $catchErrors = true; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Set the whoops instance. |
|
31
|
|
|
* |
|
32
|
|
|
* @param Run|null $whoops |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(Run $whoops = null) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->whoops = $whoops; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Whether catch errors or not. |
|
41
|
|
|
* |
|
42
|
|
|
* @param bool $catchErrors |
|
43
|
|
|
* |
|
44
|
|
|
* @return self |
|
45
|
|
|
*/ |
|
46
|
|
|
public function catchErrors($catchErrors = true) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->catchErrors = (bool) $catchErrors; |
|
49
|
|
|
|
|
50
|
|
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Execute the middleware. |
|
55
|
|
|
* |
|
56
|
|
|
* @param ServerRequestInterface $request |
|
57
|
|
|
* @param ResponseInterface $response |
|
58
|
|
|
* @param callable $next |
|
59
|
|
|
* |
|
60
|
|
|
* @return ResponseInterface |
|
61
|
|
|
*/ |
|
62
|
|
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
|
63
|
|
|
{ |
|
64
|
|
|
$whoops = $this->getWhoopsInstance($request); |
|
65
|
|
|
|
|
66
|
|
|
//Catch errors means register whoops globally |
|
67
|
|
|
if ($this->catchErrors) { |
|
68
|
|
|
$whoops->register(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
try { |
|
72
|
|
|
$response = $next($request, $response); |
|
73
|
|
|
} catch (\Exception $exception) { |
|
74
|
|
|
$method = Run::EXCEPTION_HANDLER; |
|
75
|
|
|
|
|
76
|
|
|
$whoops->allowQuit(false); |
|
77
|
|
|
$whoops->writeToOutput(false); |
|
78
|
|
|
$whoops->sendHttpCode(false); |
|
79
|
|
|
|
|
80
|
|
|
$body = Middleware::createStream(); |
|
81
|
|
|
$body->write($whoops->$method($exception)); |
|
82
|
|
|
|
|
83
|
|
|
$response = $response->withStatus(500)->withBody($body); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if ($this->catchErrors) { |
|
87
|
|
|
$whoops->unregister(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $response; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Returns the whoops instance or create one. |
|
95
|
|
|
* |
|
96
|
|
|
* @param ServerRequestInterface $request |
|
97
|
|
|
* |
|
98
|
|
|
* @return Run |
|
99
|
|
|
*/ |
|
100
|
|
|
private function getWhoopsInstance(ServerRequestInterface $request) |
|
101
|
|
|
{ |
|
102
|
|
|
if ($this->whoops) { |
|
103
|
|
|
return $this->whoops; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$whoops = new Run(); |
|
107
|
|
|
|
|
108
|
|
|
if (php_sapi_name() === 'cli') { |
|
109
|
|
|
$whoops->pushHandler(new PlainTextHandler()); |
|
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
return $whoops; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$format = FormatNegotiator::getFormat($request); |
|
115
|
|
|
|
|
116
|
|
|
switch ($format) { |
|
117
|
|
|
case 'json': |
|
118
|
|
|
$whoops->pushHandler(new JsonResponseHandler()); |
|
|
|
|
|
|
119
|
|
|
break; |
|
120
|
|
|
|
|
121
|
|
|
case 'html': |
|
122
|
|
|
$whoops->pushHandler(new PrettyPageHandler()); |
|
|
|
|
|
|
123
|
|
|
break; |
|
124
|
|
|
|
|
125
|
|
|
case 'xml': |
|
126
|
|
|
$whoops->pushHandler(new XmlResponseHandler()); |
|
|
|
|
|
|
127
|
|
|
break; |
|
128
|
|
|
|
|
129
|
|
|
default: |
|
130
|
|
|
if (empty($format)) { |
|
131
|
|
|
$whoops->pushHandler(new PrettyPageHandler()); |
|
|
|
|
|
|
132
|
|
|
} else { |
|
133
|
|
|
$whoops->pushHandler(new PlainTextHandler()); |
|
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
break; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return $whoops; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: