Skip to content

Commit

Permalink
Merge pull request #9 from rullzer/enh/add_preClose_callback
Browse files Browse the repository at this point in the history
Add a preClose callback
  • Loading branch information
icewind1991 authored Feb 14, 2019
2 parents 74d0347 + 3a28b61 commit e1b5ac1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
16 changes: 14 additions & 2 deletions src/CallbackWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ class CallbackWrapper extends Wrapper {
*/
protected $readDirCallBack;

/**
* @var callable
*/
protected $preCloseCallback;

/**
* Wraps a stream with the provided callbacks
*
Expand All @@ -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');
Expand All @@ -76,6 +82,7 @@ protected function open() {
$this->writeCallback = $context['write'];
$this->closeCallback = $context['close'];
$this->readDirCallBack = $context['readDir'];
$this->preCloseCallback = $context['preClose'];
return true;
}

Expand Down Expand Up @@ -104,6 +111,11 @@ public function stream_write($data) {
}

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);
Expand Down
22 changes: 20 additions & 2 deletions tests/CallbackWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ class CallbackWrapperTest extends WrapperTest {
* @param callable $write
* @param callable $close
* @param callable $readDir
* @param callable $preClose
* @return resource
*/
protected function wrapSource($source, $read = null, $write = null, $close = null, $readDir = null) {
return \Icewind\Streams\CallbackWrapper::wrap($source, $read, $write, $close, $readDir);
protected function wrapSource($source, $read = null, $write = null, $close = null, $readDir = null, $preClose = null) {
return \Icewind\Streams\CallbackWrapper::wrap($source, $read, $write, $close, $readDir, $preClose);
}

/**
Expand Down Expand Up @@ -83,4 +84,21 @@ public function testReadDirCallback() {
readdir($wrapped);
$this->assertTrue($called);
}

public function testPreCloseCallback() {
$called = false;

$source = fopen('php://temp', 'r+');
fwrite($source, 'foobar');
rewind($source);

$callBack = function ($stream) use (&$called, $source) {
$called = true;
$this->assertSame($stream, $source);
};

$wrapped = $this->wrapSource($source, null, null, null, null, $callBack);
fclose($wrapped);
$this->assertTrue($called);
}
}

0 comments on commit e1b5ac1

Please sign in to comment.