| 1 |
<?php |
| 2 |
/** |
| 3 |
* Function for caching between runs. |
| 4 |
* |
| 5 |
* @author Greg Sherwood <[email protected]> |
| 6 |
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) |
| 7 |
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace PHP_CodeSniffer\Util; |
| 11 |
|
| 12 |
use PHP_CodeSniffer\Autoload; |
| 13 |
use PHP_CodeSniffer\Config; |
| 14 |
use PHP_CodeSniffer\Ruleset; |
| 15 |
|
| 16 |
class Cache |
| 17 |
{ |
| 18 |
|
| 19 |
/** |
| 20 |
* The filesystem location of the cache file. |
| 21 |
* |
| 22 |
* @var void |
| 23 |
*/ |
| 24 |
private static $path = ''; |
| 25 |
|
| 26 |
/** |
| 27 |
* The cached data. |
| 28 |
* |
| 29 |
* @var array<string, mixed> |
| 30 |
*/ |
| 31 |
private static $cache = []; |
| 32 |
|
| 33 |
/** |
| 34 |
* Saves the current cache to the filesystem. |
| 35 |
* |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
public static function save() |
| 39 |
{ |
| 40 |
file_put_contents(self::$path, json_encode(self::$cache)); |
| 41 |
|
| 42 |
}//end save() |
| 43 |
|
| 44 |
|
| 45 |
/** |
| 46 |
* Retrieves a single entry from the cache. |
| 47 |
* |
| 48 |
* @param string $key The key of the data to get. If NULL, |
| 49 |
* everything in the cache is returned. |
| 50 |
* |
| 51 |
* @return mixed |
| 52 |
*/ |
| 53 |
public static function get($key=null) |
| 54 |
{ |
| 55 |
if ($key === null) { |
| 56 |
return self::$cache; |
| 57 |
} |
| 58 |
|
| 59 |
if (isset(self::$cache[$key]) === true) { |
| 60 |
return self::$cache[$key]; |
| 61 |
} |
| 62 |
|
| 63 |
return false; |
| 64 |
|
| 65 |
}//end get() |
| 66 |
|
| 67 |
|
| 68 |
/** |
| 69 |
* Retrieves a single entry from the cache. |
| 70 |
* |
| 71 |
* @param string $key The key of the data to set. If NULL, |
| 72 |
* sets the entire cache. |
| 73 |
* @param mixed $value The value to set. |
| 74 |
* |
| 75 |
* @return void |
| 76 |
*/ |
| 77 |
public static function set($key, $value) |
| 78 |
{ |
| 79 |
if ($key === null) { |
| 80 |
self::$cache = $value; |
| 81 |
} else { |
| 82 |
self::$cache[$key] = $value; |
| 83 |
} |
| 84 |
|
| 85 |
}//end set() |
| 86 |
|
| 87 |
|
| 88 |
/** |
| 89 |
* Retrieves the number of cache entries. |
| 90 |
* |
| 91 |
* @return int |
| 92 |
*/ |
| 93 |
public static function getSize() |
| 94 |
{ |
| 95 |
return (count(self::$cache) - 1); |
| 96 |
|
| 97 |
}//end getSize() |
| 98 |
|
| 99 |
|
| 100 |
}//end class |
| 101 |
|