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 *