| 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 |
class Cache { |
| 13 |
|
| 14 |
/** |
| 15 |
* The filesystem location of the cache file. |
| 16 |
* |
| 17 |
* @var void |
| 18 |
*/ |
| 19 |
private static $path = ''; |
| 20 |
|
| 21 |
/** |
| 22 |
* The cached data. |
| 23 |
* |
| 24 |
* @var array<string, mixed> |
| 25 |
*/ |
| 26 |
private static $cache = []; |
| 27 |
|
| 28 |
/** |
| 29 |
* Saves the current cache to the filesystem. |
| 30 |
* |
| 31 |
* @return void |
| 32 |
*/ |
| 33 |
public static function save() { |
| 34 |
file_put_contents( self::$path, json_encode( self::$cache ) ); |
| 35 |
|
| 36 |
}//end save() |
| 37 |
|
| 38 |
|
| 39 |
/** |
| 40 |
* Retrieves a single entry from the cache. |
| 41 |
* |
| 42 |
* @param string $key The key of the data to get. If NULL, |
| 43 |
* everything in the cache is returned. |
| 44 |
* |
| 45 |
* @return mixed |
| 46 |
*/ |
| 47 |
public static function get( $key = null ) { |
| 48 |
if ( $key === null ) { |
| 49 |
return self::$cache; |
| 50 |
} |
| 51 |
|
| 52 |
if ( isset( self::$cache[ $key ] ) === true ) { |
| 53 |
return self::$cache[ $key ]; |
| 54 |
} |
| 55 |
|
| 56 |
return false; |
| 57 |
|
| 58 |
}//end get() |
| 59 |
|
| 60 |
|
| 61 |
/** |
| 62 |
* Retrieves a single entry from the cache. |
| 63 |
* |
| 64 |
* @param string $key The key of the data to set. If NULL, |
| 65 |
* sets the entire cache. |
| 66 |
* @param mixed $value The value to set. |
| 67 |
* |
| 68 |
* @return void |
| 69 |
*/ |
| 70 |
public static function set( $key, $value ) { |
| 71 |
if ( $key === null ) { |
| 72 |
self::$cache = $value; |
| 73 |
} else { |
| 74 |
self::$cache[ $key ] = $value; |
| 75 |
} |
| 76 |
|
| 77 |
}//end set() |
| 78 |
|
| 79 |
|
| 80 |
/** |
| 81 |
* Retrieves the number of cache entries. |
| 82 |
* |
| 83 |
* @return int |
| 84 |
*/ |
| 85 |
public static function getSize() { |
| 86 |
return ( count( self::$cache ) - 1 ); |
| 87 |
|
| 88 |
}//end getSize() |
| 89 |
|
| 90 |
|
| 91 |
}//end class |
| 92 |
|