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