Skip to content

Commit

Permalink
adapt to StreamInterface changes; also use hsl
Browse files Browse the repository at this point in the history
  • Loading branch information
usox committed Mar 24, 2018
1 parent 006880c commit 142437e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 26 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
],
"require": {
"hhvm": ">=3.24",
"hhvm/experimental-http-request-response-interfaces": "dev-master"
"hhvm/experimental-http-request-response-interfaces": "dev-master",
"hhvm/hsl": "^1.3"
},
"require-dev": {
"facebook/fbexpect": "^0.4.1"
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<phpunit bootstrap="vendor/autoload.php">
<phpunit bootstrap="vendor/hh_autoload.php">
<logging>
<log type="coverage-html" target="dev/coverage" charset="UTF-8" highlight="true" />
</logging>
Expand Down
34 changes: 15 additions & 19 deletions src/Stream.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,29 @@
namespace Usox\HackTTP;

use Facebook\Experimental\Http\Message\StreamInterface;
use HH\Lib\C;

final class Stream implements StreamInterface {

private ?int $size;

private ?string $uri;
private bool $has_uri;

private bool $readable = false;

private bool $writable = false;

private bool $seekable = false;

private static Vector<string> $read_modes = Vector{
private static vec<string> $read_modes = vec[
'r', 'w+', 'r+', 'x+', 'c+', 'rb', 'w+b', 'r+b', 'x+b',
'c+b', 'rt', 'w+t', 'r+t', 'x+t', 'c+t', 'a+'
};
];

private static Vector<string> $write_modes = Vector{
private static vec<string> $write_modes = vec[
'w', 'w+', 'rw', 'r+', 'x+', 'c+', 'wb', 'w+b', 'r+b',
'x+b', 'c+b', 'w+t', 'r+t', 'x+t', 'c+t', 'a', 'a+'
};
];

public function __construct(private ?resource $stream) {
if ($stream === null) {
Expand All @@ -33,9 +34,9 @@ final class Stream implements StreamInterface {

$meta = stream_get_meta_data($this->stream);
$this->seekable = (bool) $meta['seekable'];
$this->readable = static::$read_modes->linearSearch($meta['mode']) !== -1;
$this->writable = static::$write_modes->linearSearch($meta['mode']) !== -1;
$this->uri = (string) $this->getMetadata('uri');
$this->readable = C\contains(static::$read_modes, $meta['mode']);
$this->writable = C\contains(static::$write_modes, $meta['mode']);
$this->has_uri = $meta['uri'] !== null;
}

public function __destruct(): void {
Expand Down Expand Up @@ -69,8 +70,8 @@ final class Stream implements StreamInterface {
}

$result = $this->stream;
$this->stream = $this->size = $this->uri = null;
$this->readable = $this->writable = $this->seekable = false;
$this->stream = $this->size = null;
$this->has_uri = $this->readable = $this->writable = $this->seekable = false;

return $result;
}
Expand All @@ -84,7 +85,7 @@ final class Stream implements StreamInterface {
return null;
}

if ($this->uri !== null) {
if ($this->has_uri === true) {
clearstatcache();
}

Expand Down Expand Up @@ -190,16 +191,11 @@ final class Stream implements StreamInterface {
return $bytes;
}

public function getMetadata(?string $key = null): mixed {
public function getMetadata(): dict<string, mixed> {
if ($this->stream === null) {
return $key !== null ? null : [];
} elseif ($key === null) {
return stream_get_meta_data($this->stream);
return dict[];
}

$meta = stream_get_meta_data($this->stream);

return array_key_exists($key, $meta) ? $meta[$key] : null;
return dict(stream_get_meta_data($this->stream));
}

public function __toString(): string {
Expand Down
12 changes: 7 additions & 5 deletions tests/StreamTest.hh
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@ include_once 'vendor/hhvm/experimental-http-request-response-interfaces/src/Stre
class StreamTest extends \PHPUnit_Framework_TestCase {
public static $isFReadError = false;

public function testconstructorinitializesproperties() {
public function testConstructorInitializesProperties() {
$handle = fopen('php://temp', 'r+');
fwrite($handle, 'data');
$stream = new Stream($handle);

$meta_data = $stream->getMetadata();

$this->assertTrue($stream->isReadable());
$this->assertTrue($stream->isWritable());
$this->assertTrue($stream->isSeekable());
$this->assertEquals('php://temp', $stream->getMetadata('uri'));
$this->assertInternalType('array', $stream->getMetadata());
$this->assertEquals('php://temp', $meta_data['uri']);
$this->assertEquals(4, $stream->getSize());
$this->assertFalse($stream->eof());

$stream->close();
}

Expand Down Expand Up @@ -121,8 +124,7 @@ class StreamTest extends \PHPUnit_Framework_TestCase {
$this->assertFalse($stream->isWritable());
$this->assertFalse($stream->isSeekable());
$this->assertNull($stream->getSize());
$this->assertSame([], $stream->getMetadata());
$this->assertNull($stream->getMetadata('foo'));
$this->assertEquals(dict[], $stream->getMetadata());

$throws = function ($fn) {
try {
Expand Down

0 comments on commit 142437e

Please sign in to comment.