Skip to content

Use DiactorosStreamFactory in MessageFactory, closes #19 #21

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 1 commit into from
Nov 18, 2015
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 0 additions & 14 deletions spec/MessageFactory/DiactorosFactorySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,6 @@ function it_creates_a_request_with_empty_body()
$this->createRequest('POST', '/', [], null, '1.1')->shouldHaveType('Psr\Http\Message\RequestInterface');
}

function it_creates_a_request_with_stream_body(StreamInterface $stream)
{
$stream->rewind()->shouldBeCalled();

$this->createRequest('POST', '/', [], $stream, '1.1')->shouldHaveType('Psr\Http\Message\RequestInterface');
}

function it_creates_a_request_with_resource_body()
{
$resource = tmpfile();

$this->createRequest('POST', '/', [], $resource, '1.1')->shouldHaveType('Psr\Http\Message\RequestInterface');
}

function it_creates_a_response()
{
$this->createResponse()->shouldHaveType('Psr\Http\Message\ResponseInterface');
Expand Down
46 changes: 13 additions & 33 deletions src/MessageFactory/DiactorosFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,27 @@

namespace Http\Discovery\MessageFactory;

use Http\Discovery\StreamFactory\DiactorosStreamFactory;
use Http\Message\MessageFactory;
use Psr\Http\Message\StreamInterface;
use Zend\Diactoros\Request;
use Zend\Diactoros\Response;
use Zend\Diactoros\Stream;

/**
* @author GeLo <[email protected]>
*/
class DiactorosFactory implements MessageFactory
{
/**
* @var DiactorosStreamFactory
*/
private $streamFactory;

public function __construct()
{
$this->streamFactory = new DiactorosStreamFactory();
}

/**
* {@inheritdoc}
*/
Expand All @@ -35,7 +45,7 @@ public function createRequest(
return (new Request(
$uri,
$method,
$this->createStream($body),
$this->streamFactory->createStream($body),
$headers
))->withProtocolVersion($protocolVersion);
}
Expand All @@ -51,39 +61,9 @@ public function createResponse(
$protocolVersion = '1.1'
) {
return (new Response(
$this->createStream($body),
$this->streamFactory->createStream($body),
$statusCode,
$headers
))->withProtocolVersion($protocolVersion);
}

/**
* Creates a stream
*
* @param string|resource|StreamInterface|null $body
*
* @return StreamInterface
*
* @throws \InvalidArgumentException If the stream body is invalid
*/
protected function createStream($body = null)
{
if (!$body instanceof StreamInterface) {
if (is_resource($body)) {
$body = new Stream($body);
} else {
$stream = new Stream('php://memory', 'rw');

if (isset($body)) {
$stream->write((string) $body);
}

$body = $stream;
}
}

$body->rewind();

return $body;
}
}