From 0a78597117d8a02937ea05206f219294449fb06e Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 24 Apr 2018 11:05:02 +0200 Subject: [PATCH] use better method to determine when a stream is a directory stream --- src/Wrapper.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Wrapper.php b/src/Wrapper.php index 8e52eff..babd2c1 100644 --- a/src/Wrapper.php +++ b/src/Wrapper.php @@ -26,12 +26,15 @@ abstract class Wrapper implements File, Directory { protected $source; protected static function wrapSource($source, $context, $protocol, $class) { + if (!is_resource($source)) { + throw new \BadMethodCallException(); + } try { stream_wrapper_register($protocol, $class); - if (@rewinddir($source) === false) { - $wrapped = fopen($protocol . '://', 'r+', false, $context); - } else { + if (self::isDirectoryHandle($source)) { $wrapped = opendir($protocol . '://', $context); + } else { + $wrapped = fopen($protocol . '://', 'r+', false, $context); } } catch (\BadMethodCallException $e) { stream_wrapper_unregister($protocol); @@ -41,6 +44,11 @@ protected static function wrapSource($source, $context, $protocol, $class) { return $wrapped; } + protected static function isDirectoryHandle($resource) { + $meta = stream_get_meta_data($resource); + return $meta['stream_type'] == 'dir'; + } + /** * Load the source from the stream context and return the context options *