Add ResponderInterface and add setter for chunk site to Responder
This commit is contained in:
parent
df8e274f26
commit
90b9503c72
|
|
@ -5,9 +5,16 @@ namespace WellRESTed\Routing;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Psr\Http\Message\StreamableInterface;
|
use Psr\Http\Message\StreamableInterface;
|
||||||
|
|
||||||
class Responder
|
class Responder implements ResponderInterface
|
||||||
{
|
{
|
||||||
public function respond(ResponseInterface $response, $chunkSize = 0)
|
private $chunkSize = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Outputs a response.
|
||||||
|
*
|
||||||
|
* @param ResponseInterface $response Response to output
|
||||||
|
*/
|
||||||
|
public function respond(ResponseInterface $response)
|
||||||
{
|
{
|
||||||
// Status Line
|
// Status Line
|
||||||
header($this->getStatusLine($response));
|
header($this->getStatusLine($response));
|
||||||
|
|
@ -22,10 +29,18 @@ class Responder
|
||||||
// Body
|
// Body
|
||||||
$body = $response->getBody();
|
$body = $response->getBody();
|
||||||
if ($body->isReadable()) {
|
if ($body->isReadable()) {
|
||||||
$this->outputBody($response->getBody(), $chunkSize);
|
$this->outputBody($response->getBody());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $chunkSize
|
||||||
|
*/
|
||||||
|
public function setChunkSize($chunkSize)
|
||||||
|
{
|
||||||
|
$this->chunkSize = $chunkSize;
|
||||||
|
}
|
||||||
|
|
||||||
private function getStatusLine(ResponseInterface $response)
|
private function getStatusLine(ResponseInterface $response)
|
||||||
{
|
{
|
||||||
$protocol = $response->getProtocolVersion();
|
$protocol = $response->getProtocolVersion();
|
||||||
|
|
@ -38,12 +53,12 @@ class Responder
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function outputBody(StreamableInterface $body, $chunkSize)
|
private function outputBody(StreamableInterface $body)
|
||||||
{
|
{
|
||||||
if ($chunkSize > 0) {
|
if ($this->chunkSize > 0) {
|
||||||
$body->rewind();
|
$body->rewind();
|
||||||
while (!$body->eof()) {
|
while (!$body->eof()) {
|
||||||
print $body->read($chunkSize);
|
print $body->read($this->chunkSize);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
print (string) $body;
|
print (string) $body;
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace WellRESTed\Routing;
|
||||||
|
|
||||||
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
|
||||||
|
interface ResponderInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Outputs a response.
|
||||||
|
*
|
||||||
|
* @param ResponseInterface $response Response to output
|
||||||
|
*/
|
||||||
|
public function respond(ResponseInterface $response);
|
||||||
|
}
|
||||||
|
|
@ -110,6 +110,7 @@ class ResponderTest extends \PHPUnit_Framework_TestCase
|
||||||
});
|
});
|
||||||
|
|
||||||
$responder = new Responder();
|
$responder = new Responder();
|
||||||
|
$responder->setChunkSize($chunkSize);
|
||||||
|
|
||||||
ob_start();
|
ob_start();
|
||||||
$responder->respond($this->response->reveal(), $chunkSize);
|
$responder->respond($this->response->reveal(), $chunkSize);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue