Skip to content

Commit

Permalink
use better method to determine when a stream is a directory stream
Browse files Browse the repository at this point in the history
  • Loading branch information
icewind1991 committed Apr 24, 2018
1 parent 851b822 commit 0a78597
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/Wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
*
Expand Down

0 comments on commit 0a78597

Please sign in to comment.