mirror of
https://codeberg.org/icewind/streams.git
synced 2026-06-03 16:44:07 +02:00
Add directory support to wrappers
This commit is contained in:
parent
448dfc577c
commit
53cfe7f4f1
5 changed files with 121 additions and 28 deletions
|
|
@ -13,10 +13,11 @@ namespace Icewind\Streams;
|
|||
* The following options should be passed in the context when opening the stream
|
||||
* [
|
||||
* 'callback' => [
|
||||
* 'source' => resource
|
||||
* 'read' => function($count){} (optional)
|
||||
* 'write' => function($data){} (optional)
|
||||
* 'close' => function(){} (optional)
|
||||
* 'source' => resource
|
||||
* 'read' => function($count){} (optional)
|
||||
* 'write' => function($data){} (optional)
|
||||
* 'close' => function(){} (optional)
|
||||
* 'readdir' => function(){} (optional)
|
||||
* ]
|
||||
* ]
|
||||
*
|
||||
|
|
@ -38,6 +39,11 @@ class CallbackWrapper extends Wrapper {
|
|||
*/
|
||||
protected $closeCallback;
|
||||
|
||||
/**
|
||||
* @var callable
|
||||
*/
|
||||
protected $readDirCallBack;
|
||||
|
||||
/**
|
||||
* Wraps a stream with the provided callbacks
|
||||
*
|
||||
|
|
@ -45,31 +51,25 @@ class CallbackWrapper extends Wrapper {
|
|||
* @param callable $read (optional)
|
||||
* @param callable $write (optional)
|
||||
* @param callable $close (optional)
|
||||
* @param callable $readDir (optional)
|
||||
* @return resource
|
||||
*
|
||||
* @throws \BadMethodCallException
|
||||
*/
|
||||
public static function wrap($source, $read = null, $write = null, $close = null) {
|
||||
public static function wrap($source, $read = null, $write = null, $close = null, $readDir = null) {
|
||||
$context = stream_context_create(array(
|
||||
'callback' => array(
|
||||
'source' => $source,
|
||||
'read' => $read,
|
||||
'write' => $write,
|
||||
'close' => $close
|
||||
'close' => $close,
|
||||
'readDir' => $readDir
|
||||
)
|
||||
));
|
||||
stream_wrapper_register('callback', '\Icewind\Streams\CallbackWrapper');
|
||||
try {
|
||||
$wrapped = fopen('callback://', 'r+', false, $context);
|
||||
} catch (\BadMethodCallException $e) {
|
||||
stream_wrapper_unregister('callback');
|
||||
throw $e;
|
||||
}
|
||||
stream_wrapper_unregister('callback');
|
||||
return $wrapped;
|
||||
return Wrapper::wrapSource($source, $context, 'callback', '\Icewind\Streams\CallbackWrapper');
|
||||
}
|
||||
|
||||
public function stream_open($path, $mode, $options, &$opened_path) {
|
||||
protected function open() {
|
||||
$context = $this->loadContext('callback');
|
||||
|
||||
if (is_callable($context['read'])) {
|
||||
|
|
@ -81,9 +81,20 @@ class CallbackWrapper extends Wrapper {
|
|||
if (is_callable($context['close'])) {
|
||||
$this->closeCallback = $context['close'];
|
||||
}
|
||||
if (is_callable($context['readDir'])) {
|
||||
$this->readDirCallBack = $context['readDir'];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function dir_opendir($path, $options) {
|
||||
return $this->open();
|
||||
}
|
||||
|
||||
public function stream_open($path, $mode, $options, &$opened_path) {
|
||||
return $this->open();
|
||||
}
|
||||
|
||||
public function stream_read($count) {
|
||||
$result = parent::stream_read($count);
|
||||
if ($this->readCallback) {
|
||||
|
|
@ -107,4 +118,12 @@ class CallbackWrapper extends Wrapper {
|
|||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function dir_readdir() {
|
||||
$result = parent::dir_readdir();
|
||||
if ($this->readDirCallBack) {
|
||||
call_user_func($this->readDirCallBack);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue