2003-11-18 22:18:38 +00:00
|
|
|
<?php
|
|
|
|
|
2004-10-29 20:58:58 +00:00
|
|
|
/** @file cachingrecursiveiterator.inc
|
2004-10-31 19:05:37 +00:00
|
|
|
* @ingroup SPL
|
2004-10-29 20:58:58 +00:00
|
|
|
* @brief class CachingRecursiveIterator
|
|
|
|
* @author Marcus Boerger
|
|
|
|
* @date 2003 - 2004
|
|
|
|
*
|
|
|
|
* SPL - Standard PHP Library
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2004-10-31 19:05:37 +00:00
|
|
|
* @brief Cached recursive iteration over another Iterator
|
2004-10-29 20:58:58 +00:00
|
|
|
* @author Marcus Boerger
|
|
|
|
* @version 1.1
|
2004-10-31 19:05:37 +00:00
|
|
|
*
|
2004-11-01 18:11:39 +00:00
|
|
|
* @see CachingIterator
|
2004-10-29 20:58:58 +00:00
|
|
|
*/
|
2003-11-18 22:18:38 +00:00
|
|
|
class CachingRecursiveIterator extends CachingIterator implements RecursiveIterator
|
|
|
|
{
|
|
|
|
protected $hasChildren;
|
|
|
|
protected $getChildren;
|
|
|
|
|
2004-10-29 20:58:58 +00:00
|
|
|
/** Construct from another iterator
|
|
|
|
*
|
|
|
|
* @param it Iterator to cache
|
|
|
|
* @param flags Bitmask:
|
|
|
|
* - CIT_CALL_TOSTRING (whether to call __toString() for every element)
|
|
|
|
* - CIT_CATCH_GET_CHILD (whether to catch exceptions when trying to get childs)
|
|
|
|
*/
|
2003-12-08 08:31:08 +00:00
|
|
|
function __construct(RecursiveIterator $it, $flags = CIT_CALL_TOSTRING)
|
2003-11-22 20:51:15 +00:00
|
|
|
{
|
2003-12-07 15:03:11 +00:00
|
|
|
parent::__construct($it, $flags);
|
2003-11-18 22:18:38 +00:00
|
|
|
}
|
2004-10-29 20:58:58 +00:00
|
|
|
|
|
|
|
/** Rewind Iterator
|
|
|
|
*/
|
2004-05-02 22:07:32 +00:00
|
|
|
function rewind();
|
|
|
|
{
|
|
|
|
$this->hasChildren = false;
|
|
|
|
$this->getChildren = NULL;
|
|
|
|
parent::rewind();
|
|
|
|
}
|
|
|
|
|
2004-10-29 20:58:58 +00:00
|
|
|
/** Forward to next element if necessary then an Iterator for the Children
|
|
|
|
* will be created.
|
|
|
|
*/
|
2003-11-22 20:51:15 +00:00
|
|
|
function next()
|
|
|
|
{
|
2003-11-18 22:18:38 +00:00
|
|
|
if ($this->hasChildren = $this->it->hasChildren()) {
|
2003-11-19 00:18:30 +00:00
|
|
|
try {
|
2003-12-07 15:03:11 +00:00
|
|
|
//$this->getChildren = new CachingRecursiveIterator($this->it->getChildren(), $this->flags);
|
2003-11-19 00:18:30 +00:00
|
|
|
// workaround memleaks...
|
|
|
|
$child = $this->it->getChildren();
|
2003-12-07 15:03:11 +00:00
|
|
|
$this->getChildren = new CachingRecursiveIterator($child, $this->flags);
|
2003-11-19 00:18:30 +00:00
|
|
|
}
|
|
|
|
catch(Exception $e) {
|
2003-12-07 15:03:11 +00:00
|
|
|
if (!$this->flags & CIT_CATCH_GET_CHILD) {
|
2003-11-19 00:18:30 +00:00
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
$this->hasChildren = false;
|
|
|
|
$this->getChildren = NULL;
|
|
|
|
}
|
2003-11-18 22:18:38 +00:00
|
|
|
} else {
|
|
|
|
$this->getChildren = NULL;
|
|
|
|
}
|
|
|
|
parent::next();
|
|
|
|
}
|
|
|
|
|
2004-10-29 20:58:58 +00:00
|
|
|
/** @return whether the current element has children
|
|
|
|
* @note The check whether the Iterator for the children can be created was
|
|
|
|
* already executed. Hence when flag CIT_CATCH_GET_CHILD was given in
|
|
|
|
* constructor this fucntion returns false so that getChildren does
|
|
|
|
* not try to access those children.
|
|
|
|
*/
|
2003-11-22 20:51:15 +00:00
|
|
|
function hasChildren()
|
|
|
|
{
|
2003-11-18 22:18:38 +00:00
|
|
|
return $this->hasChildren;
|
|
|
|
}
|
|
|
|
|
2004-10-29 20:58:58 +00:00
|
|
|
/** @return An Iterator for the children
|
|
|
|
*/
|
2003-11-22 20:51:15 +00:00
|
|
|
function getChildren()
|
|
|
|
{
|
2003-11-18 22:18:38 +00:00
|
|
|
return $this->getChildren;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|