1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
namespace Behapi\Debug\Introspection\Response\VarDumper; |
3
|
|
|
|
4
|
|
|
use Psr\Http\Message\MessageInterface; |
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
6
|
|
|
|
7
|
|
|
use Symfony\Component\VarDumper\VarDumper; |
8
|
|
|
|
9
|
|
|
use Behapi\Debug\Introspection\Adapter; |
10
|
|
|
use Behapi\Debug\Introspection\UnsupportedMessage; |
11
|
|
|
|
12
|
|
|
final class JsonAdapter implements Adapter |
13
|
|
|
{ |
14
|
|
|
public function introspect(MessageInterface $message, array $headers): void |
15
|
|
|
{ |
16
|
|
|
if (!$this->supports($message)) { |
17
|
|
|
throw new UnsupportedMessage($message, ResponseInterface::class); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
assert($message instanceof ResponseInterface); |
21
|
|
|
|
22
|
|
|
// mandatory, clearing the line |
23
|
|
|
// todo : check how to clear without this echo... |
24
|
|
|
echo "\n"; |
25
|
|
|
|
26
|
|
|
$dump = [ |
27
|
|
|
'Response Status' => "{$message->getStatusCode()} {$message->getReasonPhrase()}", |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
foreach ($headers as $header) { |
31
|
|
|
$dump["Response {$header}"] = $message->getHeaderLine($header); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$body = (string) $message->getBody(); |
35
|
|
|
|
36
|
|
|
if (!empty($body)) { |
37
|
|
|
$dump['Response Body'] = json_decode($body); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
VarDumper::dump($dump); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function supports(MessageInterface $message): bool |
44
|
|
|
{ |
45
|
|
|
if (!class_exists(VarDumper::class)) { |
46
|
|
|
return false; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (!$message instanceof ResponseInterface) { |
50
|
|
|
return false; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
[$contentType,] = explode(';', $message->getHeaderLine('Content-Type'), 2); |
54
|
|
|
|
55
|
|
|
return 'application/json' === $contentType; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|