]> granicus.if.org Git - php/commitdiff
update directory examples
authorMarcus Boerger <helly@php.net>
Tue, 18 Nov 2003 22:18:38 +0000 (22:18 +0000)
committerMarcus Boerger <helly@php.net>
Tue, 18 Nov 2003 22:18:38 +0000 (22:18 +0000)
ext/spl/examples/cachingiterator.inc [new file with mode: 0644]
ext/spl/examples/cachingrecursiveiterator.inc [new file with mode: 0644]
ext/spl/examples/directorytree.php
ext/spl/examples/directorytreeiterator.inc [new file with mode: 0644]
ext/spl/examples/limititerator.inc
ext/spl/examples/recursiveiterator.inc [new file with mode: 0644]
ext/spl/examples/searchiterator.inc

diff --git a/ext/spl/examples/cachingiterator.inc b/ext/spl/examples/cachingiterator.inc
new file mode 100644 (file)
index 0000000..a9be5d5
--- /dev/null
@@ -0,0 +1,58 @@
+<?php
+
+class CachingIterator
+{
+       protected $it;
+       protected $current;
+       protected $key;
+       protected $more;
+       protected $strvalue;
+
+       function __construct(Iterator $it) {
+               $this->it = $it;
+       }
+
+       function rewind() {
+               $this->it->rewind();
+               $this->next();
+       }
+       
+       function next() {
+               if ($this->more = $this->it->hasMore()) {
+                       $this->current = $this->it->current();
+                       $this->key = $this->it->key();
+                       $this->strvalue = (string)$this->current;
+               } else {
+                       $this->current = NULL;
+                       $this->key = NULL;
+                       $this->strvalue = '';
+               }
+               $this->it->next();
+       }
+       
+       function hasMore() {
+               return $this->more;
+       }
+
+       function hasNext() {
+               return $this->it->hasMore();
+       }
+       
+       function current() {
+               return $this->current;
+       }
+
+       function key() {
+               return $this->key;
+       }
+
+       function __call($func, $params) {
+               return call_user_func_array(array($this->it, $func), $params);
+       }
+       
+       function __toString() {
+               return $this->strvalue;
+       }
+}
+
+?>
\ No newline at end of file
diff --git a/ext/spl/examples/cachingrecursiveiterator.inc b/ext/spl/examples/cachingrecursiveiterator.inc
new file mode 100644 (file)
index 0000000..cb89fdb
--- /dev/null
@@ -0,0 +1,30 @@
+<?php
+
+class CachingRecursiveIterator extends CachingIterator implements RecursiveIterator
+{
+       protected $hasChildren;
+       protected $getChildren;
+
+       function __construct(RecursiveIterator $it) {
+               parent::__construct($it);
+       }
+       
+       function next() {
+               if ($this->hasChildren = $this->it->hasChildren()) {
+                       $this->getChildren = new CachingRecursiveIterator($this->it->getChildren());
+               } else {
+                       $this->getChildren = NULL;
+               }
+               parent::next();
+       }
+
+       function hasChildren() {
+               return $this->hasChildren;
+       }
+
+       function getChildren() {
+               return $this->getChildren;
+       }
+}
+
+?>
\ No newline at end of file
index a0cd1d9a0c68dde7bcb313543f05f8cc6ab01dda..a5d5d994902a48478054365cf6d11426d2399d9e 100755 (executable)
@@ -11,8 +11,8 @@
 
 $length = $argc > 3 ? $argv[3] : NULL;
 
-foreach(new RecursiveIteratorIterator(new DirectoryTreeIterator($argv[1])) as $pathname => $file) {
-       echo "$pathname\n";
+foreach(new LimitIterator(new DirectoryTreeIterator($argv[1]), @$argv[2], $length) as $pathname => $file) {
+       echo "$file\n";
 }
 
 ?>
\ No newline at end of file
diff --git a/ext/spl/examples/directorytreeiterator.inc b/ext/spl/examples/directorytreeiterator.inc
new file mode 100644 (file)
index 0000000..3059cc6
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+
+class DirectoryTreeIterator extends RecursiveIteratorIterator
+{
+       function __construct($path)
+       {
+               parent::__construct(new CachingRecursiveIterator(new RecursiveDirectoryIterator($path)), 1);
+       }
+       
+       function current()
+       {
+               $tree = '';
+               for ($l=0; $l < $this->getLevel(); $l++) {
+                       $tree .= $this->getSubIterator($l)->hasMore() ? '| ' : '  ';
+               }
+               return $tree . ($this->getSubIterator($l)->hasNext() ? '|-' : '\-') 
+                      . $this->getSubIterator($l);
+       }
+}
+
+?>
\ No newline at end of file
index cdc71e98ce61264149ed7ce211dcab28b80d97df..1b0e927a2ea6be977584aed666853abc8ce49730 100755 (executable)
@@ -21,7 +21,7 @@ class LimitIterator implements Iterator
        {
                $this->it->rewind();
                $this->index = 0;
-               if (is_a($this->it, 'SeekableIterator')) {
+               if ($this->it instanceof SeekableIterator) {
                        $this->index = $this->it->seek($this->offset);
                } else {
                        while($this->index < $this->offset && $this->it->hasMore()) {
diff --git a/ext/spl/examples/recursiveiterator.inc b/ext/spl/examples/recursiveiterator.inc
new file mode 100644 (file)
index 0000000..63523ff
--- /dev/null
@@ -0,0 +1,9 @@
+<?php
+
+interface RecursiveIterator implements Iterator
+{
+       function hasChildren();
+       function getChildren();
+}
+
+?>
\ No newline at end of file
index 42c249be9b7b029217138df922378e4442dd8857..1ce5a2eebd7de70ab0bd948e9d4f5f5a4a0d044a 100755 (executable)
@@ -4,6 +4,11 @@ abstract class SearchIterator extends FilterIterator
 {
        private $done = false;
 
+       function rewind() {
+               parent::rewind();
+               $this->done = false;
+       }
+
        function hasMore() {
                return !$this->done && parent::hasMore();
        }