1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\Message\Formatter; |
4
|
|
|
|
5
|
|
|
use Http\Message\Formatter; |
6
|
|
|
use Psr\Http\Message\MessageInterface; |
7
|
|
|
use Psr\Http\Message\RequestInterface; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* A formatter that prints the complete HTTP message. |
12
|
|
|
* |
13
|
|
|
* @author Tobias Nyholm <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class FullHttpMessageFormatter implements Formatter |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* The maximum length of the body. |
19
|
|
|
* |
20
|
|
|
* @var int|null |
21
|
|
|
*/ |
22
|
|
|
private $maxBodyLength; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param int|null $maxBodyLength |
26
|
|
|
*/ |
27
|
10 |
|
public function __construct($maxBodyLength = 1000) |
28
|
|
|
{ |
29
|
10 |
|
$this->maxBodyLength = $maxBodyLength; |
30
|
10 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
4 |
View Code Duplication |
public function formatRequest(RequestInterface $request) |
|
|
|
|
36
|
|
|
{ |
37
|
4 |
|
$message = sprintf( |
38
|
4 |
|
"%s %s HTTP/%s\n", |
39
|
4 |
|
$request->getMethod(), |
40
|
4 |
|
$request->getRequestTarget(), |
41
|
4 |
|
$request->getProtocolVersion() |
42
|
4 |
|
); |
43
|
|
|
|
44
|
4 |
|
foreach ($request->getHeaders() as $name => $values) { |
45
|
4 |
|
$message .= $name.': '.implode(', ', $values)."\n"; |
46
|
4 |
|
} |
47
|
|
|
|
48
|
4 |
|
return $this->addBody($request, $message); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
4 |
View Code Duplication |
public function formatResponse(ResponseInterface $response) |
|
|
|
|
55
|
|
|
{ |
56
|
4 |
|
$message = sprintf( |
57
|
4 |
|
"HTTP/%s %s %s\n", |
58
|
4 |
|
$response->getProtocolVersion(), |
59
|
4 |
|
$response->getStatusCode(), |
60
|
4 |
|
$response->getReasonPhrase() |
61
|
4 |
|
); |
62
|
|
|
|
63
|
4 |
|
foreach ($response->getHeaders() as $name => $values) { |
64
|
4 |
|
$message .= $name.': '.implode(', ', $values)."\n"; |
65
|
4 |
|
} |
66
|
|
|
|
67
|
4 |
|
return $this->addBody($response, $message); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Add the message body if the stream is seekable. |
72
|
|
|
* |
73
|
|
|
* @param MessageInterface $request |
74
|
|
|
* @param string $message |
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
8 |
|
private function addBody(MessageInterface $request, $message) |
79
|
|
|
{ |
80
|
8 |
|
$stream = $request->getBody(); |
81
|
8 |
|
if (!$stream->isSeekable() || 0 === $this->maxBodyLength) { |
82
|
|
|
// Do not read the stream |
83
|
4 |
|
return $message."\n"; |
84
|
|
|
} |
85
|
|
|
|
86
|
4 |
|
if (null === $this->maxBodyLength) { |
87
|
2 |
|
$message .= "\n".$stream->__toString(); |
88
|
2 |
|
} else { |
89
|
2 |
|
$message .= "\n".mb_substr($stream->__toString(), 0, $this->maxBodyLength); |
90
|
|
|
} |
91
|
|
|
|
92
|
4 |
|
$stream->rewind(); |
93
|
|
|
|
94
|
4 |
|
return $message; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.