Skip to content

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

Merged
merged 3 commits into from
Jul 4, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

- #41: Response builder broke header value

### Added
Copy link
Member

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.


- The FullHttpMessageFormatter was added

## 1.2.0 - 2016-03-29

Expand Down
72 changes: 72 additions & 0 deletions src/Formatter/FullHttpMessageFormatter.php
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/Lendth/Length/


/**
* @param int $maxBodyLendth number of chars.
Copy link
Member

Choose a reason for hiding this comment

The 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);
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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;
}
}