mirror of
https://codeberg.org/icewind/streams.git
synced 2026-06-03 16:44:07 +02:00
cleanup context handling for wrappers
This commit is contained in:
parent
3171a535de
commit
2c8ae56d02
16 changed files with 182 additions and 258 deletions
|
|
@ -12,7 +12,7 @@ namespace Icewind\Streams;
|
|||
*
|
||||
* This wrapper itself doesn't implement any functionality but is just a base class for other wrappers to extend
|
||||
*/
|
||||
abstract class Wrapper implements File, Directory {
|
||||
abstract class Wrapper extends WrapperHandler implements File, Directory {
|
||||
/**
|
||||
* @var resource
|
||||
*/
|
||||
|
|
@ -26,55 +26,14 @@ abstract class Wrapper implements File, Directory {
|
|||
protected $source;
|
||||
|
||||
/**
|
||||
* @param $source
|
||||
* @param $context
|
||||
* @param null $protocol deprecated, protocol is now automatically generated
|
||||
* @param null $class deprecated, class is now automatically generated
|
||||
* @return bool|resource
|
||||
* @param resource $source
|
||||
*/
|
||||
protected static function wrapSource($source, $context, $protocol = null, $class = null) {
|
||||
if ($class === null) {
|
||||
$class = static::class;
|
||||
}
|
||||
$parts = explode('\\', $class);
|
||||
$protocol = strtolower(array_pop($parts));
|
||||
if (!is_resource($source)) {
|
||||
throw new \BadMethodCallException();
|
||||
}
|
||||
try {
|
||||
stream_wrapper_register($protocol, $class);
|
||||
if (self::isDirectoryHandle($source)) {
|
||||
$wrapped = opendir($protocol . '://', $context);
|
||||
} else {
|
||||
$wrapped = fopen($protocol . '://', 'r+', false, $context);
|
||||
}
|
||||
} catch (\BadMethodCallException $e) {
|
||||
stream_wrapper_unregister($protocol);
|
||||
throw $e;
|
||||
}
|
||||
stream_wrapper_unregister($protocol);
|
||||
return $wrapped;
|
||||
protected function setSourceStream($source) {
|
||||
$this->source = $source;
|
||||
}
|
||||
|
||||
protected static function isDirectoryHandle($resource) {
|
||||
$meta = stream_get_meta_data($resource);
|
||||
return $meta['stream_type'] == 'dir';
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the source from the stream context and return the context options
|
||||
*
|
||||
* @param string $name
|
||||
* @return array
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function loadContext($name) {
|
||||
$context = stream_context_get_options($this->context);
|
||||
if (isset($context[$name])) {
|
||||
$context = $context[$name];
|
||||
} else {
|
||||
throw new \BadMethodCallException('Invalid context, "' . $name . '" options not set');
|
||||
}
|
||||
protected function loadContext($name = null) {
|
||||
$context = parent::loadContext($name);
|
||||
if (isset($context['source']) and is_resource($context['source'])) {
|
||||
$this->setSourceStream($context['source']);
|
||||
} else {
|
||||
|
|
@ -83,13 +42,6 @@ abstract class Wrapper implements File, Directory {
|
|||
return $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param resource $source
|
||||
*/
|
||||
protected function setSourceStream($source) {
|
||||
$this->source = $source;
|
||||
}
|
||||
|
||||
public function stream_seek($offset, $whence = SEEK_SET) {
|
||||
$result = fseek($this->source, $offset, $whence);
|
||||
return $result == 0 ? true : false;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue