Add directory support to wrappers

This commit is contained in:
Robin Appelman 2014-08-27 15:36:58 +02:00
commit 53cfe7f4f1
5 changed files with 121 additions and 28 deletions

View file

@ -12,7 +12,7 @@ namespace Icewind\Streams;
*
* This wrapper itself doesn't implement any functionality but is just a base class for other wrappers to extend
*/
abstract class Wrapper implements File {
abstract class Wrapper implements File, Directory {
/**
* @var resource
*/
@ -25,6 +25,22 @@ abstract class Wrapper implements File {
*/
protected $source;
protected static function wrapSource($source, $context, $protocol, $class) {
try {
stream_wrapper_register($protocol, $class);
if (@rewinddir($source) === false) {
$wrapped = fopen($protocol . '://', 'r+', false, $context);
} else {
$wrapped = opendir($protocol . '://', $context);
}
} catch (\BadMethodCallException $e) {
stream_wrapper_unregister($protocol);
throw $e;
}
stream_wrapper_unregister($protocol);
return $wrapped;
}
/**
* Load the source from the stream context and return the context options
*
@ -107,4 +123,17 @@ abstract class Wrapper implements File {
public function stream_close() {
return fclose($this->source);
}
public function dir_readdir() {
return readdir($this->source);
}
public function dir_closedir() {
closedir($this->source);
return true;
}
public function dir_rewinddir() {
return rewind($this->source);
}
}