handle seeks in countwrapper

This commit is contained in:
Robin Appelman 2024-12-05 15:32:45 +01:00
commit a0b84d362c
2 changed files with 29 additions and 0 deletions

View file

@ -60,6 +60,17 @@ class CountWrapper extends Wrapper {
return true; return true;
} }
public function stream_seek($offset, $whence = SEEK_SET) {
if ($whence === SEEK_SET) {
$this->readCount = $offset;
$this->writeCount = $offset;
} else if ($whence === SEEK_CUR) {
$this->readCount += $offset;
$this->writeCount += $offset;
}
return parent::stream_seek($offset, $whence);
}
public function dir_opendir($path, $options) { public function dir_opendir($path, $options) {
return $this->open(); return $this->open();
} }

View file

@ -46,4 +46,22 @@ class CountWrapperTest extends WrapperTest {
fclose($wrapped); fclose($wrapped);
$this->assertSame(6, $count); $this->assertSame(6, $count);
} }
public function testReadCountSeek() {
$count = 0;
$source = fopen('php://temp', 'r+');
fwrite($source, 'foobar');
rewind($source);
$wrapped = CountWrapper::wrap($source, function ($readCount) use (&$count) {
$count = $readCount;
});
stream_get_contents($wrapped);
fseek($wrapped, 3);
stream_get_contents($wrapped);
fclose($wrapped);
$this->assertSame(6, $count);
}
} }