Skip to content

Commit 1765271

Browse files
committed
Use a single bit field for the flags here
1 parent 8eb22d7 commit 1765271

File tree

4 files changed

+15
-13
lines changed

4 files changed

+15
-13
lines changed

ext/spl/examples/cachingiterator.inc

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php
22

3+
define('CIT_GET_STR_VALUE', 1);
4+
define('CIT_CATCH_GET_CHILD', 2);
5+
36
class CachingIterator
47
{
58
protected $it;
@@ -9,10 +12,10 @@ class CachingIterator
912
protected $strValue;
1013
protected $getStrVal;
1114

12-
function __construct(Iterator $it, $getStrVal = true)
15+
function __construct(Iterator $it, $flags = CIT_GET_STR_VALUE)
1316
{
1417
$this->it = $it;
15-
$this->getStrVal = (boolean)$getStrVal;
18+
$this->flags = $flags & (CIT_GET_STR_VALUE|CIT_CATCH_GET_CHILD);
1619
}
1720

1821
function rewind()
@@ -26,7 +29,7 @@ class CachingIterator
2629
if ($this->more = $this->it->hasMore()) {
2730
$this->current = $this->it->current();
2831
$this->key = $this->it->key();
29-
if ($this->getStrVal) {
32+
if ($this->flags & CIT_GET_STR_VALUE) {
3033
if (is_object($this->current)) {
3134
$this->strValue = $this->current->__toString();
3235
} else {
@@ -36,7 +39,7 @@ class CachingIterator
3639
} else {
3740
$this->current = NULL;
3841
$this->key = NULL;
39-
$this->strValue = '';
42+
$this->strValue = NULL;
4043
}
4144
$this->it->next();
4245
}
@@ -68,7 +71,7 @@ class CachingIterator
6871

6972
function __toString()
7073
{
71-
if (!$this->getStrVal) {
74+
if (!$this->flags & CIT_GET_STR_VALUE) {
7275
throw new exception('CachingIterator does not fetch string value (see CachingIterator::__construct)');
7376
}
7477
return $this->strValue;

ext/spl/examples/cachingrecursiveiterator.inc

+5-6
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,22 @@ class CachingRecursiveIterator extends CachingIterator implements RecursiveItera
66
protected $getChildren;
77
protected $catch_get_child;
88

9-
function __construct(RecursiveIterator $it, $getStrVal = true, $catch_get_child = false)
9+
function __construct(RecursiveIterator $it, $flags = CIT_GET_STR_VALUE)
1010
{
11-
$this->catch_get_child = $catch_get_child;
12-
parent::__construct($it, $getStrVal);
11+
parent::__construct($it, $flags);
1312
}
1413

1514
function next()
1615
{
1716
if ($this->hasChildren = $this->it->hasChildren()) {
1817
try {
19-
//$this->getChildren = new CachingRecursiveIterator($this->it->getChildren(), $this->getStrVal, $this->catch_get_child);
18+
//$this->getChildren = new CachingRecursiveIterator($this->it->getChildren(), $this->flags);
2019
// workaround memleaks...
2120
$child = $this->it->getChildren();
22-
$this->getChildren = new CachingRecursiveIterator($child, $this->getStrVal, $this->catch_get_child);
21+
$this->getChildren = new CachingRecursiveIterator($child, $this->flags);
2322
}
2423
catch(Exception $e) {
25-
if (!$this->catch_get_child) {
24+
if (!$this->flags & CIT_CATCH_GET_CHILD) {
2625
throw $e;
2726
}
2827
$this->hasChildren = false;

ext/spl/examples/directorygraphiterator.inc

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class DirectoryGraphIterator extends DirectoryTreeIterator
44
{
55
function __construct($path)
66
{
7-
RecursiveIteratorIterator::__construct(new CachingRecursiveIterator(new ParentIterator(new RecursiveDirectoryIterator($path)), true, true), 1);
7+
RecursiveIteratorIterator::__construct(new CachingRecursiveIterator(new ParentIterator(new RecursiveDirectoryIterator($path)), CIT_GET_STR_VALUE|CIT_CATCH_GET_CHILD), 1);
88
}
99
}
1010

ext/spl/examples/directorytreeiterator.inc

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class DirectoryTreeIterator extends RecursiveIteratorIterator
44
{
55
function __construct($path)
66
{
7-
parent::__construct(new CachingRecursiveIterator(new RecursiveDirectoryIterator($path), true, true), 1);
7+
parent::__construct(new CachingRecursiveIterator(new RecursiveDirectoryIterator($path), CIT_GET_STR_VALUE|CIT_CATCH_GET_CHILD), 1);
88
}
99

1010
function current()

0 commit comments

Comments
 (0)