1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the overtrue/phplint. |
5
|
|
|
* |
6
|
|
|
* (c) overtrue <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Overtrue\PHPLint; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Cache. |
16
|
|
|
*/ |
17
|
|
|
class Cache |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected static $filename = '.phplint-cache'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Fetch cache. |
26
|
|
|
* |
27
|
|
|
* @return mixed |
28
|
|
|
*/ |
29
|
|
|
public static function get() |
30
|
|
|
{ |
31
|
|
|
$content = file_get_contents(self::getFilename()); |
32
|
|
|
|
33
|
|
|
return $content ? json_decode($content, true) : null; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Check cache exists. |
38
|
|
|
* |
39
|
|
|
* @return bool |
40
|
|
|
*/ |
41
|
|
|
public static function exists() |
42
|
|
|
{ |
43
|
|
|
return file_exists(self::getFilename()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Alias if exists();. |
48
|
|
|
* |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
|
|
public static function isCached() |
52
|
|
|
{ |
53
|
|
|
return self::exists(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Set cache. |
58
|
|
|
* |
59
|
|
|
* @param mixed $contents |
60
|
|
|
* |
61
|
|
|
* @return int |
62
|
|
|
*/ |
63
|
|
|
public static function put($contents) |
64
|
|
|
{ |
65
|
|
|
return file_put_contents(self::getFilename(), json_encode($contents)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Set cache filename. |
70
|
|
|
* |
71
|
|
|
* @param string $filename |
72
|
|
|
*/ |
73
|
|
|
public static function setFilename($filename) |
74
|
|
|
{ |
75
|
|
|
self::$filename = $filename; |
76
|
|
|
self::makeFolderForFilename(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Try to create the folder recursively where the cache file is stored. |
81
|
|
|
* It depends on current value of static::getFilename(). |
82
|
|
|
*/ |
83
|
|
|
private static function makeFolderForFilename() |
84
|
|
|
{ |
85
|
|
|
$filename = self::getFilename(); |
86
|
|
|
$dirname = dirname($filename); |
87
|
|
|
if (!file_exists($dirname)) { |
88
|
|
|
mkdir($dirname, 0777, true); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Return cache filename. |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public static function getFilename() |
98
|
|
|
{ |
99
|
|
|
return (getcwd() ?: './').'/'.self::$filename; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|