Browse Source
MFH: Added stream_cast() and stream_set_options() to user-space stream
MFH: Added stream_cast() and stream_set_options() to user-space stream
wrappers, allowing stream_select(), stream_set_blocking(), stream_set_timeout() and stream_set_write_buffer() to work with user-space stream wrappers. Will document.PECL
4 changed files with 427 additions and 2 deletions
-
3NEWS
-
94ext/standard/tests/file/userstreams_002.phpt
-
153ext/standard/tests/file/userstreams_003.phpt
-
179main/streams/userspace.c
@ -0,0 +1,94 @@ |
|||
--TEST-- |
|||
User-space streams: stream_cast() |
|||
--FILE-- |
|||
<?php |
|||
class test_wrapper_base { |
|||
public $return_value; |
|||
function stream_open($path, $mode, $openedpath) { |
|||
return true; |
|||
} |
|||
function stream_eof() { |
|||
return false; |
|||
} |
|||
} |
|||
class test_wrapper extends test_wrapper_base { |
|||
function stream_cast($castas) { |
|||
return $this->return_value; |
|||
} |
|||
} |
|||
function test($name, $fd, $return_value) { |
|||
echo "\n------ $name: -------\n"; |
|||
$data = stream_get_meta_data($fd); |
|||
$data['wrapper_data']->return_value = $return_value; |
|||
$r = array($fd); |
|||
$w = $e = null; |
|||
var_dump(stream_select($r, $w, $e, 0) !== false); |
|||
} |
|||
|
|||
var_dump(stream_wrapper_register('test', 'test_wrapper')); |
|||
var_dump(stream_wrapper_register('test2', 'test_wrapper_base')); |
|||
|
|||
$fd = fopen("test://foo","r"); |
|||
$fd2 = fopen("test2://foo","r"); |
|||
|
|||
test("valid stream", $fd, STDIN); |
|||
test("stream_cast not implemented", $fd2, null); |
|||
test("return value is false", $fd, false); |
|||
test("return value not a stream resource", $fd, "foo"); |
|||
test("return value is stream itself", $fd, $fd); |
|||
test("return value cannot be casted", $fd, $fd2); |
|||
|
|||
?> |
|||
--EXPECTF-- |
|||
bool(true) |
|||
bool(true) |
|||
|
|||
------ valid stream: ------- |
|||
bool(true) |
|||
|
|||
------ stream_cast not implemented: ------- |
|||
|
|||
Warning: stream_select(): test_wrapper_base::stream_cast is not implemented! in %s |
|||
|
|||
Warning: stream_select(): cannot represent a stream of type user-space as a select()able descriptor in %s |
|||
|
|||
Warning: stream_select(): No stream arrays were passed in %s |
|||
bool(false) |
|||
|
|||
------ return value is false: ------- |
|||
|
|||
Warning: stream_select(): cannot represent a stream of type user-space as a select()able descriptor in %s |
|||
|
|||
Warning: stream_select(): No stream arrays were passed in %s |
|||
bool(false) |
|||
|
|||
------ return value not a stream resource: ------- |
|||
|
|||
Warning: stream_select(): supplied argument is not a valid stream resource in %s |
|||
|
|||
Warning: stream_select(): test_wrapper::stream_cast must return a stream resource in %s |
|||
|
|||
Warning: stream_select(): cannot represent a stream of type user-space as a select()able descriptor in %s |
|||
|
|||
Warning: stream_select(): No stream arrays were passed in %s |
|||
bool(false) |
|||
|
|||
------ return value is stream itself: ------- |
|||
|
|||
Warning: stream_select(): test_wrapper::stream_cast must not return itself in %s |
|||
|
|||
Warning: stream_select(): cannot represent a stream of type user-space as a select()able descriptor in %s |
|||
|
|||
Warning: stream_select(): No stream arrays were passed in %s |
|||
bool(false) |
|||
|
|||
------ return value cannot be casted: ------- |
|||
|
|||
Warning: stream_select(): test_wrapper_base::stream_cast is not implemented! in %s |
|||
|
|||
Warning: stream_select(): cannot represent a stream of type user-space as a select()able descriptor in %s |
|||
|
|||
Warning: stream_select(): cannot represent a stream of type user-space as a select()able descriptor in %s |
|||
|
|||
Warning: stream_select(): No stream arrays were passed in %s |
|||
bool(false) |
|||
@ -0,0 +1,153 @@ |
|||
--TEST-- |
|||
User-space streams: stream_set_option() |
|||
--FILE-- |
|||
<?php |
|||
class test_wrapper_base { |
|||
public $return_value; |
|||
public $expected_option; |
|||
public $expected_value; |
|||
function stream_open($path, $mode, $openedpath) { |
|||
return true; |
|||
} |
|||
function stream_eof() { |
|||
return false; |
|||
} |
|||
} |
|||
class test_wrapper extends test_wrapper_base { |
|||
function stream_set_option($option, $value, $ptrparam) { |
|||
echo "value:\n"; |
|||
var_dump($value); |
|||
echo "ptrparam:\n"; |
|||
var_dump($ptrparam); |
|||
echo "\$option === $option === " . $this->expected_option . ":\n";; |
|||
var_dump($option === $this->expected_option); |
|||
echo "\$value === $value === " . $this->expected_value. ":\n";; |
|||
var_dump($value === $this->expected_value); |
|||
return $this->return_value; |
|||
} |
|||
} |
|||
|
|||
function test($name, $fd, $return_value, $func, $args, $expected_option, $expected_value) { |
|||
echo "\n------ $name: -------\n"; |
|||
$data = stream_get_meta_data($fd); |
|||
$data['wrapper_data']->return_value = $return_value; |
|||
$data['wrapper_data']->expected_option = $expected_option; |
|||
$data['wrapper_data']->expected_value = $expected_value; |
|||
var_dump(call_user_func_array($func, $args)); |
|||
} |
|||
|
|||
var_dump(stream_wrapper_register('test', 'test_wrapper')); |
|||
var_dump(stream_wrapper_register('test2', 'test_wrapper_base')); |
|||
|
|||
$fd = fopen("test://foo","r"); |
|||
$fd2 = fopen("test2://foo","r"); |
|||
|
|||
test("stream_set_blocking - 1", $fd, true, "stream_set_blocking", array($fd,0), STREAM_OPTION_BLOCKING, 0); |
|||
test("stream_set_blocking - 2", $fd, false, "stream_set_blocking", array($fd,1), STREAM_OPTION_BLOCKING, 1); |
|||
test("stream_set_blocking - 3", $fd, "foo", "stream_set_blocking", array($fd,0), STREAM_OPTION_BLOCKING, 0); |
|||
test("stream_set_blocking - 4", $fd2, true, "stream_set_blocking", array($fd2,1), STREAM_OPTION_BLOCKING, 1); |
|||
|
|||
test("stream_set_write_buffer - 1", $fd, true, "stream_set_write_buffer", array($fd,0), STREAM_OPTION_WRITE_BUFFER, STREAM_BUFFER_NONE); |
|||
test("stream_set_write_buffer - 2", $fd, true, "stream_set_write_buffer", array($fd,4096), STREAM_OPTION_WRITE_BUFFER, STREAM_BUFFER_FULL); |
|||
test("stream_set_write_buffer - 3", $fd, false, "stream_set_write_buffer", array($fd,8192), STREAM_OPTION_WRITE_BUFFER, STREAM_BUFFER_FULL); |
|||
|
|||
test("stream_set_timeout - 1", $fd, true, "stream_set_timeout", array($fd,10,11), STREAM_OPTION_READ_TIMEOUT, 10); |
|||
test("stream_set_timeout - 2", $fd, false, "stream_set_timeout", array($fd,11,12), STREAM_OPTION_READ_TIMEOUT, 11); |
|||
|
|||
?> |
|||
--EXPECTF-- |
|||
bool(true) |
|||
bool(true) |
|||
|
|||
------ stream_set_blocking - 1: ------- |
|||
value: |
|||
int(0) |
|||
ptrparam: |
|||
NULL |
|||
$option === 1 === 1: |
|||
bool(true) |
|||
$value === 0 === 0: |
|||
bool(true) |
|||
bool(true) |
|||
|
|||
------ stream_set_blocking - 2: ------- |
|||
value: |
|||
int(1) |
|||
ptrparam: |
|||
NULL |
|||
$option === 1 === 1: |
|||
bool(true) |
|||
$value === 1 === 1: |
|||
bool(true) |
|||
bool(false) |
|||
|
|||
------ stream_set_blocking - 3: ------- |
|||
value: |
|||
int(0) |
|||
ptrparam: |
|||
NULL |
|||
$option === 1 === 1: |
|||
bool(true) |
|||
$value === 0 === 0: |
|||
bool(true) |
|||
bool(true) |
|||
|
|||
------ stream_set_blocking - 4: ------- |
|||
|
|||
Warning: stream_set_blocking(): test_wrapper_base::stream_set_option is not implemented! in %s |
|||
bool(false) |
|||
|
|||
------ stream_set_write_buffer - 1: ------- |
|||
value: |
|||
int(0) |
|||
ptrparam: |
|||
int(8192) |
|||
$option === 3 === 3: |
|||
bool(true) |
|||
$value === 0 === 0: |
|||
bool(true) |
|||
int(0) |
|||
|
|||
------ stream_set_write_buffer - 2: ------- |
|||
value: |
|||
int(2) |
|||
ptrparam: |
|||
int(4096) |
|||
$option === 3 === 3: |
|||
bool(true) |
|||
$value === 2 === 2: |
|||
bool(true) |
|||
int(0) |
|||
|
|||
------ stream_set_write_buffer - 3: ------- |
|||
value: |
|||
int(2) |
|||
ptrparam: |
|||
int(8192) |
|||
$option === 3 === 3: |
|||
bool(true) |
|||
$value === 2 === 2: |
|||
bool(true) |
|||
int(-1) |
|||
|
|||
------ stream_set_timeout - 1: ------- |
|||
value: |
|||
int(10) |
|||
ptrparam: |
|||
int(11) |
|||
$option === 4 === 4: |
|||
bool(true) |
|||
$value === 10 === 10: |
|||
bool(true) |
|||
bool(true) |
|||
|
|||
------ stream_set_timeout - 2: ------- |
|||
value: |
|||
int(11) |
|||
ptrparam: |
|||
int(12) |
|||
$option === 4 === 4: |
|||
bool(true) |
|||
$value === 11 === 11: |
|||
bool(true) |
|||
bool(false) |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue