Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Add http factory psr 17 #356

Merged
merged 22 commits into from
Aug 28, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions build/subsplitt/viserio-split-full.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ git subsplit publish src/Viserio/Exception:git@github.com:viserio/exception.git
git subsplit publish src/Viserio/Filesystem:git@github.com:viserio/filesystem.git
git subsplit publish src/Viserio/Hashing:git@github.com:viserio/hashing.git
git subsplit publish src/Viserio/Http:git@github.com:viserio/http.git
git subsplit publish src/Viserio/HttpFactory:git@github.com:viserio/http-factory.git
git subsplit publish src/Viserio/Log:git@github.com:viserio/log.git
git subsplit publish src/Viserio/Mail:git@github.com:viserio/mail.git
git subsplit publish src/Viserio/Middleware:git@github.com:viserio/middleware.git
Expand Down
1 change: 1 addition & 0 deletions build/subsplitt/viserio-split.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ git subsplit publish --heads="master 0.10.0-dev" --no-tags src/Viserio/Exception
git subsplit publish --heads="master 0.10.0-dev" --no-tags src/Viserio/Filesystem:git@github.com:viserio/filesystem.git
git subsplit publish --heads="master 0.10.0-dev" --no-tags src/Viserio/Hashing:git@github.com:viserio/hashing.git
git subsplit publish --heads="master 0.10.0-dev" --no-tags src/Viserio/Http:git@github.com:viserio/http.git
git subsplit publish --heads="master 0.10.0-dev" --no-tags src/Viserio/HttpFactory:git@github.com:viserio/http-factory.git
git subsplit publish --heads="master 0.10.0-dev" --no-tags src/Viserio/Log:git@github.com:viserio/log.git
git subsplit publish --heads="master 0.10.0-dev" --no-tags src/Viserio/Mail:git@github.com:viserio/mail.git
git subsplit publish --heads="master 0.10.0-dev" --no-tags src/Viserio/Middleware:git@github.com:viserio/middleware.git
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"danielstjules/stringy" : "^2.3",
"defuse/php-encryption" : "^2.0",
"filp/whoops" : "^2.0",
"http-interop/http-factory" : "dev-master",
"league/flysystem" : "^1.0",
"jeremeamia/SuperClosure" : "^2.2",
"monolog/monolog" : "^1.17",
Expand Down Expand Up @@ -82,6 +83,7 @@
"viserio/filessystem" : "self.version",
"viserio/hashing" : "self.version",
"viserio/http" : "self.version",
"viserio/http-factory" : "self.version",
"viserio/log" : "self.version",
"viserio/mail" : "self.version",
"viserio/middleware" : "self.version",
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@
<testsuite name="Narrowspark Http Component Test Suite">
<directory>./src/Viserio/Http/Tests</directory>
</testsuite>
<testsuite name="Narrowspark Http Factory Component Test Suite">
<directory>./src/Viserio/HttpFactory/Tests</directory>
</testsuite>
<testsuite name="Narrowspark Log Component Test Suite">
<directory>./src/Viserio/Log/Tests</directory>
</testsuite>
Expand Down
14 changes: 10 additions & 4 deletions src/Viserio/Http/AbstractMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public function withoutHeader($header)
public function getBody()
{
if (! $this->stream) {
$this->stream = (new StreamFactory())->createStreamFromString('');
$this->stream = new Stream(fopen('php://temp', 'r+'));
}

return $this->stream;
Expand Down Expand Up @@ -247,17 +247,23 @@ protected function setHeaders(array $headers)
*/
protected function createStream($body): StreamInterface
{
$stream = new StreamFactory();
$type = gettype($body);

if ($body instanceof StreamInterface) {
return $body;
} elseif (is_string($body)) {
return $stream->createStreamFromString($body);
$stream = fopen('php://temp', 'r+');

if ($body !== '') {
fwrite($stream, $body);
fseek($stream, 0);
}

return new Stream($stream);
} elseif ($type === 'NULL') {
return new Stream(fopen('php://temp', 'r+'));
} elseif ($type === 'resource') {
return $stream->createStreamFromResource($body);
return new Stream($body);
}

throw new InvalidArgumentException('Invalid resource type: ' . gettype($body));
Expand Down
24 changes: 0 additions & 24 deletions src/Viserio/Http/RequestFactory.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Viserio/Http/Response/RedirectResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use InvalidArgumentException;
use Psr\Http\Message\UriInterface;
use Viserio\Http\Response;
use Viserio\Http\StreamFactory;
use Viserio\Http\Stream;

class RedirectResponse extends Response
{
Expand Down Expand Up @@ -33,6 +33,6 @@ public function __construct($uri, int $status = 302, array $headers = [])

$headers['location'] = [(string) $uri];

parent::__construct($status, $headers, (new StreamFactory())->createStream());
parent::__construct($status, $headers, new Stream(fopen('php://temp', 'r+')));
}
}
22 changes: 0 additions & 22 deletions src/Viserio/Http/ResponseFactory.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Viserio/Http/Stream/LazyOpenStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Psr\Http\Message\StreamInterface;
use Throwable;
use UnexpectedValueException;
use Viserio\Http\StreamFactory;
use Viserio\Http\Stream;
use Viserio\Http\Util;

class LazyOpenStream implements StreamInterface
Expand Down Expand Up @@ -188,6 +188,6 @@ public function write($string)
*/
protected function createStream(): StreamInterface
{
return (new StreamFactory())->createStreamFromResource(Util::tryFopen($this->filename, $this->mode));
return new Stream(Util::tryFopen($this->filename, $this->mode));
}
}
54 changes: 0 additions & 54 deletions src/Viserio/Http/StreamFactory.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Viserio/Http/Tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use Psr\Http\Message\UriInterface;
use StdClass;
use Viserio\Http\Request;
use Viserio\Http\Stream;
use Viserio\Http\Stream\FnStream;
use Viserio\Http\StreamFactory;
use Viserio\Http\Uri;

