Better cleanup of registered streams

This commit is contained in:
Robin Appelman 2014-07-31 01:06:38 +02:00
commit 99ecfc5287
4 changed files with 26 additions and 7 deletions

View file

@ -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
) )
)); ));
$wrapped = fopen('callback://', 'r+', false, $context); stream_wrapper_register('callback', '\Icewind\Streams\CallbackWrapper');
try {
$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;
} }

View file

@ -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');
$wrapped = opendir('iterator://', $context); try {
$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;
} }

View file

@ -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)
)); ));
$wrapped = fopen('null://', 'r+', false, $context); stream_wrapper_register('null', '\Icewind\Streams\NullWrapper');
try {
$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;
} }

View file

@ -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');