diff options
author | Marcus Boerger <helly@php.net> | 2003-11-22 20:51:15 +0000 |
---|---|---|
committer | Marcus Boerger <helly@php.net> | 2003-11-22 20:51:15 +0000 |
commit | 3d9ec6338463c4cb273b5f94798a70a38c57dc79 (patch) | |
tree | df929f79658dc5278c236b13f6310ce22bddeb9f | |
parent | eca139ec5207d2af60dbcd617d2b481b668283bf (diff) | |
download | php-git-3d9ec6338463c4cb273b5f94798a70a38c57dc79.tar.gz |
Update examples
-rw-r--r-- | ext/spl/examples/cachingiterator.inc | 27 | ||||
-rw-r--r-- | ext/spl/examples/cachingrecursiveiterator.inc | 22 | ||||
-rwxr-xr-x | ext/spl/examples/dba_array.php | 17 | ||||
-rwxr-xr-x | ext/spl/examples/dba_dump.php | 4 | ||||
-rwxr-xr-x | ext/spl/examples/dba_reader.inc | 4 | ||||
-rwxr-xr-x | ext/spl/examples/directorytree.php | 4 | ||||
-rw-r--r-- | ext/spl/examples/directorytreeiterator.inc | 7 | ||||
-rwxr-xr-x | ext/spl/examples/findfile.php | 2 | ||||
-rwxr-xr-x | ext/spl/examples/key_filter.inc | 14 | ||||
-rwxr-xr-x | ext/spl/examples/recursiveiteratoriterator.inc | 58 | ||||
-rwxr-xr-x | ext/spl/examples/tree.php | 10 |
11 files changed, 105 insertions, 64 deletions
diff --git a/ext/spl/examples/cachingiterator.inc b/ext/spl/examples/cachingiterator.inc index a9be5d5d84..9146ceeaf6 100644 --- a/ext/spl/examples/cachingiterator.inc +++ b/ext/spl/examples/cachingiterator.inc @@ -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; } } diff --git a/ext/spl/examples/cachingrecursiveiterator.inc b/ext/spl/examples/cachingrecursiveiterator.inc index 55acd1e195..59709a7522 100644 --- a/ext/spl/examples/cachingrecursiveiterator.inc +++ b/ext/spl/examples/cachingrecursiveiterator.inc @@ -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; } } diff --git a/ext/spl/examples/dba_array.php b/ext/spl/examples/dba_array.php index ebbe5a7bac..bd73ce2a0d 100755 --- a/ext/spl/examples/dba_array.php +++ b/ext/spl/examples/dba_array.php @@ -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 { diff --git a/ext/spl/examples/dba_dump.php b/ext/spl/examples/dba_dump.php index 77ea2008bd..04efdf4d88 100755 --- a/ext/spl/examples/dba_dump.php +++ b/ext/spl/examples/dba_dump.php @@ -15,10 +15,10 @@ 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) { diff --git a/ext/spl/examples/dba_reader.inc b/ext/spl/examples/dba_reader.inc index b8c7365f97..d21db45613 100755 --- a/ext/spl/examples/dba_reader.inc +++ b/ext/spl/examples/dba_reader.inc @@ -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; diff --git a/ext/spl/examples/directorytree.php b/ext/spl/examples/directorytree.php index a5d5d99490..e22244a9e1 100755 --- a/ext/spl/examples/directorytree.php +++ b/ext/spl/examples/directorytree.php @@ -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 diff --git a/ext/spl/examples/directorytreeiterator.inc b/ext/spl/examples/directorytreeiterator.inc index bd815b6821..b247e70701 100644 --- a/ext/spl/examples/directorytreeiterator.inc +++ b/ext/spl/examples/directorytreeiterator.inc @@ -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 diff --git a/ext/spl/examples/findfile.php b/ext/spl/examples/findfile.php index 003111e272..dc23c5a032 100755 --- a/ext/spl/examples/findfile.php +++ b/ext/spl/examples/findfile.php @@ -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); diff --git a/ext/spl/examples/key_filter.inc b/ext/spl/examples/key_filter.inc index 47ceed2246..6dfd19c85b 100755 --- a/ext/spl/examples/key_filter.inc +++ b/ext/spl/examples/key_filter.inc @@ -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. diff --git a/ext/spl/examples/recursiveiteratoriterator.inc b/ext/spl/examples/recursiveiteratoriterator.inc index 95d52d7f76..7d151f9e9f 100755 --- a/ext/spl/examples/recursiveiteratoriterator.inc +++ b/ext/spl/examples/recursiveiteratoriterator.inc @@ -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; } } diff --git a/ext/spl/examples/tree.php b/ext/spl/examples/tree.php index 176c286076..0dc7c056f8 100755 --- a/ext/spl/examples/tree.php +++ b/ext/spl/examples/tree.php @@ -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 |