Skip to content

Commit

Permalink
handle seeks in countwrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
icewind1991 committed Dec 5, 2024
1 parent 89a8191 commit a0b84d3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/CountWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ protected function open() {
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) {
return $this->open();
}
Expand Down
18 changes: 18 additions & 0 deletions tests/CountWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,22 @@ public function testWriteCount() {
fclose($wrapped);
$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);
}
}

0 comments on commit a0b84d3

Please sign in to comment.