mirror of
https://codeberg.org/icewind/streams.git
synced 2026-06-03 16:44:07 +02:00
minor cleanup
This commit is contained in:
parent
782e09ec62
commit
a91efa8520
1 changed files with 16 additions and 15 deletions
|
|
@ -39,6 +39,15 @@ class WrapperHandler {
|
|||
return strtolower(array_pop($parts));
|
||||
}
|
||||
|
||||
private static function buildContext($protocol, $context, $source) {
|
||||
if (is_array($context)) {
|
||||
$context['source'] = $source;
|
||||
return stream_context_create([$protocol => $context]);
|
||||
} else {
|
||||
return $context;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param resource|int $source
|
||||
* @param resource|array $context
|
||||
|
|
@ -52,34 +61,26 @@ class WrapperHandler {
|
|||
}
|
||||
|
||||
$protocol = self::getProtocol($class);
|
||||
|
||||
if (is_array($context)) {
|
||||
$context['source'] = $source;
|
||||
$context = stream_context_create([$protocol => $context]);
|
||||
}
|
||||
|
||||
if ($source !== self::NO_SOURCE_DIR && !is_resource($source)) {
|
||||
throw new \BadMethodCallException();
|
||||
}
|
||||
$context = self::buildContext($protocol, $context, $source);
|
||||
try {
|
||||
stream_wrapper_register($protocol, $class);
|
||||
if (self::isDirectoryHandle($source)) {
|
||||
$wrapped = opendir($protocol . '://', $context);
|
||||
return opendir($protocol . '://', $context);
|
||||
} else {
|
||||
$wrapped = fopen($protocol . '://', 'r+', false, $context);
|
||||
return fopen($protocol . '://', 'r+', false, $context);
|
||||
}
|
||||
} catch (\BadMethodCallException $e) {
|
||||
} finally {
|
||||
stream_wrapper_unregister($protocol);
|
||||
throw $e;
|
||||
}
|
||||
stream_wrapper_unregister($protocol);
|
||||
return $wrapped;
|
||||
}
|
||||
|
||||
protected static function isDirectoryHandle($resource) {
|
||||
if ($resource === self::NO_SOURCE_DIR) {
|
||||
return true;
|
||||
}
|
||||
if (!is_resource($resource)) {
|
||||
throw new \BadMethodCallException('Invalid stream source');
|
||||
}
|
||||
$meta = stream_get_meta_data($resource);
|
||||
return $meta['stream_type'] === 'dir' || $meta['stream_type'] === 'user-space-dir';
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue