Stream can be created with a string as well as resource handle.

This commit is contained in:
PJ Dietz 2015-04-12 13:10:40 -04:00
parent 6e83b6b050
commit 4096295421
6 changed files with 89 additions and 146 deletions

View File

@ -9,7 +9,7 @@ use WellRESTed\Message\Response;
use WellRESTed\Message\ServerRequest; use WellRESTed\Message\ServerRequest;
use WellRESTed\Routing\Route\RouteFactory; use WellRESTed\Routing\Route\RouteFactory;
use WellRESTed\Routing\Route\RouteFactoryInterface; use WellRESTed\Routing\Route\RouteFactoryInterface;
use WellRESTed\Stream\StringStream; use WellRESTed\Stream\Stream;
class Router implements MiddlewareInterface class Router implements MiddlewareInterface
{ {
@ -64,7 +64,7 @@ class Router implements MiddlewareInterface
$this->routeTable->dispatch($request, $response); $this->routeTable->dispatch($request, $response);
} catch (HttpException $e) { } catch (HttpException $e) {
$response = $response->withStatus($e->getCode()); $response = $response->withStatus($e->getCode());
$response = $response->withBody(new StringStream($e->getMessage())); $response = $response->withBody(new Stream($e->getMessage()));
} }
$statusCode = $response->getStatusCode(); $statusCode = $response->getStatusCode();
if (isset($this->statusHandlers[$statusCode])) { if (isset($this->statusHandlers[$statusCode])) {

View File

@ -7,15 +7,27 @@ use Psr\Http\Message\StreamableInterface;
class Stream implements StreamableInterface class Stream implements StreamableInterface
{ {
/** @var resource */ /** @var resource */
private $handle; private $resource;
/** /**
* @param resource $handle A file system pointer resource that is typically created using fopen(). * Create a new Stream passing either a stream resource handle (e.g.,
* from fopen) or a string.
*
* If $resource is a string, the Stream will open a php://temp stream,
* write the string to the stream, and use that temp resource.
*
* @param resource|string $resource A file system pointer resource or
* string
*/ */
public function __construct($handle) public function __construct($resource)
{ {
if (is_resource($handle)) { if (is_resource($resource) && get_resource_type($resource) === "stream") {
$this->handle = $handle; $this->resource = $resource;
} elseif (is_string($resource)) {
$this->resource = fopen("php://temp", "r+");
if ($resource !== "") {
$this->write($resource);
}
} else { } else {
throw new \InvalidArgumentException("Expected a resource handler."); throw new \InvalidArgumentException("Expected a resource handler.");
} }
@ -33,7 +45,7 @@ class Stream implements StreamableInterface
*/ */
public function __toString() public function __toString()
{ {
rewind($this->handle); rewind($this->resource);
return $this->getContents(); return $this->getContents();
} }
@ -44,7 +56,7 @@ class Stream implements StreamableInterface
*/ */
public function close() public function close()
{ {
fclose($this->handle); fclose($this->resource);
} }
/** /**
@ -56,8 +68,8 @@ class Stream implements StreamableInterface
*/ */
public function detach() public function detach()
{ {
$stream = $this->handle; $stream = $this->resource;
$this->handle = null; $this->resource = null;
return $stream; return $stream;
} }
@ -68,7 +80,7 @@ class Stream implements StreamableInterface
*/ */
public function getSize() public function getSize()
{ {
$statistics = fstat($this->handle); $statistics = fstat($this->resource);
return $statistics["size"] ?: null; return $statistics["size"] ?: null;
} }
@ -79,7 +91,7 @@ class Stream implements StreamableInterface
*/ */
public function tell() public function tell()
{ {
return ftell($this->handle); return ftell($this->resource);
} }
/** /**
@ -89,7 +101,7 @@ class Stream implements StreamableInterface
*/ */
public function eof() public function eof()
{ {
return feof($this->handle); return feof($this->resource);
} }
/** /**
@ -116,7 +128,7 @@ class Stream implements StreamableInterface
*/ */
public function seek($offset, $whence = SEEK_SET) public function seek($offset, $whence = SEEK_SET)
{ {
fseek($this->handle, $offset, $whence); fseek($this->resource, $offset, $whence);
} }
/** /**
@ -132,7 +144,7 @@ class Stream implements StreamableInterface
*/ */
public function rewind() public function rewind()
{ {
rewind($this->handle); rewind($this->resource);
} }
/** /**
@ -155,7 +167,7 @@ class Stream implements StreamableInterface
*/ */
public function write($string) public function write($string)
{ {
return fwrite($this->handle, $string); return fwrite($this->resource, $string);
} }
/** /**
@ -180,7 +192,7 @@ class Stream implements StreamableInterface
*/ */
public function read($length) public function read($length)
{ {
return fread($this->handle, $length); return fread($this->resource, $length);
} }
/** /**
@ -190,7 +202,7 @@ class Stream implements StreamableInterface
*/ */
public function getContents() public function getContents()
{ {
return stream_get_contents($this->handle); return stream_get_contents($this->resource);
} }
/** /**
@ -207,7 +219,7 @@ class Stream implements StreamableInterface
*/ */
public function getMetadata($key = null) public function getMetadata($key = null)
{ {
$metadata = stream_get_meta_data($this->handle); $metadata = stream_get_meta_data($this->resource);
if ($key === null) { if ($key === null) {
return $metadata; return $metadata;
} else { } else {

View File

@ -1,13 +0,0 @@
<?php
namespace WellRESTed\Stream;
class StringStream extends Stream
{
public function __construct($string = "")
{
$handle = fopen("php://memory", "w+");
fwrite($handle, $string);
parent::__construct($handle);
}
}

View File

@ -18,7 +18,6 @@ use WellRESTed\Routing\Router;
* @uses WellRESTed\Routing\Route\StaticRoute * @uses WellRESTed\Routing\Route\StaticRoute
* @uses WellRESTed\Routing\Route\Route * @uses WellRESTed\Routing\Route\Route
* @uses WellRESTed\Stream\Stream * @uses WellRESTed\Stream\Stream
* @uses WellRESTed\Stream\StringStream
*/ */
class RouterTest extends \PHPUnit_Framework_TestCase class RouterTest extends \PHPUnit_Framework_TestCase
{ {

View File

@ -1,34 +0,0 @@
<?php
namespace WellRESTed\Test\Stream;
use WellRESTed\Stream\StringStream;
/**
* @covers WellRESTed\Stream\StringStream
* @uses WellRESTed\Stream\Stream
*/
class StringStreamTest extends \PHPUnit_Framework_TestCase
{
public function testCreatesInstance()
{
$stream = new StringStream();
$this->assertNotNull($stream);
}
public function testCreatesInstanceWithString()
{
$message = "Hello, World!";
$stream = new StringStream($message);
$this->assertEquals($message, (string) $stream);
}
public function testAllowsWriting()
{
$message = "Hello, World!";
$stream = new StringStream();
$stream->write("Hello,");
$stream->write(" World!");
$this->assertEquals($message, (string) $stream);
}
}

View File

@ -4,134 +4,125 @@ namespace WellRESTed\Test\Stream;
use WellRESTed\Stream\Stream; use WellRESTed\Stream\Stream;
/**
* @uses WellRESTed\Stream\Stream
*/
class StreamTest extends \PHPUnit_Framework_TestCase class StreamTest extends \PHPUnit_Framework_TestCase
{ {
private $handle; private $resource;
private $content = "Hello, world!"; private $content = "Hello, world!";
public function setUp() public function setUp()
{ {
$this->handle = fopen("php://memory", "w+"); $this->resource = fopen("php://memory", "w+");
fwrite($this->handle, $this->content); fwrite($this->resource, $this->content);
} }
public function tearDown() public function tearDown()
{ {
if (is_resource($this->handle)) { if (is_resource($this->resource)) {
fclose($this->handle); fclose($this->resource);
} }
} }
/** /**
* @covers WellRESTed\Stream\Stream::__construct() * @covers WellRESTed\Stream\Stream::__construct()
*/ */
public function testCreatesInstance() public function testCreatesInstanceWithStreamResource()
{ {
$stream = new Stream($this->handle); $stream = new Stream($this->resource);
$this->assertNotNull($stream);
}
public function testCreatesInstanceWithString()
{
$stream = new Stream("Hello, world!");
$this->assertNotNull($stream); $this->assertNotNull($stream);
} }
/** /**
* @covers WellRESTed\Stream\Stream::__construct() * @covers WellRESTed\Stream\Stream::__construct()
* @expectedException \InvalidArgumentException * @expectedException \InvalidArgumentException
* @dataProvider invalidResourceProvider
*/ */
public function testThrowsExceptiondWithoutHandle() public function testThrowsExceptiondWithInvalidResource($resource)
{ {
new Stream(null); new Stream($resource);
}
public function invalidResourceProvider()
{
return [
[null],
[true],
[4],
[[]]
];
} }
/** /**
* @covers WellRESTed\Stream\Stream::__toString() * @covers WellRESTed\Stream\Stream::__toString()
* @uses WellRESTed\Stream\Stream
*/ */
public function testCastsToString() public function testCastsToString()
{ {
$content = "Hello, world!"; $stream = new Stream($this->resource);
$this->assertEquals($this->content, (string) $stream);
$h = fopen("php://memory", "w+");
fwrite($h, $content);
rewind($h);
$stream = new Stream($h);
$this->assertEquals($content, (string) $stream);
}
/**
* @covers WellRESTed\Stream\Stream::__toString()
* @uses WellRESTed\Stream\Stream
*/
public function testRewindsBeforeCastingToString()
{
$content = "Hello, world!";
$h = fopen("php://memory", "w+");
fwrite($h, $content);
$stream = new Stream($h);
$this->assertEquals($content, (string) $stream);
} }
/** /**
* @covers WellRESTed\Stream\Stream::close() * @covers WellRESTed\Stream\Stream::close()
* @uses WellRESTed\Stream\Stream
*/ */
public function testClosesHandle() public function testClosesHandle()
{ {
$stream = new Stream($this->handle); $stream = new Stream($this->resource);
$stream->close(); $stream->close();
$this->assertFalse(is_resource($this->handle)); $this->assertFalse(is_resource($this->resource));
} }
/** /**
* @covers WellRESTed\Stream\Stream::detach() * @covers WellRESTed\Stream\Stream::detach()
* @uses WellRESTed\Stream\Stream
*/ */
public function testDetachReturnsHandle() public function testDetachReturnsHandle()
{ {
$stream = new Stream($this->handle); $stream = new Stream($this->resource);
$h = $stream->detach(); $this->assertSame($this->resource, $stream->detach());
$this->assertSame($this->handle, $h);
} }
/** /**
* @covers WellRESTed\Stream\Stream::detach() * @covers WellRESTed\Stream\Stream::detach()
* @uses WellRESTed\Stream\Stream
*/ */
public function testDetachUnsetsInstanceVariable() public function testDetachUnsetsInstanceVariable()
{ {
$stream = new Stream($this->handle); $stream = new Stream($this->resource);
$stream->detach(); $stream->detach();
$this->assertNull($stream->detach()); $this->assertNull($stream->detach());
} }
/** /**
* @covers WellRESTed\Stream\Stream::getSize * @covers WellRESTed\Stream\Stream::getSize
* @uses WellRESTed\Stream\Stream
*/ */
public function testReturnsSize() public function testReturnsSize()
{ {
$stream = new Stream($this->handle); $stream = new Stream($this->resource);
$this->assertEquals(strlen($this->content), $stream->getSize()); $this->assertEquals(strlen($this->content), $stream->getSize());
} }
/** /**
* @covers WellRESTed\Stream\Stream::tell * @covers WellRESTed\Stream\Stream::tell
* @uses WellRESTed\Stream\Stream
*/ */
public function testTellReturnsHandlePosition() public function testTellReturnsHandlePosition()
{ {
$stream = new Stream($this->handle); $stream = new Stream($this->resource);
fseek($this->handle, 10); fseek($this->resource, 10);
$this->assertEquals(10, $stream->tell()); $this->assertEquals(10, $stream->tell());
} }
/** /**
* @covers WellRESTed\Stream\Stream::eof * @covers WellRESTed\Stream\Stream::eof
* @uses WellRESTed\Stream\Stream
*/ */
public function testReturnsOef() public function testReturnsOef()
{ {
$stream = new Stream($this->handle); $stream = new Stream($this->resource);
$stream->rewind(); $stream->rewind();
$this->assertFalse($stream->eof()); $this->assertFalse($stream->eof());
$stream->getContents(); $stream->getContents();
@ -140,58 +131,53 @@ class StreamTest extends \PHPUnit_Framework_TestCase
/** /**
* @covers WellRESTed\Stream\Stream::isSeekable * @covers WellRESTed\Stream\Stream::isSeekable
* @uses WellRESTed\Stream\Stream
*/ */
public function testReadsSeekableStatusFromMetadata() public function testReadsSeekableStatusFromMetadata()
{ {
$stream = new Stream($this->handle); $stream = new Stream($this->resource);
$metadata = stream_get_meta_data($this->handle); $metadata = stream_get_meta_data($this->resource);
$seekable = $metadata["seekable"] == 1; $seekable = $metadata["seekable"] == 1;
$this->assertEquals($seekable, $stream->isSeekable()); $this->assertEquals($seekable, $stream->isSeekable());
} }
/** /**
* @covers WellRESTed\Stream\Stream::seek * @covers WellRESTed\Stream\Stream::seek
* @uses WellRESTed\Stream\Stream
*/ */
public function testSeeksToPosition() public function testSeeksToPosition()
{ {
$stream = new Stream($this->handle); $stream = new Stream($this->resource);
$stream->seek(10); $stream->seek(10);
$this->assertEquals(10, ftell($this->handle)); $this->assertEquals(10, ftell($this->resource));
} }
/** /**
* @covers WellRESTed\Stream\Stream::rewind * @covers WellRESTed\Stream\Stream::rewind
* @uses WellRESTed\Stream\Stream
*/ */
public function testRewindReturnsToBeginning() public function testRewindReturnsToBeginning()
{ {
$stream = new Stream($this->handle); $stream = new Stream($this->resource);
$stream->seek(10); $stream->seek(10);
$stream->rewind(); $stream->rewind();
$this->assertEquals(0, ftell($this->handle)); $this->assertEquals(0, ftell($this->resource));
} }
/** /**
* @covers WellRESTed\Stream\Stream::write * @covers WellRESTed\Stream\Stream::write
* @uses WellRESTed\Stream\Stream
*/ */
public function testWritesToHandle() public function testWritesToHandle()
{ {
$message = "\nThis is a stream."; $message = "\nThis is a stream.";
$stream = new Stream($this->handle); $stream = new Stream($this->resource);
$stream->write($message); $stream->write($message);
$this->assertEquals($this->content . $message, (string) $stream); $this->assertEquals($this->content . $message, (string) $stream);
} }
/** /**
* @covers WellRESTed\Stream\Stream::read * @covers WellRESTed\Stream\Stream::read
* @uses WellRESTed\Stream\Stream
*/ */
public function testReadsFromStream() public function testReadsFromStream()
{ {
$stream = new Stream($this->handle); $stream = new Stream($this->resource);
$stream->seek(7); $stream->seek(7);
$string = $stream->read(5); $string = $stream->read(5);
$this->assertEquals("world", $string); $this->assertEquals("world", $string);
@ -199,11 +185,10 @@ class StreamTest extends \PHPUnit_Framework_TestCase
/** /**
* @covers WellRESTed\Stream\Stream::getContents * @covers WellRESTed\Stream\Stream::getContents
* @uses WellRESTed\Stream\Stream
*/ */
public function testReadsToEnd() public function testReadsToEnd()
{ {
$stream = new Stream($this->handle); $stream = new Stream($this->resource);
$stream->seek(7); $stream->seek(7);
$string = $stream->getContents(); $string = $stream->getContents();
$this->assertEquals("world!", $string); $this->assertEquals("world!", $string);
@ -211,28 +196,25 @@ class StreamTest extends \PHPUnit_Framework_TestCase
/** /**
* @covers WellRESTed\Stream\Stream::getMetadata * @covers WellRESTed\Stream\Stream::getMetadata
* @uses WellRESTed\Stream\Stream
*/ */
public function testReturnsMetadataArray() public function testReturnsMetadataArray()
{ {
$stream = new Stream($this->handle); $stream = new Stream($this->resource);
$this->assertEquals(stream_get_meta_data($this->handle), $stream->getMetadata()); $this->assertEquals(stream_get_meta_data($this->resource), $stream->getMetadata());
} }
/** /**
* @covers WellRESTed\Stream\Stream::getMetadata * @covers WellRESTed\Stream\Stream::getMetadata
* @uses WellRESTed\Stream\Stream
*/ */
public function testReturnsMetadataItem() public function testReturnsMetadataItem()
{ {
$stream = new Stream($this->handle); $stream = new Stream($this->resource);
$metadata = stream_get_meta_data($this->handle); $metadata = stream_get_meta_data($this->resource);
$this->assertEquals($metadata["mode"], $stream->getMetadata("mode")); $this->assertEquals($metadata["mode"], $stream->getMetadata("mode"));
} }
/** /**
* @covers WellRESTed\Stream\Stream::isReadable * @covers WellRESTed\Stream\Stream::isReadable
* @uses WellRESTed\Stream\Stream
* @dataProvider modeProvider * @dataProvider modeProvider
*/ */
public function testReturnsIsReadableForReadableStreams($mode, $readable, $writeable) public function testReturnsIsReadableForReadableStreams($mode, $readable, $writeable)
@ -241,15 +223,13 @@ class StreamTest extends \PHPUnit_Framework_TestCase
if ($mode[0] === "x") { if ($mode[0] === "x") {
unlink($tmp); unlink($tmp);
} }
fclose($this->handle); $resource = fopen($tmp, $mode);
$this->handle = fopen($tmp, $mode); $stream = new Stream($resource);
$stream = new Stream($this->handle);
$this->assertEquals($readable, $stream->isReadable()); $this->assertEquals($readable, $stream->isReadable());
} }
/** /**
* @covers WellRESTed\Stream\Stream::isWritable * @covers WellRESTed\Stream\Stream::isWritable
* @uses WellRESTed\Stream\Stream
* @dataProvider modeProvider * @dataProvider modeProvider
*/ */
public function testReturnsIsWritableForWritableStreams($mode, $readable, $writeable) public function testReturnsIsWritableForWritableStreams($mode, $readable, $writeable)
@ -258,9 +238,8 @@ class StreamTest extends \PHPUnit_Framework_TestCase
if ($mode[0] === "x") { if ($mode[0] === "x") {
unlink($tmp); unlink($tmp);
} }
fclose($this->handle); $resource = fopen($tmp, $mode);
$this->handle = fopen($tmp, $mode); $stream = new Stream($resource);
$stream = new Stream($this->handle);
$this->assertEquals($writeable, $stream->isWritable()); $this->assertEquals($writeable, $stream->isWritable());
} }