mirror of
https://codeberg.org/icewind/streams.git
synced 2026-06-03 16:44:07 +02:00
Add IteratorDirectory to create directory handles from iterators and arrays
This commit is contained in:
parent
b876cef7f2
commit
214a31bc98
3 changed files with 258 additions and 0 deletions
123
src/IteratorDirectory.php
Normal file
123
src/IteratorDirectory.php
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
<?php
|
||||
/**
|
||||
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
|
||||
* This file is licensed under the Licensed under the MIT license:
|
||||
* http://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
namespace Icewind\Streams;
|
||||
|
||||
/**
|
||||
* Create a directory handle from an iterator or array
|
||||
*
|
||||
* The following options should be passed in the context when opening the stream
|
||||
* [
|
||||
* 'dir' => [
|
||||
* 'array' => string[]
|
||||
* 'iterator' => \Iterator
|
||||
* ]
|
||||
* ]
|
||||
*
|
||||
* Either 'array' or 'iterator' need to be set, if both are set, 'iterator' takes preference
|
||||
*/
|
||||
class IteratorDirectory implements Directory {
|
||||
/**
|
||||
* @var resource
|
||||
*/
|
||||
public $context;
|
||||
|
||||
/**
|
||||
* @var \Iterator
|
||||
*/
|
||||
protected $iterator;
|
||||
|
||||
/**
|
||||
* 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');
|
||||
}
|
||||
if (isset($context['iterator']) and $context['iterator'] instanceof \Iterator) {
|
||||
$this->iterator = $context['iterator'];
|
||||
} else if (isset($context['array']) and is_array($context['array'])) {
|
||||
$this->iterator = new \ArrayIterator($context['array']);
|
||||
} else {
|
||||
throw new \BadMethodCallException('Invalid context, iterator or array not set');
|
||||
}
|
||||
return $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param array $options
|
||||
* @return bool
|
||||
*/
|
||||
public function dir_opendir($path, $options) {
|
||||
$this->loadContext('dir');
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function dir_readdir() {
|
||||
if ($this->iterator->valid()) {
|
||||
$result = $this->iterator->current();
|
||||
$this->iterator->next();
|
||||
return $result;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function dir_closedir() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function dir_rewinddir() {
|
||||
$this->iterator->rewind();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a directory handle from the provided array or iterator
|
||||
*
|
||||
* @param \Iterator | array $source
|
||||
* @return resource
|
||||
*
|
||||
* @throws \BadMethodCallException
|
||||
*/
|
||||
public static function wrap($source) {
|
||||
if ($source instanceof \Iterator) {
|
||||
$context = stream_context_create(array(
|
||||
'dir' => array(
|
||||
'iterator' => $source)
|
||||
));
|
||||
} else if (is_array($source)) {
|
||||
$context = stream_context_create(array(
|
||||
'dir' => array(
|
||||
'array' => $source)
|
||||
));
|
||||
} else {
|
||||
throw new \BadMethodCallException('$source should be an Iterator or array');
|
||||
}
|
||||
stream_wrapper_register('iterator', '\Icewind\Streams\IteratorDirectory');
|
||||
$wrapped = opendir('iterator://', $context);
|
||||
stream_wrapper_unregister('iterator');
|
||||
return $wrapped;
|
||||
}
|
||||
}
|
||||
|
|
@ -13,6 +13,11 @@ 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 {
|
||||
/**
|
||||
* @var resource
|
||||
*/
|
||||
public $context;
|
||||
|
||||
/**
|
||||
* The wrapped stream
|
||||
*
|
||||
|
|
|
|||
130
tests/IteratorDirectory.php
Normal file
130
tests/IteratorDirectory.php
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
<?php
|
||||
/**
|
||||
* Copyright (c) 2014 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 IteratorDirectory extends \PHPUnit_Framework_TestCase {
|
||||
|
||||
/**
|
||||
* @param \Iterator | array $source
|
||||
* @return resource
|
||||
*/
|
||||
protected function wrapSource($source) {
|
||||
return \Icewind\Streams\IteratorDirectory::wrap($source);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \BadMethodCallException
|
||||
*/
|
||||
public function testNoContext() {
|
||||
stream_wrapper_register('iterator', '\Icewind\Streams\IteratorDirectory');
|
||||
$context = stream_context_create(array());
|
||||
try {
|
||||
opendir('iterator://', $context);
|
||||
stream_wrapper_unregister('iterator');
|
||||
} catch (\Exception $e) {
|
||||
stream_wrapper_unregister('iterator');
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \BadMethodCallException
|
||||
*/
|
||||
public function testInvalidSource() {
|
||||
stream_wrapper_register('iterator', '\Icewind\Streams\IteratorDirectory');
|
||||
$context = stream_context_create(array(
|
||||
'dir' => array(
|
||||
'array' => 2
|
||||
)
|
||||
));
|
||||
try {
|
||||
opendir('iterator://', $context);
|
||||
stream_wrapper_unregister('iterator');
|
||||
} catch (\Exception $e) {
|
||||
stream_wrapper_unregister('iterator');
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \BadMethodCallException
|
||||
*/
|
||||
public function testWrapInvalidSource() {
|
||||
$this->wrapSource(2);
|
||||
}
|
||||
|
||||
public function fileListProvider() {
|
||||
$longList = array_fill(0, 500, 'foo');
|
||||
return array(
|
||||
array(
|
||||
array(
|
||||
'foo',
|
||||
'bar',
|
||||
'qwerty'
|
||||
)
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'with spaces',
|
||||
'under_scores',
|
||||
'日本語',
|
||||
'character %$_',
|
||||
'.',
|
||||
'0',
|
||||
'double "quotes"',
|
||||
"single 'quotes'"
|
||||
)
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'single item'
|
||||
)
|
||||
),
|
||||
array(
|
||||
$longList
|
||||
),
|
||||
array(
|
||||
array()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
protected function basicTest($fileList, $dh) {
|
||||
$result = array();
|
||||
|
||||
while (($file = readdir($dh)) !== false) {
|
||||
$result[] = $file;
|
||||
}
|
||||
|
||||
$this->assertEquals($fileList, $result);
|
||||
|
||||
rewinddir($dh);
|
||||
if (count($fileList)) {
|
||||
$this->assertEquals($fileList[0], readdir($dh));
|
||||
} else {
|
||||
$this->assertFalse(readdir($dh));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider fileListProvider
|
||||
*/
|
||||
public function testBasicIterator($fileList) {
|
||||
$iterator = new \ArrayIterator($fileList);
|
||||
$dh = $this->wrapSource($iterator);
|
||||
$this->basicTest($fileList, $dh);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider fileListProvider
|
||||
*/
|
||||
public function testBasicArray($fileList) {
|
||||
$dh = $this->wrapSource($fileList);
|
||||
$this->basicTest($fileList, $dh);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue