Skip to content

Commit 00163e3

Browse files
committed
Use DiactorosStreamFactory in MessageFactory, closes #19
1 parent ffd9756 commit 00163e3

File tree

2 files changed

+13
-47
lines changed

2 files changed

+13
-47
lines changed

spec/MessageFactory/DiactorosFactorySpec.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,6 @@ function it_creates_a_request_with_empty_body()
4040
$this->createRequest('POST', '/', [], null, '1.1')->shouldHaveType('Psr\Http\Message\RequestInterface');
4141
}
4242

43-
function it_creates_a_request_with_stream_body(StreamInterface $stream)
44-
{
45-
$stream->rewind()->shouldBeCalled();
46-
47-
$this->createRequest('POST', '/', [], $stream, '1.1')->shouldHaveType('Psr\Http\Message\RequestInterface');
48-
}
49-
50-
function it_creates_a_request_with_resource_body()
51-
{
52-
$resource = tmpfile();
53-
54-
$this->createRequest('POST', '/', [], $resource, '1.1')->shouldHaveType('Psr\Http\Message\RequestInterface');
55-
}
56-
5743
function it_creates_a_response()
5844
{
5945
$this->createResponse()->shouldHaveType('Psr\Http\Message\ResponseInterface');

src/MessageFactory/DiactorosFactory.php

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,27 @@
1111

1212
namespace Http\Discovery\MessageFactory;
1313

14+
use Http\Discovery\StreamFactory\DiactorosStreamFactory;
1415
use Http\Message\MessageFactory;
1516
use Psr\Http\Message\StreamInterface;
1617
use Zend\Diactoros\Request;
1718
use Zend\Diactoros\Response;
18-
use Zend\Diactoros\Stream;
1919

2020
/**
2121
* @author GeLo <[email protected]>
2222
*/
2323
class DiactorosFactory implements MessageFactory
2424
{
25+
/**
26+
* @var DiactorosStreamFactory
27+
*/
28+
private $streamFactory;
29+
30+
public function __construct()
31+
{
32+
$this->streamFactory = new DiactorosStreamFactory();
33+
}
34+
2535
/**
2636
* {@inheritdoc}
2737
*/
@@ -35,7 +45,7 @@ public function createRequest(
3545
return (new Request(
3646
$uri,
3747
$method,
38-
$this->createStream($body),
48+
$this->streamFactory->createStream($body),
3949
$headers
4050
))->withProtocolVersion($protocolVersion);
4151
}
@@ -51,39 +61,9 @@ public function createResponse(
5161
$protocolVersion = '1.1'
5262
) {
5363
return (new Response(
54-
$this->createStream($body),
64+
$this->streamFactory->createStream($body),
5565
$statusCode,
5666
$headers
5767
))->withProtocolVersion($protocolVersion);
5868
}
59-
60-
/**
61-
* Creates a stream
62-
*
63-
* @param string|resource|StreamInterface|null $body
64-
*
65-
* @return StreamInterface
66-
*
67-
* @throws \InvalidArgumentException If the stream body is invalid
68-
*/
69-
protected function createStream($body = null)
70-
{
71-
if (!$body instanceof StreamInterface) {
72-
if (is_resource($body)) {
73-
$body = new Stream($body);
74-
} else {
75-
$stream = new Stream('php://memory', 'rw');
76-
77-
if (isset($body)) {
78-
$stream->write((string) $body);
79-
}
80-
81-
$body = $stream;
82-
}
83-
}
84-
85-
$body->rewind();
86-
87-
return $body;
88-
}
8969
}

0 commit comments

Comments
 (0)