Skip to content

Commit

Permalink
easier access to source metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
icewind1991 committed Jun 9, 2022
1 parent e4e3221 commit 6e347e4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/Wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected function loadContext($name = null) {

public function stream_seek($offset, $whence = SEEK_SET) {
$result = fseek($this->source, $offset, $whence);
return $result == 0 ? true : false;
return $result == 0;
}

public function stream_tell() {
Expand Down Expand Up @@ -94,8 +94,6 @@ public function stream_eof() {
public function stream_close() {
if (is_resource($this->source)) {
return fclose($this->source);
} else {
return true;
}
}

Expand All @@ -115,4 +113,19 @@ public function dir_rewinddir() {
public function getSource() {
return $this->source;
}

/**
* Retrieves header/metadata from the source stream.
*
* This is equivalent to calling `stream_get_meta_data` on the source stream except nested stream wrappers are handled transparently
*
* @return array
*/
public function getMetaData(): array {
$meta = stream_get_meta_data($this->source);
while (isset($meta['wrapper_data']) && $meta['wrapper_data'] instanceof Wrapper) {
$meta = $meta['wrapper_data']->getMetaData();
}
return $meta;
}
}
13 changes: 13 additions & 0 deletions tests/WrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Icewind\Streams\Tests;

use Icewind\Streams\Wrapper;
use PHPUnit\Framework\TestCase;

abstract class WrapperTest extends TestCase {
Expand Down Expand Up @@ -151,4 +152,16 @@ public function testDoubleClose() {
fclose($wrapped);
$this->assertFalse(is_resource($source));
}

public function testGetMetaData() {
$source = fopen(__FILE__, 'r+');
$sourceMeta = stream_get_meta_data($source);

$wrapped = $this->wrapSource($source);
$wrappedMeta = stream_get_meta_data($wrapped);
$wrapper = $wrappedMeta['wrapper_data'];
$this->assertInstanceOf(Wrapper::class, $wrapper);

$this->assertEquals($sourceMeta, $wrapper->getMetaData());
}
}

0 comments on commit 6e347e4

Please sign in to comment.