From: Marcus Boerger Date: Sat, 8 May 2004 12:24:15 +0000 (+0000) Subject: - Update examples X-Git-Tag: RELEASE_0_1~264 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a6feb3f405f76f068c6a89058f3382b4ebe4d4f2;p=php - Update examples - Update documentation - Move classes/interfaces already implemented in c to new subdir internal --- diff --git a/ext/spl/examples/KeyFilter.inc b/ext/spl/examples/KeyFilter.inc deleted file mode 100755 index ba73435955..0000000000 --- a/ext/spl/examples/KeyFilter.inc +++ /dev/null @@ -1,106 +0,0 @@ -it = $it; - $this->regex = $regex; - $this->fetch(); - } - - /** - * Rewind input iterator - */ - function rewind() { - $this->it->rewind(); - } - - /** - * Destruct the iterator. - */ - function __destruct() { - unset($this->it); - } - - /** - * Fetch next element and store it. - * - * @return void - */ - protected function fetch() { - $this->key = false; - $this->curr = false; - while ($this->it->valid()) { - $key = $this->it->key(); - if (ereg($this->regex, $key)) { - $this->key = $key; - $this->curr = $this->it->current(); - return; - } - $this->it->next(); - }; - } - - /** - * Move to next element - * - * @return void - */ - function next() { - $this->it->next(); - $this->fetch(); - } - - /** - * @return Whether more elements are available - */ - function valid() { - return $this->key !== false; - } - - /** - * @return The current key - */ - function key() { - return $this->key; - } - - /** - * @return The current value - */ - function current() { - return $this->curr; - } - - /** - * hidden __clone - */ - protected function __clone() { - // disallow clone - } -} - -?> \ No newline at end of file diff --git a/ext/spl/examples/autoload.inc b/ext/spl/examples/autoload.inc index 430680645a..c2c4222f85 100755 --- a/ext/spl/examples/autoload.inc +++ b/ext/spl/examples/autoload.inc @@ -1,5 +1,8 @@ db = dba_popen($file, "c", $handler); - if (!$this->db) { - throw new exception("Databse could not be opened"); - } - } - - function __destruct() - { - dba_close($this->db); - } - - function get($name) - { - $data = dba_fetch($name, $this->db); - if($data) { - if (ini_get('magic_quotes_runtime')) { - $data = stripslashes($data); - } - //return unserialize($data); - return $data; - } - else - { - return NULL; - } - } - - function set($name, $value) - { - //dba_replace($name, serialize($value), $this->db); - dba_replace($name, $value, $this->db); - return $value; - } - - function exists($name) - { - return dba_exists($name, $this->db); - } - - function del($name) - { - return dba_delete($name, $this->db); - } -} +if (!class_exists("DbaReader", false)) require_once("dbareader.inc"); try { if ($argc > 2) { diff --git a/ext/spl/examples/dba_dump.php b/ext/spl/examples/dba_dump.php index cdee831b0a..613674fef8 100755 --- a/ext/spl/examples/dba_dump.php +++ b/ext/spl/examples/dba_dump.php @@ -9,7 +9,7 @@ * * Note: configure with --enable-dba * - * (c) Marcus Boerger, 2003 + * (c) Marcus Boerger, 2003 - 2004 */ if ($argc < 3) { @@ -24,13 +24,13 @@ EOF; exit(1); } -require_once("dba_reader.inc"); -require_once("KeyFilter.inc"); +if (!class_exists("DbaReader")) require_once("dbareader.inc"); +if (!class_exists("KeyFilter")) require_once("keyfilter.inc"); $db = new DbaReader($argv[1], $argv[2]); if ($argc>3) { - $db = new keyFilter($db, $argv[3]); + $db = new KeyFilter($db, $argv[3]); } foreach($db as $key => $val) { diff --git a/ext/spl/examples/dbaarray.inc b/ext/spl/examples/dbaarray.inc new file mode 100755 index 0000000000..51cd38fadf --- /dev/null +++ b/ext/spl/examples/dbaarray.inc @@ -0,0 +1,89 @@ +db = dba_popen($file, "c", $handler); + if (!$this->db) { + throw new exception("Databse could not be opened"); + } + } + + /** + * Close database. + */ + function __destruct() + { + parent::__destruct(); + } + + /** + * Read an entry. + * + * @param $name key to read from + * @return value associated with $name + */ + function offsetGet($name) + { + $data = dba_fetch($name, $this->db); + if($data) { + if (ini_get('magic_quotes_runtime')) { + $data = stripslashes($data); + } + //return unserialize($data); + return $data; + } + else + { + return NULL; + } + } + + /** + * Set an entry. + * + * @param $name key to write to + * @param $value value to write + */ + function offsetSet($name, $value) + { + //dba_replace($name, serialize($value), $this->db); + dba_replace($name, $value, $this->db); + return $value; + } + + /** + * @return whether key $name exists. + */ + function offsetExists($name) + { + return dba_exists($name, $this->db); + } + + /** + * Delete a key/value pair. + * + * @param $name key to delete. + */ + function offsetUnset($name) + { + return dba_delete($name, $this->db); + } +} + +?> \ No newline at end of file diff --git a/ext/spl/examples/dba_reader.inc b/ext/spl/examples/dbareader.inc similarity index 93% rename from ext/spl/examples/dba_reader.inc rename to ext/spl/examples/dbareader.inc index fa8fd3d854..add4df9b1f 100755 --- a/ext/spl/examples/dba_reader.inc +++ b/ext/spl/examples/dbareader.inc @@ -1,14 +1,14 @@ it->isDot(); } - function hasChildren() { + /** @return whether the current entry is a directory + */ + function hasChildren() + { return $this->it->hasChildren(); } - function getChildren() { + /** @return the current subdirectory as a new DirectoryFilterDots instance. + */ + function getChildren() + { return new DirectoryFilterDots($this->it->getPathname()); } - function key() { + /** @return the current entries path name + */ + function key() + { return $this->it->getPathname(); } } diff --git a/ext/spl/examples/directorygraphiterator.inc b/ext/spl/examples/directorygraphiterator.inc deleted file mode 100644 index a8382e2baa..0000000000 --- a/ext/spl/examples/directorygraphiterator.inc +++ /dev/null @@ -1,11 +0,0 @@ - \ No newline at end of file diff --git a/ext/spl/examples/directorytree.inc b/ext/spl/examples/directorytree.inc index 218ca513d9..31c84b4af8 100755 --- a/ext/spl/examples/directorytree.inc +++ b/ext/spl/examples/directorytree.inc @@ -1,7 +1,15 @@ getSubIterator($l)->hasNext() ? '|-' : '\-') . $this->getSubIterator($l)->__toString(); } - + + /** Aggregates the inner iterator + */ function __call($func, $params) { return call_user_func_array(array($this->getSubIterator(), $func), $params); diff --git a/ext/spl/examples/emptyiterator.inc b/ext/spl/examples/emptyiterator.inc index 34b5118e74..1a79173938 100755 --- a/ext/spl/examples/emptyiterator.inc +++ b/ext/spl/examples/emptyiterator.inc @@ -1,6 +1,6 @@ file = $file; @@ -25,10 +36,21 @@ class FindFile extends FilterIterator } } + /** @return whether the current file matches the given filename + */ function accept() { return !strcmp($this->current(), $this->file); } + + /** @return the filename to search for. + * @note This may be overloaded and contain a regular expression for an + * extended class that uses regular expressions to search. + */ + function getSearch() + { + return $this->file; + } } ?> \ No newline at end of file diff --git a/ext/spl/examples/findregex.php b/ext/spl/examples/findregex.php index faed6a8ee0..55431c0e38 100755 --- a/ext/spl/examples/findregex.php +++ b/ext/spl/examples/findregex.php @@ -24,15 +24,10 @@ EOF; exit(1); } -class RegexFindFile extends FindFile -{ - function accept() - { - return preg_match($this->file, $this->current()); - } -} +if (!class_exists("RegexFindFile")) require_once("regexfindfile.inc"); -foreach(new RegexFindFile($argv[1], $argv[2]) as $file) { +foreach(new RegexFindFile($argv[1], $argv[2]) as $file) +{ echo $file->getPathname()."\n"; } diff --git a/ext/spl/examples/infiniteiterator.inc b/ext/spl/examples/infiniteiterator.inc index d6488750b6..ea83edb76e 100755 --- a/ext/spl/examples/infiniteiterator.inc +++ b/ext/spl/examples/infiniteiterator.inc @@ -1,6 +1,6 @@ it = $it; } + /** @return the inner iterator + */ function getInnerIterator() { return $this->it; } + /** Rewind the inner iterator. + * @return void + */ function rewind() { $this->it->rewind(); } + /** @return whether the current element is valid + */ function valid() { return $this->it->valid(); } + /** @return the current value + */ function current() { return $this->it->current(); } + /** @return the current key + */ function key() { return $this->it->key(); } + /** Move the inner Iterator forward to its next element or rewind it. + * @return void + */ function next() { $this->it->next(); @@ -62,6 +81,13 @@ class InfiniteIterator implements Iterator $this->it->rewind(); } } + + /** Aggregates the inner iterator + */ + 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/ini_groups.php b/ext/spl/examples/ini_groups.php index 2e76752e23..81cd4012b0 100755 --- a/ext/spl/examples/ini_groups.php +++ b/ext/spl/examples/ini_groups.php @@ -24,8 +24,8 @@ EOF; exit(1); } -require_once("dba_reader.inc"); -require_once("IniGroups.inc"); +if (!class_exists("KeyFilter")) require_once("keyfilter.inc"); +if (!class_exists("IniGroups")) require_once("inigroups.inc"); $it = new IniGroups($argv[1]); if ($argc>2) { diff --git a/ext/spl/examples/IniGroups.inc b/ext/spl/examples/inigroups.inc similarity index 79% rename from ext/spl/examples/IniGroups.inc rename to ext/spl/examples/inigroups.inc index bb596c2717..745930643b 100755 --- a/ext/spl/examples/IniGroups.inc +++ b/ext/spl/examples/inigroups.inc @@ -1,11 +1,12 @@ regex = $regex; + } + + /** \return whether the current key mathes the regular expression + */ + function accept() + { + return ereg($this->regex, $this->getInnerIterator()->key()); + } + + /** @return regular expression used as filter + */ + function getRegex() + { + return $this->regex; + } + + /** + * hidden __clone + */ + protected function __clone() + { + // disallow clone + } +} + +?> \ No newline at end of file diff --git a/ext/spl/examples/searchiterator.inc b/ext/spl/examples/searchiterator.inc index a9ba9df171..c2bbbe6dea 100755 --- a/ext/spl/examples/searchiterator.inc +++ b/ext/spl/examples/searchiterator.inc @@ -1,21 +1,49 @@ done = false; } - function valid() { + /** @return whether the current element is valid + * which can only happen once per iteration. + */ + function valid() + { return !$this->done && parent::valid(); } - function next() { + /** Do not move forward but instead mark as finished. + * @return void + */ + function next() + { $this->done = true; } + + /** Aggregates the inner iterator + */ + 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/tree.php b/ext/spl/examples/tree.php index 9a61acf942..ce989a2c34 100755 --- a/ext/spl/examples/tree.php +++ b/ext/spl/examples/tree.php @@ -6,7 +6,7 @@ * * Simply specify the path to tree with parameter . * - * (c) Marcus Boerger, 2003 + * (c) Marcus Boerger, 2003 - 2004 */ // The following line only operates on classes which are converted to c already. @@ -26,8 +26,11 @@ EOF; exit(1); } +if (!class_exists("DirectoryTreeIterator")) require_once("directorytreeiterator.inc"); + echo $argv[1]."\n"; -foreach(new DirectoryGraphIterator($argv[1]) as $file) { +foreach(new DirectoryTreeIterator($argv[1]) as $file) +{ echo $file . "\n"; } diff --git a/ext/spl/examples/cachingiterator.inc b/ext/spl/internal/cachingiterator.inc old mode 100644 new mode 100755 similarity index 100% rename from ext/spl/examples/cachingiterator.inc rename to ext/spl/internal/cachingiterator.inc diff --git a/ext/spl/examples/cachingrecursiveiterator.inc b/ext/spl/internal/cachingrecursiveiterator.inc old mode 100644 new mode 100755 similarity index 100% rename from ext/spl/examples/cachingrecursiveiterator.inc rename to ext/spl/internal/cachingrecursiveiterator.inc diff --git a/ext/spl/examples/filteriterator.inc b/ext/spl/internal/filteriterator.inc similarity index 100% rename from ext/spl/examples/filteriterator.inc rename to ext/spl/internal/filteriterator.inc diff --git a/ext/spl/examples/limititerator.inc b/ext/spl/internal/limititerator.inc similarity index 100% rename from ext/spl/examples/limititerator.inc rename to ext/spl/internal/limititerator.inc diff --git a/ext/spl/examples/parentiterator.inc b/ext/spl/internal/parentiterator.inc old mode 100644 new mode 100755 similarity index 100% rename from ext/spl/examples/parentiterator.inc rename to ext/spl/internal/parentiterator.inc diff --git a/ext/spl/examples/recursiveiterator.inc b/ext/spl/internal/recursiveiterator.inc old mode 100644 new mode 100755 similarity index 100% rename from ext/spl/examples/recursiveiterator.inc rename to ext/spl/internal/recursiveiterator.inc diff --git a/ext/spl/examples/recursiveiteratoriterator.inc b/ext/spl/internal/recursiveiteratoriterator.inc similarity index 100% rename from ext/spl/examples/recursiveiteratoriterator.inc rename to ext/spl/internal/recursiveiteratoriterator.inc diff --git a/ext/spl/examples/seekableiterator.inc b/ext/spl/internal/seekableiterator.inc similarity index 100% rename from ext/spl/examples/seekableiterator.inc rename to ext/spl/internal/seekableiterator.inc