]> granicus.if.org Git - php/commitdiff
Add new iterator example AppendIterator and use it in findfile.php example.
authorMarcus Boerger <helly@php.net>
Sun, 25 Apr 2004 13:06:15 +0000 (13:06 +0000)
committerMarcus Boerger <helly@php.net>
Sun, 25 Apr 2004 13:06:15 +0000 (13:06 +0000)
# The initial idea came from a request by Sebastian

ext/spl/examples/appenditerator.inc [new file with mode: 0755]
ext/spl/examples/findfile.inc
ext/spl/examples/findfile.php

diff --git a/ext/spl/examples/appenditerator.inc b/ext/spl/examples/appenditerator.inc
new file mode 100755 (executable)
index 0000000..3e04019
--- /dev/null
@@ -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
index e0c685b659bc193fb1caece8527f137d539909f6..c4bce73a30d00ca52a0fc7152958959a99ebd835 100755 (executable)
@@ -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()
index 0622c4e3d77eaf3a25886ffdacc5511ba349b24f..58f53764cf42a77e7bd76450e8ae37e67845594b 100755 (executable)
@@ -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.