cleanup context handling for wrappers

This commit is contained in:
Robin Appelman 2019-03-11 16:52:13 +01:00
commit 2c8ae56d02
16 changed files with 182 additions and 258 deletions

View file

@ -20,7 +20,7 @@ namespace Icewind\Streams;
*
* Either 'array' or 'iterator' need to be set, if both are set, 'iterator' takes preference
*/
class IteratorDirectory implements Directory {
class IteratorDirectory extends WrapperHandler implements Directory {
/**
* @var resource
*/
@ -36,15 +36,10 @@ class IteratorDirectory implements Directory {
*
* @param string $name
* @return array
* @throws \Exception
* @throws \BadMethodCallException
*/
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['iterator'])) {
$this->iterator = $context['iterator'];
} else if (isset($context['array'])) {
@ -61,7 +56,7 @@ class IteratorDirectory implements Directory {
* @return bool
*/
public function dir_opendir($path, $options) {
$this->loadContext('dir');
$this->loadContext();
return true;
}
@ -103,21 +98,16 @@ class IteratorDirectory implements Directory {
*/
public static function wrap($source) {
if ($source instanceof \Iterator) {
$context = stream_context_create(array(
'dir' => array(
'iterator' => $source)
));
$options = [
'iterator' => $source
];
} else if (is_array($source)) {
$context = stream_context_create(array(
'dir' => array(
'array' => $source)
));
$options = [
'array' => $source
];
} else {
throw new \BadMethodCallException('$source should be an Iterator or array');
}
stream_wrapper_register('iterator', static::class);
$wrapped = opendir('iterator://', $context);
stream_wrapper_unregister('iterator');
return $wrapped;
return self::wrapSource(self::NO_SOURCE_DIR, $options);
}
}