From: Marcus Boerger Date: Sun, 25 Apr 2004 13:06:15 +0000 (+0000) Subject: Add new iterator example AppendIterator and use it in findfile.php example. X-Git-Tag: RELEASE_0_1~372 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e058626c6669b112b03084e74f1bac49ce569c97;p=php Add new iterator example AppendIterator and use it in findfile.php example. # The initial idea came from a request by Sebastian --- 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 @@ +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 to search in. + * Path to search in. You can specify multiple paths by separating + * them with ';'. * Filename to look for. * - * (c) Marcus Boerger, 2003 + * (c) Marcus Boerger, 2003 - 2004 */ if ($argc < 3) { echo << +Usage: php findfile.php Find a specific file by name.