]> granicus.if.org Git - php/commitdiff
Update examples
authorMarcus Boerger <helly@php.net>
Sat, 22 Nov 2003 20:51:15 +0000 (20:51 +0000)
committerMarcus Boerger <helly@php.net>
Sat, 22 Nov 2003 20:51:15 +0000 (20:51 +0000)
ext/spl/examples/cachingiterator.inc
ext/spl/examples/cachingrecursiveiterator.inc
ext/spl/examples/dba_array.php
ext/spl/examples/dba_dump.php
ext/spl/examples/dba_reader.inc
ext/spl/examples/directorytree.php
ext/spl/examples/directorytreeiterator.inc
ext/spl/examples/findfile.php
ext/spl/examples/key_filter.inc
ext/spl/examples/recursiveiteratoriterator.inc
ext/spl/examples/tree.php

index a9be5d5d848c3747de8a4c0251855f5fd2dffaf4..9146ceeaf64a2f77841d79c0e70992813f40ad51 100644 (file)
@@ -8,16 +8,19 @@ class CachingIterator
        protected $more;
        protected $strvalue;
 
-       function __construct(Iterator $it) {
+       function __construct(Iterator $it)
+       {
                $this->it = $it;
        }
 
-       function rewind() {
+       function rewind()
+       {
                $this->it->rewind();
                $this->next();
        }
        
-       function next() {
+       function next()
+       {
                if ($this->more = $this->it->hasMore()) {
                        $this->current = $this->it->current();
                        $this->key = $this->it->key();
@@ -30,27 +33,33 @@ class CachingIterator
                $this->it->next();
        }
        
-       function hasMore() {
+       function hasMore()
+       {
                return $this->more;
        }
 
-       function hasNext() {
+       function hasNext()
+       {
                return $this->it->hasMore();
        }
        
-       function current() {
+       function current()
+       {
                return $this->current;
        }
 
-       function key() {
+       function key()
+       {
                return $this->key;
        }
 
-       function __call($func, $params) {
+       function __call($func, $params)
+       {
                return call_user_func_array(array($this->it, $func), $params);
        }
        
-       function __toString() {
+       function __toString()
+       {
                return $this->strvalue;
        }
 }
index 55acd1e1956af9400595e7fbcd897ef12ca8b809..59709a75222297201f840740a9c06cf3b4417d23 100644 (file)
@@ -4,23 +4,25 @@ class CachingRecursiveIterator extends CachingIterator implements RecursiveItera
 {
        protected $hasChildren;
        protected $getChildren;
-       protected $catch_get_child_exceptions;
+       protected $catch_get_child;
 
-       function __construct(RecursiveIterator $it, $catch_get_child_exceptions = false) {
-               $this->catch_get_child_exceptions = $catch_get_child_exceptions;
+       function __construct(RecursiveIterator $it, $catch_get_child = false)
+       {
+               $this->catch_get_child = $catch_get_child;
                parent::__construct($it);
        }
        
-       function next() {
+       function next()
+       {
                if ($this->hasChildren = $this->it->hasChildren()) {
                        try {
-                               //$this->getChildren = new CachingRecursiveIterator($this->it->getChildren(), $this->catch_get_child_exceptions);
+                               //$this->getChildren = new CachingRecursiveIterator($this->it->getChildren(), $this->catch_get_child);
                                // workaround memleaks...
                                $child = $this->it->getChildren();
-                               $this->getChildren = new CachingRecursiveIterator($child, $this->catch_get_child_exceptions);
+                               $this->getChildren = new CachingRecursiveIterator($child, $this->catch_get_child);
                        }
                        catch(Exception $e) {
-                               if (!$this->catch_get_child_exceptions) {
+                               if (!$this->catch_get_child) {
                                        throw $e;
                                }
                                $this->hasChildren = false;
@@ -32,11 +34,13 @@ class CachingRecursiveIterator extends CachingIterator implements RecursiveItera
                parent::next();
        }
 
-       function hasChildren() {
+       function hasChildren()
+       {
                return $this->hasChildren;
        }
 
-       function getChildren() {
+       function getChildren()
+       {
                return $this->getChildren;
        }
 }
index ebbe5a7bac4db1d4d5a3527c820960d3dfcdb280..bd73ce2a0d356526a88af797ed7516b69a697ec1 100755 (executable)
@@ -12,7 +12,7 @@
  * (c) Marcus Boerger
  */
 
-class dba_array implements spl_array_access {
+class DbaArray implements ArrayAccess {
        private $db;
 
        function __construct($file, $handler)
@@ -35,7 +35,8 @@ class dba_array implements spl_array_access {
                        if (ini_get('magic_quotes_runtime')) {
                                $data = stripslashes($data);
                        }
-                       return unserialize($data);
+                       //return unserialize($data);
+                       return $data;
                }
                else 
                {
@@ -45,7 +46,8 @@ class dba_array implements spl_array_access {
 
        function set($name, $value)
        {
-               dba_replace($name, serialize($value), $this->db);
+               //dba_replace($name, serialize($value), $this->db);
+               dba_replace($name, $value, $this->db);
                return $value;
        }
 
@@ -53,18 +55,23 @@ class dba_array implements spl_array_access {
        {
                return dba_exists($name, $this->db);
        }
+
+       function del($name)
+       {
+               return dba_delete($name, $this->db);
+       }
 }
 
 try {
        if ($argc > 2) {
-               $dba = new dba_array($argv[1], $argv[2]);
+               $dba = new DbaArray($argv[1], $argv[2]);
                if ($dba && $argc > 3) {
                        if ($argc > 4) {
                                $dba[$argv[3]] = $argv[4];
                        }
                        var_dump(array('Index' => $argv[3], 'Value' => $dba[$argv[3]]));
                }
-               $dba = NULL;
+               unset($dba);
        }
        else
        {
index 77ea2008bd27afaccfb7533dc7b043949d2649c0..04efdf4d88677b8b4915418c36113228dcd8be11 100755 (executable)
 require_once("dba_reader.inc");
 require_once("key_filter.inc");
 
-$db = new dba_reader($argv[1], $argv[2]);
+$db = new DbaReader($argv[1], $argv[2]);
 
 if ($argc>3) {
-       $db = new key_filter($db, $argv[3]);
+       $db = new keyFilter($db, $argv[3]);
 }
 
 foreach($db as $key => $val) {
index b8c7365f97a95a3bde8eba7f2a534207a67403b3..d21db456139da77e3960b52cb823a0c6c589f89e 100755 (executable)
@@ -1,11 +1,11 @@
 <?php
 
 /**
- * @brief   This implements an dba iterator.
+ * @brief   This implements a Dba Iterator.
  * @author  Marcus Boerger
  * @version 1.0
  */
-class dba_reader implements spl_sequence_assoc
+class DbaReader implements Iterator
 {
 
        private $db = NULL;
index a5d5d994902a48478054365cf6d11426d2399d9e..e22244a9e1149672bed013e9986a1a499cf065c7 100755 (executable)
@@ -11,8 +11,8 @@
 
 $length = $argc > 3 ? $argv[3] : NULL;
 
-foreach(new LimitIterator(new DirectoryTreeIterator($argv[1]), @$argv[2], $length) as $pathname => $file) {
-       echo "$file\n";
+foreach(new LimitIterator(new DirectoryTreeIterator($argv[1]), @$argv[2], $length) as $file) {
+       echo $file ."\n";
 }
 
 ?>
\ No newline at end of file
index bd815b6821d93edb02df1e521974bc1c595ed854..b247e707010c4f47f9d8a680917b9d52827f9de8 100644 (file)
@@ -10,12 +10,17 @@ class DirectoryTreeIterator extends RecursiveIteratorIterator
        function current()
        {
                $tree = '';
-               for ($l=0; $l < $this->getLevel(); $l++) {
+               for ($l=0; $l < $this->getDepth(); $l++) {
                        $tree .= $this->getSubIterator($l)->hasNext() ? '| ' : '  ';
                }
                return $tree . ($this->getSubIterator($l)->hasNext() ? '|-' : '\-') 
                       . $this->getSubIterator($l);
        }
+       
+       function __call($func, $params)
+       {
+               return call_user_func_array(array($this->getSubIterator(), $func), $params);
+       }
 }
 
 ?>
\ No newline at end of file
index 003111e272aeaa32396cf1c3c760bf9606dee703..dc23c5a0320de804cc5b26617fa92c46d5cfbd33 100755 (executable)
@@ -6,7 +6,7 @@ class FindFile extends SearchIterator
 
        function __construct($path, $file) {
                $this->file = $file;
-               parent::__construct(new DirectoryTree($path));
+               parent::__construct(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)));
        }
        function accept() {
                return !strcmp($this->it->current(), $this->file);
index 47ceed2246c6e1e5f847e18661c0f8ce4e6e5847..6dfd19c85b390e238ee4ebe3f1f1b8e24b25af90 100755 (executable)
@@ -10,7 +10,7 @@
  * and the instance will only return elements which match the given regular 
  * expression.
  */
-class key_filter implements spl_forward_assoc
+class KeyFilter implements Iterator
 {
        protected $it;
        protected $regex;
@@ -25,14 +25,18 @@ class key_filter implements spl_forward_assoc
         * @param it     Object that implements at least spl_forward
         * @patam regex  Regular expression used as a filter.
         */
-       function __construct(spl_forward $it, $regex) {
-               if ($it instanceof spl_sequence) {
-                       $it->rewind();
-               }
+       function __construct(Iterator $it, $regex) {
                $this->it = $it;
                $this->regex = $regex;
                $this->fetch();
        }
+
+       /**
+        * Rewind input iterator
+        */
+       function rewind() {
+               $this->it->rewind();
+       }
        
        /**
         * Destruct the iterator.
index 95d52d7f7626fee313ce8fc21cb51a03dbfc4507..7d151f9e9fe33d2b0a6f08983414c7de1bf3f211 100755 (executable)
@@ -11,44 +11,50 @@ class RecursiveIteratorIterator implements Iterator
        protected $ait = array();
        protected $count = 0;
 
-       function __construct(RecursiveIterator $it) {
-               $this->count = 1;
+       function __construct(RecursiveIterator $it)
+       {
                $this->ait[0] = $it;
        }
 
 
-       function rewind() {
-               while ($this->count > 1) {
-                       unset($this->ait[--$this->count]);
+       function rewind()
+       {
+               while ($this->count) {
+                       unset($this->ait[$this->count--]);
                }
                $this->ait[0]->rewind();
                $this->ait[0]->recursed = false;
        }
        
-       function hasMore() {
+       function hasMore()
+       {
                $count = $this->count;
-               while ($count--) {
+               while ($count) {
                        $it = $this->ait[$count];
-                       if ($it->hasMore()) {// || (!$it->recursed && $it->isRecursive())) {
+                       if ($it->hasMore()) {
                                return true;
                        }
+                       $count--;
                }
                return false;
        }
        
-       function key() {
-               $it = $this->ait[$this->count-1];
+       function key()
+       {
+               $it = $this->ait[$this->count];
                return $it->key();
        }
        
-       function current() {
-               $it = $this->ait[$this->count-1];
+       function current()
+       {
+               $it = $this->ait[$this->count];
                return $it->current();
        }
        
-       function next() {
+       function next()
+       {
                while ($this->count) {
-                       $it = $this->ait[$this->count-1];
+                       $it = $this->ait[$this->count];
                        if ($it->hasMore()) {
                                if (!$it->recursed && $it->hasChildren()) {
                                        $it->recursed = true;
@@ -56,8 +62,8 @@ class RecursiveIteratorIterator implements Iterator
                                        $sub->recursed = false;
                                        $sub->rewind();
                                        if ($sub->hasMore()) {
-                                               $this->ait[$this->count++] = $sub;
-                                               if (!is_a($sub, 'RecursiveIterator')) {
+                                               $this->ait[++$this->count] = $sub;
+                                               if (!$sub instanceof RecursiveIterator)) {
                                                        throw new Exception(get_class($sub).'::getChildren() must return an object that implements RecursiveIterator');
                                                } 
                                                return;
@@ -71,16 +77,24 @@ class RecursiveIteratorIterator implements Iterator
                                }
                                $it->recursed = false;
                        }
-                       if ($this->count <= 1) {
-                               return;
+                       if ($this->count) {
+                               unset($this->ait[$this->count--]);
+                               $it = $this->ait[$this->count];
                        }
-                       unset($this->ait[--$this->count]);
-                       $it = $this->ait[$this->count-1];
                }
        }
        
-       function getCurrentIterator() {
-               return $this->ait[$this->count-1];
+       function getSubIterator($level = NULL)
+       {
+               if (is_null($level)) {
+                       $level = $this->count;
+               }
+               return @$this->ait[$level];
+       }
+
+       function getDepth()
+       {
+               return $this->level;
        }
 }
 
index 176c2860767bf33bc58f6e8fcbb3bdf568d57f81..0dc7c056f85d3402fcf4ca8b9388d44de52a7dce 100755 (executable)
@@ -1,18 +1,16 @@
 <?php
 
-/* tree view example
+/** tree view example
  *
- * Usage: php tree.php <path>
+ * Usage: php Tree.php <path>
  *
  * Simply specify the path to tree with parameter <path>.
  *
  * (c) Marcus Boerger
  */
 
-require_once("sub_dir.inc");
-
-foreach(new sub_dir($argv[1], true, isset($argv[2]) ? $argv[2] : false) as $f) {
-       echo "$f\n";
+foreach(new DirectoryGraphIterator($argv[1]) as $file) {
+       echo $file . "\n";
 }
 
 ?>
\ No newline at end of file