mirror of
https://codeberg.org/icewind/streams.git
synced 2026-06-03 16:44:07 +02:00
Better cleanup of registered streams
This commit is contained in:
parent
214a31bc98
commit
99ecfc5287
4 changed files with 26 additions and 7 deletions
|
|
@ -46,9 +46,10 @@ class CallbackWrapper extends Wrapper {
|
||||||
* @param callable $write (optional)
|
* @param callable $write (optional)
|
||||||
* @param callable $close (optional)
|
* @param callable $close (optional)
|
||||||
* @return resource
|
* @return resource
|
||||||
|
*
|
||||||
|
* @throws \BadMethodCallException
|
||||||
*/
|
*/
|
||||||
public static function wrap($source, $read = null, $write = null, $close = null) {
|
public static function wrap($source, $read = null, $write = null, $close = null) {
|
||||||
stream_wrapper_register('callback', '\Icewind\Streams\CallbackWrapper');
|
|
||||||
$context = stream_context_create(array(
|
$context = stream_context_create(array(
|
||||||
'callback' => array(
|
'callback' => array(
|
||||||
'source' => $source,
|
'source' => $source,
|
||||||
|
|
@ -57,7 +58,13 @@ class CallbackWrapper extends Wrapper {
|
||||||
'close' => $close
|
'close' => $close
|
||||||
)
|
)
|
||||||
));
|
));
|
||||||
|
stream_wrapper_register('callback', '\Icewind\Streams\CallbackWrapper');
|
||||||
|
try {
|
||||||
$wrapped = fopen('callback://', 'r+', false, $context);
|
$wrapped = fopen('callback://', 'r+', false, $context);
|
||||||
|
} catch (\BadMethodCallException $e) {
|
||||||
|
stream_wrapper_unregister('callback');
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
stream_wrapper_unregister('callback');
|
stream_wrapper_unregister('callback');
|
||||||
return $wrapped;
|
return $wrapped;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -116,7 +116,12 @@ class IteratorDirectory implements Directory {
|
||||||
throw new \BadMethodCallException('$source should be an Iterator or array');
|
throw new \BadMethodCallException('$source should be an Iterator or array');
|
||||||
}
|
}
|
||||||
stream_wrapper_register('iterator', '\Icewind\Streams\IteratorDirectory');
|
stream_wrapper_register('iterator', '\Icewind\Streams\IteratorDirectory');
|
||||||
|
try {
|
||||||
$wrapped = opendir('iterator://', $context);
|
$wrapped = opendir('iterator://', $context);
|
||||||
|
} catch (\BadMethodCallException $e) {
|
||||||
|
stream_wrapper_unregister('iterator');
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
stream_wrapper_unregister('iterator');
|
stream_wrapper_unregister('iterator');
|
||||||
return $wrapped;
|
return $wrapped;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,14 +16,21 @@ class NullWrapper extends Wrapper {
|
||||||
*
|
*
|
||||||
* @param resource $source
|
* @param resource $source
|
||||||
* @return resource
|
* @return resource
|
||||||
|
*
|
||||||
|
* @throws \BadMethodCallException
|
||||||
*/
|
*/
|
||||||
public static function wrap($source) {
|
public static function wrap($source) {
|
||||||
stream_wrapper_register('null', '\Icewind\Streams\NullWrapper');
|
|
||||||
$context = stream_context_create(array(
|
$context = stream_context_create(array(
|
||||||
'null' => array(
|
'null' => array(
|
||||||
'source' => $source)
|
'source' => $source)
|
||||||
));
|
));
|
||||||
|
stream_wrapper_register('null', '\Icewind\Streams\NullWrapper');
|
||||||
|
try {
|
||||||
$wrapped = fopen('null://', 'r+', false, $context);
|
$wrapped = fopen('null://', 'r+', false, $context);
|
||||||
|
} catch (\BadMethodCallException $e) {
|
||||||
|
stream_wrapper_unregister('null');
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
stream_wrapper_unregister('null');
|
stream_wrapper_unregister('null');
|
||||||
return $wrapped;
|
return $wrapped;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,8 +21,8 @@ class IteratorDirectory extends \PHPUnit_Framework_TestCase {
|
||||||
* @expectedException \BadMethodCallException
|
* @expectedException \BadMethodCallException
|
||||||
*/
|
*/
|
||||||
public function testNoContext() {
|
public function testNoContext() {
|
||||||
stream_wrapper_register('iterator', '\Icewind\Streams\IteratorDirectory');
|
|
||||||
$context = stream_context_create(array());
|
$context = stream_context_create(array());
|
||||||
|
stream_wrapper_register('iterator', '\Icewind\Streams\IteratorDirectory');
|
||||||
try {
|
try {
|
||||||
opendir('iterator://', $context);
|
opendir('iterator://', $context);
|
||||||
stream_wrapper_unregister('iterator');
|
stream_wrapper_unregister('iterator');
|
||||||
|
|
@ -36,12 +36,12 @@ class IteratorDirectory extends \PHPUnit_Framework_TestCase {
|
||||||
* @expectedException \BadMethodCallException
|
* @expectedException \BadMethodCallException
|
||||||
*/
|
*/
|
||||||
public function testInvalidSource() {
|
public function testInvalidSource() {
|
||||||
stream_wrapper_register('iterator', '\Icewind\Streams\IteratorDirectory');
|
|
||||||
$context = stream_context_create(array(
|
$context = stream_context_create(array(
|
||||||
'dir' => array(
|
'dir' => array(
|
||||||
'array' => 2
|
'array' => 2
|
||||||
)
|
)
|
||||||
));
|
));
|
||||||
|
stream_wrapper_register('iterator', '\Icewind\Streams\IteratorDirectory');
|
||||||
try {
|
try {
|
||||||
opendir('iterator://', $context);
|
opendir('iterator://', $context);
|
||||||
stream_wrapper_unregister('iterator');
|
stream_wrapper_unregister('iterator');
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue