106 lines
2.8 KiB
PHP
106 lines
2.8 KiB
PHP
<?php
|
|
namespace Misuzu;
|
|
|
|
use Whoops\Exception\Formatter;
|
|
use Whoops\Handler\Handler;
|
|
|
|
final class WhoopsReporter extends Handler
|
|
{
|
|
private $reportUrl;
|
|
private $reportSign;
|
|
|
|
public function __construct(?string $reportUrl = null, ?string $reportSign = null)
|
|
{
|
|
$this->setReportInfo($reportUrl, $reportSign);
|
|
}
|
|
|
|
public function setReportInfo(?string $reportUrl = null, ?string $reportSign = null): void
|
|
{
|
|
$this->reportUrl = $reportUrl;
|
|
$this->reportSign = $reportSign;
|
|
}
|
|
|
|
public function handle()
|
|
{
|
|
echo $this->html(
|
|
$this->report()
|
|
? 'Information about this error has been sent to the devs.'
|
|
: 'Report what you were trying to a developer.'
|
|
);
|
|
|
|
return Handler::QUIT;
|
|
}
|
|
|
|
public function contentType(): string
|
|
{
|
|
return 'text/html';
|
|
}
|
|
|
|
public function html(string $text): string
|
|
{
|
|
return <<<HTML
|
|
<!doctype html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>Something broke!</title>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<link href="/css/mio.css" rel="stylesheet">
|
|
</head>
|
|
<body class="main" style="justify-content: center; --site-max-width: 400px">
|
|
<div class="main__wrapper" style="flex: 0 0 auto">
|
|
<div class="container">
|
|
<div class="container__title">Something broke!</div>
|
|
<div class="container__content">
|
|
<p>{$text}</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|
|
HTML;
|
|
}
|
|
|
|
private function report(): bool
|
|
{
|
|
if (!mb_strlen($this->reportUrl) || !($curl = curl_init($this->reportUrl))) {
|
|
return false;
|
|
}
|
|
|
|
$json = json_encode([
|
|
'git' => [
|
|
'branch' => git_branch(),
|
|
'hash' => git_commit_hash(true),
|
|
],
|
|
'misuzu' => [
|
|
'trace_txt' => $this->getException()->getTraceAsString(),
|
|
'directory' => dirname(__DIR__, 1),
|
|
],
|
|
'exception' => Formatter::formatExceptionAsDataArray(
|
|
$this->getInspector(),
|
|
$this
|
|
),
|
|
]);
|
|
|
|
$headers = [
|
|
'Content-Type: application/json;charset=utf-8',
|
|
];
|
|
|
|
if (mb_strlen($this->reportSign)) {
|
|
$headers[] = 'X-Misuzu-Signature: sha256=' . hash_hmac('sha256', $json, $this->reportSign);
|
|
}
|
|
|
|
$setOpts = curl_setopt_array($curl, [
|
|
CURLOPT_TCP_NODELAY => true,
|
|
CURLOPT_POSTFIELDS => $json,
|
|
CURLOPT_HTTPHEADER => $headers,
|
|
CURLOPT_RETURNTRANSFER => false,
|
|
]);
|
|
|
|
if (!$setOpts) {
|
|
return false;
|
|
}
|
|
|
|
return curl_exec($curl) !== false;
|
|
}
|
|
}
|