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();
$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;
}
}
{
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;
parent::next();
}
- function hasChildren() {
+ function hasChildren()
+ {
return $this->hasChildren;
}
- function getChildren() {
+ function getChildren()
+ {
return $this->getChildren;
}
}
* (c) Marcus Boerger
*/
-class dba_array implements spl_array_access {
+class DbaArray implements ArrayAccess {
private $db;
function __construct($file, $handler)
if (ini_get('magic_quotes_runtime')) {
$data = stripslashes($data);
}
- return unserialize($data);
+ //return unserialize($data);
+ return $data;
}
else
{
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;
}
{
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
{
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) {
<?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;
$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
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
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);
* 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;
* @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.
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;
$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;
}
$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;
}
}
<?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