diff options
Diffstat (limited to 'apps/files_external/3rdparty/icewind/streams/src/CallbackWrapper.php')
-rw-r--r-- | apps/files_external/3rdparty/icewind/streams/src/CallbackWrapper.php | 67 |
1 files changed, 39 insertions, 28 deletions
diff --git a/apps/files_external/3rdparty/icewind/streams/src/CallbackWrapper.php b/apps/files_external/3rdparty/icewind/streams/src/CallbackWrapper.php index fd99aa6ebe8..c5847b95fdb 100644 --- a/apps/files_external/3rdparty/icewind/streams/src/CallbackWrapper.php +++ b/apps/files_external/3rdparty/icewind/streams/src/CallbackWrapper.php @@ -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) * ] * ] * @@ -39,54 +40,56 @@ class CallbackWrapper extends Wrapper { protected $closeCallback; /** + * @var callable + */ + protected $readDirCallBack; + + /** * Wraps a stream with the provided callbacks * * @param resource $source * @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 (isset($context['read']) and is_callable($context['read'])) { - $this->readCallback = $context['read']; - } - if (isset($context['write']) and is_callable($context['write'])) { - $this->writeCallback = $context['write']; - } - if (isset($context['close']) and is_callable($context['close'])) { - $this->closeCallback = $context['close']; - } + $this->readCallback = $context['read']; + $this->writeCallback = $context['write']; + $this->closeCallback = $context['close']; + $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) { + if (is_callable($this->readCallback)) { call_user_func($this->readCallback, $count); } return $result; @@ -94,7 +97,7 @@ class CallbackWrapper extends Wrapper { public function stream_write($data) { $result = parent::stream_write($data); - if ($this->writeCallback) { + if (is_callable($this->writeCallback)) { call_user_func($this->writeCallback, $data); } return $result; @@ -102,9 +105,17 @@ class CallbackWrapper extends Wrapper { public function stream_close() { $result = parent::stream_close(); - if ($this->closeCallback) { + if (is_callable($this->closeCallback)) { call_user_func($this->closeCallback); } return $result; } + + public function dir_readdir() { + $result = parent::dir_readdir(); + if (is_callable($this->readDirCallBack)) { + call_user_func($this->readDirCallBack); + } + return $result; + } } |