Skip to content

Commit ef1b44c

Browse files
committed
Added caching of result.
Signed-off-by: Jan Pecha <janpecha@email.cz>
1 parent 8d11644 commit ef1b44c

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/Dependency.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ class Dependency
1616

1717
/** @var array [(string) item => TRUE] */
1818
private $cache = array();
19+
20+
/** @var bool */
21+
private $useCached = TRUE;
1922

2023

2124

@@ -32,6 +35,7 @@ public function add($item, $depends = NULL)
3235
}
3336

3437
$this->items[(string)$item] = $depends;
38+
$this->useCached = FALSE;
3539
return $this;
3640
}
3741

@@ -55,10 +59,16 @@ public function reset()
5559
*/
5660
public function getResolved()
5761
{
62+
if($this->useCached)
63+
{
64+
return $this->result;
65+
}
66+
5867
$this->result = array();
5968
$this->cache = array();
6069

6170
array_walk($this->items, array($this, 'applyWalk'));
71+
$this->useCached = TRUE;
6272
return $this->result;
6373
}
6474

tests/Dependency/basic.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ $resolver->add('a', array('b', 'c'));
4545
$resolver->add('b', 'd');
4646
$resolver->add('c', array('d'));
4747

48+
Assert::same(array('d', 'b', 'c', 'a', 'x'), $resolver->getResolved());
49+
# cache test
4850
Assert::same(array('d', 'b', 'c', 'a', 'x'), $resolver->getResolved());
4951
$resolver->reset();
5052

@@ -59,5 +61,3 @@ $result = $resolver->add('x', array('a', 'b'))
5961
->getResolved();
6062

6163
Assert::same(array('d', 'b', 'c', 'a', 'x'), $result);
62-
63-

0 commit comments

Comments
 (0)