| 1 |
<?php |
| 2 |
|
| 3 |
namespace HtaccessCapabilityTester; |
| 4 |
|
| 5 |
use \HtaccessCapabilityTester\Testers\AbstractTester; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class caching test results |
| 9 |
* |
| 10 |
* @package HtaccessCapabilityTester |
| 11 |
* @author Bjørn Rosell <[email protected]> |
| 12 |
* @since Class available since the beginning |
| 13 |
*/ |
| 14 |
class TestResultCache |
| 15 |
{ |
| 16 |
|
| 17 |
/* @var array Array for caching */ |
| 18 |
protected static $cache; |
| 19 |
|
| 20 |
/** |
| 21 |
* |
| 22 |
* @param array $cacheKeys Two keys for caching (usually: basedir and the getCacheKey() for the Tester) |
| 23 |
* @param TestResult $testResult The test result to cache |
| 24 |
* |
| 25 |
* @return void |
| 26 |
*/ |
| 27 |
public static function cache($cacheKeys, $testResult) |
| 28 |
{ |
| 29 |
if (!isset(self::$cache)) { |
| 30 |
self::$cache = []; |
| 31 |
} |
| 32 |
list($key1, $key2) = $cacheKeys; |
| 33 |
if (!isset(self::$cache[$key1])) { |
| 34 |
self::$cache[$key1] = []; |
| 35 |
} |
| 36 |
self::$cache[$key1][$key2] = $testResult; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Check if in cache. |
| 41 |
* |
| 42 |
* @param array $cacheKeys Keys for caching (usually: basedir and the getCacheKey() for the Tester) |
| 43 |
* |
| 44 |
* @return bool |
| 45 |
*/ |
| 46 |
public static function isCached($cacheKeys) |
| 47 |
{ |
| 48 |
if (!isset(self::$cache)) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
list($key1, $key2) = $cacheKeys; |
| 52 |
if (!isset(self::$cache[$key1])) { |
| 53 |
return false; |
| 54 |
} |
| 55 |
if (!isset(self::$cache[$key1][$key2])) { |
| 56 |
return false; |
| 57 |
} |
| 58 |
return true; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Get from cache. |
| 63 |
* |
| 64 |
* @param array $cacheKeys Keys for caching (usually: basedir and the getCacheKey() for the Tester) |
| 65 |
* |
| 66 |
* @return TestResult The test result |
| 67 |
*/ |
| 68 |
public static function getCached($cacheKeys) |
| 69 |
{ |
| 70 |
if (!self::isCached($cacheKeys)) { |
| 71 |
throw new \Exception('Not in cache'); |
| 72 |
} |
| 73 |
list($key1, $key2) = $cacheKeys; |
| 74 |
return self::$cache[$key1][$key2]; |
| 75 |
} |
| 76 |
|
| 77 |
public static function clear() |
| 78 |
{ |
| 79 |
self::$cache = null; |
| 80 |
} |
| 81 |
} |
| 82 |
|