class RequestTest extends AbstractMessageTest
Expand Down Expand Up @@ -151,7 +151,7 @@ public function testConstructorDoesNotReadStreamBody()
{
$streamIsRead = false;

$body = FnStream::decorate((new StreamFactory())->createStreamFromString(''), [
$body = FnStream::decorate(new Stream(fopen('php://temp', 'r+')), [
'__toString' => function () use (&$streamIsRead) {
$streamIsRead = true;

Expand Down
13 changes: 9 additions & 4 deletions src/Viserio/Http/Tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Viserio\Http\Response;
use Viserio\Http\Stream;
use Viserio\Http\Stream\FnStream;
use Viserio\Http\StreamFactory;

class ResponseTest extends AbstractMessageTest
{
Expand Down Expand Up @@ -96,7 +96,7 @@ public function testCanConstructWithStatusCode()
public function testConstructorDoesNotReadStreamBody()
{
$streamIsRead = false;
$body = FnStream::decorate((new StreamFactory())->createStreamFromString(''), [
$body = FnStream::decorate(new Stream(fopen('php://temp', 'r+')), [
'__toString' => function () use (&$streamIsRead) {
$streamIsRead = true;

Expand Down Expand Up @@ -193,8 +193,13 @@ public function testSameInstanceWhenSameProtocol()

public function testWithBody()
{
$body = (new StreamFactory())->createStreamFromString('0');
$response = (new Response())->withBody($body);
$body = '0';
$stream = fopen('php://temp', 'r+');

fwrite($stream, $body);
fseek($stream, 0);

$response = (new Response())->withBody(new Stream($stream));

$this->assertInstanceOf(StreamInterface::class, $response->getBody());
$this->assertSame('0', (string) $response->getBody());
Expand Down
50 changes: 43 additions & 7 deletions src/Viserio/Http/Tests/Stream/ByteCountingStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
declare(strict_types=1);
namespace Viserio\Http\Tests\Stream;

use Viserio\Http\Stream;
use Viserio\Http\Stream\ByteCountingStream;
use Viserio\Http\StreamFactory;

class ByteCountingStreamTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -13,7 +13,13 @@ class ByteCountingStreamTest extends \PHPUnit_Framework_TestCase
*/
public function testEnsureNonNegativeByteCount()
{
new ByteCountingStream((new StreamFactory())->createStreamFromString('testing'), -2);
$body = 'testing';
$stream = fopen('php://temp', 'r+');

fwrite($stream, $body);
fseek($stream, 0);

new ByteCountingStream(new Stream($stream), -2);
}

/**
Expand All @@ -22,19 +28,37 @@ public function testEnsureNonNegativeByteCount()
*/
public function testEnsureValidByteCountNumber()
{
new ByteCountingStream((new StreamFactory())->createStreamFromString('testing'), 10);
$body = 'testing';
$stream = fopen('php://temp', 'r+');

fwrite($stream, $body);
fseek($stream, 0);

new ByteCountingStream(new Stream($stream), 10);
}

public function testByteCountingReadWhenAvailable()
{
$testStream = new ByteCountingStream((new StreamFactory())->createStreamFromString('foo bar test'), 8);
$body = 'foo bar test';
$stream = fopen('php://temp', 'r+');

fwrite($stream, $body);
fseek($stream, 0);

$testStream = new ByteCountingStream(new Stream($stream), 8);

$this->assertEquals('foo ', $testStream->read(4));
$this->assertEquals('bar ', $testStream->read(4));
$this->assertEquals('', $testStream->read(4));

$body = 'testing';
$stream = fopen('php://temp', 'r+');

fwrite($stream, $body);
fseek($stream, 0);

$testStream->close();
$testStream = new ByteCountingStream((new StreamFactory())->createStreamFromString('testing'), 5);
$testStream = new ByteCountingStream(new Stream($stream), 5);
$testStream->seek(4);

$this->assertEquals('ing', $testStream->read(5));
Expand All @@ -48,7 +72,13 @@ public function testByteCountingReadWhenAvailable()
*/
public function testEnsureStopReadWhenHitEof()
{
$testStream = new ByteCountingStream((new StreamFactory())->createStreamFromString('abc'), 3);
$body = 'abc';
$stream = fopen('php://temp', 'r+');

fwrite($stream, $body);
fseek($stream, 0);

$testStream = new ByteCountingStream(new Stream($stream), 3);
$testStream->seek(3);
$testStream->read(3);
}
Expand All @@ -59,7 +89,13 @@ public function testEnsureStopReadWhenHitEof()
*/
public function testEnsureReadUnclosedStream()
{
$body = (new StreamFactory())->createStreamFromString('closed');
$body = 'closed';
$stream = fopen('php://temp', 'r+');

fwrite($stream, $body);
fseek($stream, 0);

$body = new Stream($stream);
$closedStream = new ByteCountingStream($body, 5);
$body->close();
$closedStream->read(3);
Expand Down