Implemented stream_cast and stream_set_option.

This commit is contained in:
Matt Butcher
2012-01-19 21:15:43 -06:00
parent 49cc5bbb57
commit 3ec063155b
2 changed files with 62 additions and 5 deletions

View File

@@ -171,8 +171,18 @@ class StreamWrapper {
}
/**
* Cast stream into a lower-level stream.
*
* This is used for stream_select() and perhaps others.Because it exposes
* the lower-level buffer objects, this function can have unexpected
* side effects.
*
* @return resource
* this returns the underlying stream.
*/
public function stream_cast($cast_as) {
return $this->objStream;
}
/**
@@ -270,10 +280,6 @@ class StreamWrapper {
}
*/
public function stream_metadata($path, $option, $var) {
}
/**
* Open a stream resource.
*
@@ -474,7 +480,28 @@ class StreamWrapper {
return $ret === 0;
}
/**
* Set options on the underlying stream.
*
* The settings here do not trickle down to the network socket, which is
* left open for only a brief period of time. Instead, they impact the middle
* buffer stream, where the file is read and written to between flush/close
* operations. Thus, tuning these will not have any impact on network
* performance.
*
* See stream_set_blocking(), stream_set_timeout(), and stream_write_buffer().
*/
public function stream_set_option($option, $arg1, $arg2) {
switch ($option) {
case STREAM_OPTION_BLOCKING:
return stream_set_blocking($this->objStream, $arg1);
case STREAM_OPTION_READ_TIMEOUT:
// XXX: Should this have any effect on the lower-level
// socket, too? Or just the buffered tmp storage?
return stream_set_timeout($this->objStream, $arg1, $arg2);
case STREAM_OPTION_WRITE_BUFFER:
return stream_set_write_buffer($this->objStream, $arg2);
}
}

View File

@@ -283,6 +283,35 @@ class StreamWrapperTest extends \HPCloud\Tests\TestCase {
}
/**
* @depends testClose
*/
public function testCast() {
$url = $this->newUrl(self::FNAME);
$res = fopen($url, 'r', FALSE, $this->basicSwiftContext());
$read = array($res);
$write = array();
$except = array();
$num_changed = stream_select($read, $write, $except, 0);
$this->assertGreaterThan(0, $num_changed);
}
public function testSetOption() {
$url = $this->newUrl('fake.foo');
$fake = fopen($url, 'nope', FALSE, $this->basicSwiftContext());
$this->assertTrue(stream_set_blocking($fake, 1));
// Returns 0 on success.
$this->assertEquals(0, stream_set_write_buffer($fake, 8192));
// Cant set a timeout on a tmp storage:
$this->assertFalse(stream_set_timeout($fake, 10));
fclose($fake);
}
public function testOpenFailureWithWrite() {
// Make sure that a file opened as write only does not allow READ ops.
$url = $this->newUrl(__FUNCTION__);
@@ -292,4 +321,5 @@ class StreamWrapperTest extends \HPCloud\Tests\TestCase {
}
}