use better method to determine when a stream is a directory stream

This commit is contained in:
Robin Appelman 2018-04-24 11:05:02 +02:00
commit 0a78597117

View file

@ -26,12 +26,15 @@ abstract class Wrapper implements File, Directory {
protected $source; protected $source;
protected static function wrapSource($source, $context, $protocol, $class) { protected static function wrapSource($source, $context, $protocol, $class) {
if (!is_resource($source)) {
throw new \BadMethodCallException();
}
try { try {
stream_wrapper_register($protocol, $class); stream_wrapper_register($protocol, $class);
if (@rewinddir($source) === false) { if (self::isDirectoryHandle($source)) {
$wrapped = fopen($protocol . '://', 'r+', false, $context);
} else {
$wrapped = opendir($protocol . '://', $context); $wrapped = opendir($protocol . '://', $context);
} else {
$wrapped = fopen($protocol . '://', 'r+', false, $context);
} }
} catch (\BadMethodCallException $e) { } catch (\BadMethodCallException $e) {
stream_wrapper_unregister($protocol); stream_wrapper_unregister($protocol);
@ -41,6 +44,11 @@ abstract class Wrapper implements File, Directory {
return $wrapped; 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 * Load the source from the stream context and return the context options
* *