forked from buggregator/laravel-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SendRequestDebugToConsole.php
52 lines (45 loc) · 1.89 KB
/
SendRequestDebugToConsole.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
declare(strict_types=1);
namespace App\Listeners\Request;
use Illuminate\Http\Request;
use Spiral\RoadRunnerLaravel\Events\Contracts\WithHttpRequest;
use Spiral\RoadRunnerLaravel\Events\Contracts\WithHttpResponse;
use Spiral\RoadRunnerLaravel\Listeners\ListenerInterface;
use function Termwind\render;
class SendRequestDebugToConsole implements ListenerInterface
{
public function handle($event): void
{
if (! config('app.debug')) {
return;
}
if ($event instanceof WithHttpRequest && $event instanceof WithHttpResponse) {
$request = $event->httpRequest();
if ($request instanceof Request) {
$statusCode = $event->httpResponse()->getStatusCode();
render(
(string) view('console.http.request', [
'request' => $request,
'response' => $event->httpResponse(),
'color' => match ($request->method()) {
'GET' => 'magenta',
'POST' => 'blue',
'PUT' => 'yellow',
'DELETE' => 'red',
'HEAD' => 'white',
},
'responseColor' => match (true) {
$statusCode >= 500 => 'red',
$statusCode >= 400 => 'yellow',
$statusCode >= 300 => 'cyan',
$statusCode >= 100 => 'green',
default => 'white',
},
'duration' => number_format(round(microtime(true) - $request->getTimestamp(), 4), 4, '.', ''),
'memory' => number_format($request->getAllocatedMemory() / 1024 / 1204, 2, '.', ''),
])
);
}
}
}
}