1 | <?php |
||||
2 | |||||
3 | namespace CConverter\Cache; |
||||
4 | |||||
5 | class CacheFile |
||||
6 | { |
||||
7 | /** |
||||
8 | * check if is cache dir |
||||
9 | * |
||||
10 | * @return bool |
||||
11 | */ |
||||
12 | public static function isCacheDir() |
||||
13 | { |
||||
14 | if(!is_dir(__DIR__ .'/cache/')) mkdir(__DIR__ ."/cache/", 0700); |
||||
15 | |||||
16 | return true; |
||||
17 | } |
||||
18 | |||||
19 | /** |
||||
20 | * check if cache file exists |
||||
21 | * |
||||
22 | * @return bool |
||||
23 | */ |
||||
24 | public static function isFile() |
||||
25 | { |
||||
26 | $files = glob(__DIR__ .'/cache/*.txt'); |
||||
27 | $count = count($files); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
28 | if($count>0){ |
||||
29 | $output = true; |
||||
30 | }else{ |
||||
31 | $output = false; |
||||
32 | } |
||||
33 | |||||
34 | return $output; |
||||
35 | } |
||||
36 | |||||
37 | /** |
||||
38 | * get name of cache file |
||||
39 | * |
||||
40 | * @return string |
||||
41 | */ |
||||
42 | public static function getFileName() |
||||
43 | { |
||||
44 | $files = glob(__DIR__ .'/cache/*.txt'); |
||||
45 | $file = $files[0]; |
||||
46 | $fileName = explode("/cache/", $file); |
||||
47 | $fileName = $fileName[1]; |
||||
48 | |||||
49 | return $fileName; |
||||
50 | } |
||||
51 | |||||
52 | /** |
||||
53 | * check if cache file is needed to update |
||||
54 | * |
||||
55 | * @return bool |
||||
56 | */ |
||||
57 | public static function isCurrent($file) |
||||
58 | { |
||||
59 | $getSourceName = explode(".", $file); |
||||
60 | $getSourceName = $getSourceName[0]; |
||||
61 | |||||
62 | $current_time = time(); |
||||
63 | if($current_time>$getSourceName){ |
||||
64 | $output = false; |
||||
65 | }else{ |
||||
66 | $output = true; |
||||
67 | } |
||||
68 | |||||
69 | return $output; |
||||
70 | } |
||||
71 | |||||
72 | /** |
||||
73 | * create new cache file |
||||
74 | * |
||||
75 | * @param string|bool $file |
||||
76 | * @param array|string $data |
||||
77 | * @param int $time |
||||
78 | * @return bool |
||||
79 | */ |
||||
80 | public static function setNewCacheFile($file, $data, $time) |
||||
81 | { |
||||
82 | if($file) unlink(__DIR__ .'/cache/'.$file); |
||||
0 ignored issues
–
show
Are you sure
$file of type string|true can be used in concatenation ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
83 | |||||
84 | $current_time = time(); |
||||
85 | $new_time = $current_time + $time*60; |
||||
86 | |||||
87 | $file_name = $new_time.".txt"; |
||||
88 | file_put_contents(__DIR__ .'/cache/'.$file_name, $data); |
||||
89 | |||||
90 | return true; |
||||
91 | } |
||||
92 | |||||
93 | /** |
||||
94 | * get cache data |
||||
95 | * |
||||
96 | * @return string |
||||
97 | */ |
||||
98 | public static function getCache($file) |
||||
99 | { |
||||
100 | $data = file_get_contents(__DIR__ .'/cache/'.$file); |
||||
101 | |||||
102 | return $data; |
||||
103 | } |
||||
104 | } |
||||
105 |