-
Notifications
You must be signed in to change notification settings - Fork 39
Added a formatter that shows the full header and body of the HTTP message #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
namespace Http\Message\Formatter; | ||
|
||
use Http\Message\Formatter; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
/** | ||
* A formatter that prints the complete HTTP message. | ||
* | ||
* @author Tobias Nyholm <[email protected]> | ||
*/ | ||
class FullHttpMessageFormatter implements Formatter | ||
{ | ||
/** | ||
* The maximum length of the body. | ||
* | ||
* @var int | ||
*/ | ||
private $maxBodyLendth; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/Lendth/Length/ |
||
|
||
/** | ||
* @param int $maxBodyLendth number of chars. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Description seems to be redundant. IMO the property description is enough. |
||
*/ | ||
public function __construct($maxBodyLendth = 1000) | ||
{ | ||
$this->maxBodyLendth = $maxBodyLendth; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function formatRequest(RequestInterface $request) | ||
{ | ||
$message = sprintf( | ||
"%s %s HTTP/%s\n", | ||
$request->getMethod(), | ||
$request->getRequestTarget(), | ||
$request->getProtocolVersion() | ||
); | ||
|
||
foreach ($request->getHeaders() as $name => $values) { | ||
$message .= $name.': '.implode(', ', $values)."\n"; | ||
} | ||
|
||
$message .= "\n".mb_substr($request->getBody()->__toString(), 0, $this->maxBodyLendth); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wonder if string casting or calling toString is better. Anyone knows any recommendation for that. IMO it is a little bit weird that __toString was included in PSR-7 interfaces. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the difference? Sure one may think one looks better than the other. But is one faster then the other? String casting of an object calls __toString() as far as I know. I like calling __toString but Im happy to change if the other way is better or more coherent with the rest of our code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, they are the same. I am just looking for a concept we should follow. |
||
|
||
return $message; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function formatResponse(ResponseInterface $response) | ||
{ | ||
$message = sprintf( | ||
"HTTP/%s %s %s\n", | ||
$response->getProtocolVersion(), | ||
$response->getStatusCode(), | ||
$response->getReasonPhrase() | ||
); | ||
|
||
foreach ($response->getHeaders() as $name => $values) { | ||
$message .= $name.': '.implode(', ', $values)."\n"; | ||
} | ||
|
||
$message .= "\n".mb_substr($response->getBody()->__toString(), 0, $this->maxBodyLendth); | ||
|
||
return $message; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please keep alphabetic order in the changelog.