summaryrefslogtreecommitdiff
path: root/ext/spl/examples/cachingrecursiveiterator.inc
blob: dfeeea54e7adf67b1056637a84e3162b21c0fb54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php

class CachingRecursiveIterator extends CachingIterator implements RecursiveIterator
{
	protected $hasChildren;
	protected $getChildren;
	protected $catch_get_child;

	function __construct(RecursiveIterator $it, $flags = CIT_CALL_TOSTRING)
	{
		parent::__construct($it, $flags);
	}
	
	function next()
	{
		if ($this->hasChildren = $this->it->hasChildren()) {
			try {
				//$this->getChildren = new CachingRecursiveIterator($this->it->getChildren(), $this->flags);
				// workaround memleaks...
				$child = $this->it->getChildren();
				$this->getChildren = new CachingRecursiveIterator($child, $this->flags);
			}
			catch(Exception $e) {
				if (!$this->flags & CIT_CATCH_GET_CHILD) {
					throw $e;
				}
				$this->hasChildren = false;
				$this->getChildren = NULL;
			}
		} else {
			$this->getChildren = NULL;
		}
		parent::next();
	}

	function hasChildren()
	{
		return $this->hasChildren;
	}

	function getChildren()
	{
		return $this->getChildren;
	}
}

?>