mirror of
https://codeberg.org/icewind/streams.git
synced 2026-06-03 08:34:09 +02:00
Add seekable wrapper to make non seekable streams seekable
This commit is contained in:
parent
1cca87c499
commit
d31237bc40
2 changed files with 144 additions and 0 deletions
92
src/SeekableWrapper.php
Normal file
92
src/SeekableWrapper.php
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
<?php
|
||||
/**
|
||||
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
|
||||
* This file is licensed under the Licensed under the MIT license:
|
||||
* http://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
namespace Icewind\Streams;
|
||||
|
||||
/**
|
||||
* Wrapper that provides callbacks for write, read and close
|
||||
*
|
||||
* The following options should be passed in the context when opening the stream
|
||||
* [
|
||||
* 'callback' => [
|
||||
* 'source' => resource
|
||||
* ]
|
||||
* ]
|
||||
*
|
||||
* All callbacks are called after the operation is executed on the source stream
|
||||
*/
|
||||
class SeekableWrapper extends Wrapper {
|
||||
/**
|
||||
* @var resource
|
||||
*/
|
||||
protected $cache;
|
||||
|
||||
/**
|
||||
* Wraps a stream to make it seekable
|
||||
*
|
||||
* @param resource $source
|
||||
* @return resource
|
||||
*
|
||||
* @throws \BadMethodCallException
|
||||
*/
|
||||
public static function wrap($source) {
|
||||
$context = stream_context_create(array(
|
||||
'callback' => array(
|
||||
'source' => $source
|
||||
)
|
||||
));
|
||||
return Wrapper::wrapSource($source, $context, 'callback', '\Icewind\Streams\SeekableWrapper');
|
||||
}
|
||||
|
||||
public function dir_opendir($path, $options) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public function stream_open($path, $mode, $options, &$opened_path) {
|
||||
$this->loadContext('callback');
|
||||
$this->cache = fopen('php://temp', 'w+');
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function readTill($position) {
|
||||
$current = ftell($this->source);
|
||||
if ($position > $current) {
|
||||
$data = parent::stream_read($position - $current);
|
||||
$cachePosition = ftell($this->cache);
|
||||
fseek($this->cache, $current);
|
||||
fwrite($this->cache, $data);
|
||||
fseek($this->cache, $cachePosition);
|
||||
}
|
||||
}
|
||||
|
||||
public function stream_read($count) {
|
||||
$current = ftell($this->cache);
|
||||
$this->readTill($current + $count);
|
||||
return fread($this->cache, $count);
|
||||
}
|
||||
|
||||
public function stream_seek($offset, $whence = SEEK_SET) {
|
||||
if ($whence === SEEK_SET) {
|
||||
$target = $offset;
|
||||
} else if ($whence === SEEK_CUR) {
|
||||
$current = ftell($this->cache);
|
||||
$target = $current + $offset;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
$this->readTill($target);
|
||||
return fseek($this->cache, $target) === 0;
|
||||
}
|
||||
|
||||
public function stream_tell() {
|
||||
return ftell($this->cache);
|
||||
}
|
||||
|
||||
public function stream_eof() {
|
||||
return parent::stream_eof() and (ftell($this->source) === ftell($this->cache));
|
||||
}
|
||||
}
|
||||
52
tests/SeekableWrapper.php
Normal file
52
tests/SeekableWrapper.php
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
/**
|
||||
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
|
||||
* This file is licensed under the Licensed under the MIT license:
|
||||
* http://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
namespace Icewind\Streams\Tests;
|
||||
|
||||
class SeekableWrapper extends \PHPUnit_Framework_TestCase {
|
||||
/**
|
||||
* @param resource $source
|
||||
* @return resource
|
||||
*/
|
||||
protected function wrapSource($source) {
|
||||
return \Icewind\Streams\SeekableWrapper::wrap($source);
|
||||
}
|
||||
|
||||
protected function getSource() {
|
||||
$source = fopen('php://temp', 'w+');
|
||||
fwrite($source, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.');
|
||||
fseek($source, 0);
|
||||
return $source;
|
||||
}
|
||||
|
||||
public function testCantWrapDir() {
|
||||
$source = opendir(__DIR__);
|
||||
$this->assertFalse(@$this->wrapSource($source));
|
||||
}
|
||||
|
||||
public function testSourceNotSeeked() {
|
||||
$source = $this->getSource();
|
||||
$wrapped = $this->wrapSource($source);
|
||||
fseek($wrapped, 6);
|
||||
$this->assertEquals(6, ftell($source));
|
||||
$this->assertEquals(6, ftell($wrapped));
|
||||
$this->assertEquals('ipsum', fread($wrapped, '5'));
|
||||
fseek($wrapped, 6);
|
||||
$this->assertEquals(6, ftell($wrapped));
|
||||
$this->assertGreaterThan(6, ftell($source));
|
||||
}
|
||||
|
||||
public function testSeekRelative() {
|
||||
$source = $this->getSource();
|
||||
$wrapped = $this->wrapSource($source);
|
||||
fseek($wrapped, 6);
|
||||
fseek($wrapped, 6, SEEK_CUR);
|
||||
$this->assertEquals(12, ftell($source));
|
||||
$this->assertEquals(12, ftell($wrapped));
|
||||
$this->assertEquals('dolor', fread($wrapped, '5'));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue