add PathWrapper to get a virtual path for an existing file

This commit is contained in:
Robin Appelman 2016-10-26 22:08:04 +02:00
commit 5981b1b39c
4 changed files with 55 additions and 5 deletions

View file

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

25
src/PathWrapper.php Normal file
View file

@ -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
]
]);
}
}

View file

@ -53,7 +53,7 @@ abstract class Wrapper implements File, Directory {
if (isset($context[$name])) { if (isset($context[$name])) {
$context = $context[$name]; $context = $context[$name];
} else { } 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'])) { if (isset($context['source']) and is_resource($context['source'])) {
$this->setSourceStream($context['source']); $this->setSourceStream($context['source']);

24
tests/PathWrapper.php Normal file
View file

@ -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));
}
}