diff options
author | Marcus Boerger <helly@php.net> | 2004-04-25 13:06:15 +0000 |
---|---|---|
committer | Marcus Boerger <helly@php.net> | 2004-04-25 13:06:15 +0000 |
commit | e058626c6669b112b03084e74f1bac49ce569c97 (patch) | |
tree | b69368d57d5eb5d193c71d013e7ee6bf05a2ef60 /ext/spl/examples | |
parent | 9238d1bbbc6d725a6b9d63f6f3ac4d8a3eb94e9e (diff) | |
download | php-git-e058626c6669b112b03084e74f1bac49ce569c97.tar.gz |
Add new iterator example AppendIterator and use it in findfile.php example.
# The initial idea came from a request by Sebastian
Diffstat (limited to 'ext/spl/examples')
-rwxr-xr-x | ext/spl/examples/appenditerator.inc | 60 | ||||
-rwxr-xr-x | ext/spl/examples/findfile.inc | 11 | ||||
-rwxr-xr-x | ext/spl/examples/findfile.php | 7 |
3 files changed, 74 insertions, 4 deletions
diff --git a/ext/spl/examples/appenditerator.inc b/ext/spl/examples/appenditerator.inc new file mode 100755 index 0000000000..3e04019a60 --- /dev/null +++ b/ext/spl/examples/appenditerator.inc @@ -0,0 +1,60 @@ +<?php + +/** + * @brief Iterator that iterates over several iterators one after the other + * @author Marcus Boerger + * @version 1.0 + * + */ +class AppendIterator implements Iterator +{ + protected $iterators; + + function __construct() + { + $this->iterators = new ArrayIterator(); + } + + function append(Iterator $it) + { + $this->iterators->append($it); + } + + function getInnerIterator() + { + return $this->iterators->current(); + } + + function rewind() + { + $this->iterators->rewind(); + } + + function valid() + { + return $this->iterators->valid() && $this->getInnerIterator()->valid(); + } + + function current() + { + return $this->getInnerIterator()->current(); + } + + function key() + { + return $this->getInnerIterator()->key(); + } + + function next() + { + while($this->iterators->valid()) { + $this->getInnerIterator()->next(); + if ($this->valid()) { + return; + } + $this->iterators->next(); + } + } +} + +?>
\ No newline at end of file diff --git a/ext/spl/examples/findfile.inc b/ext/spl/examples/findfile.inc index e0c685b659..c4bce73a30 100755 --- a/ext/spl/examples/findfile.inc +++ b/ext/spl/examples/findfile.inc @@ -13,7 +13,16 @@ class FindFile extends FilterIterator function __construct($path, $file) { $this->file = $file; - parent::__construct(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path))); + $list = split(';', $path); + if (count($list) <= 1) { + parent::__construct(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path))); + } else { + $it = new AppendIterator(); + foreach($list as $path) { + $it->append(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path))); + } + parent::__construct($it); + } } function accept() diff --git a/ext/spl/examples/findfile.php b/ext/spl/examples/findfile.php index 0622c4e3d7..58f53764cf 100755 --- a/ext/spl/examples/findfile.php +++ b/ext/spl/examples/findfile.php @@ -4,15 +4,16 @@ * * Usage: php findfile.php <path> <name> * - * <path> Path to search in. + * <path> Path to search in. You can specify multiple paths by separating + * them with ';'. * <name> Filename to look for. * - * (c) Marcus Boerger, 2003 + * (c) Marcus Boerger, 2003 - 2004 */ if ($argc < 3) { echo <<<EOF -Usage: php findfile.php <file> <name> +Usage: php findfile.php <path> <name> Find a specific file by name. |