mirror of
https://codeberg.org/icewind/streams.git
synced 2026-06-04 00:54:08 +02:00
Add a preClose callback
This is useful in case a callback wants to do something with the stream right before it gets closed. Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
parent
74d0347f39
commit
3c98a7556b
1 changed files with 14 additions and 2 deletions
|
|
@ -44,6 +44,11 @@ class CallbackWrapper extends Wrapper {
|
|||
*/
|
||||
protected $readDirCallBack;
|
||||
|
||||
/**
|
||||
* @var callable
|
||||
*/
|
||||
protected $preCloseCallback;
|
||||
|
||||
/**
|
||||
* Wraps a stream with the provided callbacks
|
||||
*
|
||||
|
|
@ -56,14 +61,15 @@ class CallbackWrapper extends Wrapper {
|
|||
*
|
||||
* @throws \BadMethodCallException
|
||||
*/
|
||||
public static function wrap($source, $read = null, $write = null, $close = null, $readDir = null) {
|
||||
public static function wrap($source, $read = null, $write = null, $close = null, $readDir = null, $preClose = null) {
|
||||
$context = stream_context_create(array(
|
||||
'callback' => array(
|
||||
'source' => $source,
|
||||
'read' => $read,
|
||||
'write' => $write,
|
||||
'close' => $close,
|
||||
'readDir' => $readDir
|
||||
'readDir' => $readDir,
|
||||
'preClose' => $preClose,
|
||||
)
|
||||
));
|
||||
return Wrapper::wrapSource($source, $context, 'callback', '\Icewind\Streams\CallbackWrapper');
|
||||
|
|
@ -76,6 +82,7 @@ class CallbackWrapper extends Wrapper {
|
|||
$this->writeCallback = $context['write'];
|
||||
$this->closeCallback = $context['close'];
|
||||
$this->readDirCallBack = $context['readDir'];
|
||||
$this->preCloseCallback = $context['preClose'];
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -104,6 +111,11 @@ class CallbackWrapper extends Wrapper {
|
|||
}
|
||||
|
||||
public function stream_close() {
|
||||
if (is_callable($this->preCloseCallback)) {
|
||||
call_user_func($this->preCloseCallback, $this->loadContext('callback')['source']);
|
||||
// prevent further calls by potential PHP 7 GC ghosts
|
||||
$this->preCloseCallback = null;
|
||||
}
|
||||
$result = parent::stream_close();
|
||||
if (is_callable($this->closeCallback)) {
|
||||
call_user_func($this->closeCallback);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue