Skip to content

Commit

Permalink
add PathWrapper to get a virtual path for an existing file
Browse files Browse the repository at this point in the history
  • Loading branch information
icewind1991 committed Oct 26, 2016
1 parent d3620e8 commit 5981b1b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function wrapPath($path) {

protected function register() {
if (!$this->registered) {
$this->appendDefaultContent($this->getProtocol(), $this->contextOptions);
$this->appendDefaultContent($this->contextOptions);
stream_wrapper_register($this->getProtocol(), $this->class);
$this->registered = true;
}
Expand All @@ -71,13 +71,14 @@ protected function unregister() {
/**
* Add values to the default stream context
*
* @param string $key
* @param array $values
*/
protected function appendDefaultContent($key, $values) {
protected function appendDefaultContent($values) {
$context = stream_context_get_default();
$defaults = stream_context_get_options($context);
$defaults[$key] = $values;
foreach ($values as $key => $value) {
$defaults[$key] = $value;
}
stream_context_set_default($defaults);
}

Expand Down
25 changes: 25 additions & 0 deletions src/PathWrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Copyright (c) 2016 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Licensed under the MIT license:
* http://opensource.org/licenses/MIT
*/

namespace Icewind\Streams;

/**
* A string-like object that maps to an existing stream when opened
*/
class PathWrapper extends NullWrapper {
/**
* @param resource $source
* @return Path|string
*/
public static function getPath($source) {
return new Path(__CLASS__, [
'null' => [
'source' => $source
]
]);
}
}
2 changes: 1 addition & 1 deletion src/Wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected function loadContext($name) {
if (isset($context[$name])) {
$context = $context[$name];
} else {
throw new \BadMethodCallException('Invalid context, "callable" options not set');
throw new \BadMethodCallException('Invalid context, "' . $name . '" options not set');
}
if (isset($context['source']) and is_resource($context['source'])) {
$this->setSourceStream($context['source']);
Expand Down
24 changes: 24 additions & 0 deletions tests/PathWrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Copyright (c) 2016 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 PathWrapper extends \PHPUnit_Framework_TestCase {
private function getDataStream($data) {
$stream = fopen('php://temp', 'w+');
fwrite($stream, $data);
rewind($stream);
return $stream;
}

public function testFileGetContents() {
$data = 'foobar';
$stream = $this->getDataStream($data);
$path = \Icewind\Streams\PathWrapper::getPath($stream);
$this->assertEquals($data, file_get_contents($path));
}
}

0 comments on commit 5981b1b

Please sign in to comment.