|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Silviooosilva\CacheerPhp\CacheStore\CacheManager; |
|
4
|
|
|
|
|
5
|
|
|
use RecursiveDirectoryIterator; |
|
6
|
|
|
use RecursiveIteratorIterator; |
|
7
|
|
|
use Silviooosilva\CacheerPhp\Exceptions\CacheFileException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class FileCacheManager |
|
11
|
|
|
* @author Sílvio Silva <https://github.com/silviooosilva> |
|
12
|
|
|
* @package Silviooosilva\CacheerPhp |
|
13
|
|
|
*/ |
|
14
|
|
|
class FileCacheManager |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param string $dir |
|
19
|
|
|
* @return void |
|
20
|
|
|
*/ |
|
21
|
|
|
public function createDirectory(string $dir) |
|
22
|
|
|
{ |
|
23
|
|
|
if ((!file_exists($dir) || !is_dir($dir)) && !mkdir($dir, 0755, true)) { |
|
24
|
|
|
throw CacheFileException::create("Could not create directory: {$dir}"); |
|
25
|
|
|
} |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param string $filename |
|
30
|
|
|
* @param string $data |
|
31
|
|
|
* @return void |
|
32
|
|
|
*/ |
|
33
|
|
|
public function writeFile(string $filename, string $data) |
|
34
|
|
|
{ |
|
35
|
|
|
if (!@file_put_contents($filename, $data, LOCK_EX)) { |
|
36
|
|
|
throw CacheFileException::create("Could not write file: {$filename}"); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string $filename |
|
42
|
|
|
* @return string |
|
43
|
|
|
*/ |
|
44
|
|
|
public function readFile(string $filename) |
|
45
|
|
|
{ |
|
46
|
|
|
if (!$this->fileExists($filename)) { |
|
47
|
|
|
throw CacheFileException::create("File not found: {$filename}"); |
|
48
|
|
|
} |
|
49
|
|
|
return file_get_contents($filename); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param string $filename |
|
54
|
|
|
* @return bool |
|
55
|
|
|
*/ |
|
56
|
|
|
public function fileExists(string $filename) |
|
57
|
|
|
{ |
|
58
|
|
|
return file_exists($filename); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string $filename |
|
63
|
|
|
* @return void |
|
64
|
|
|
*/ |
|
65
|
|
|
public function removeFile(string $filename) |
|
66
|
|
|
{ |
|
67
|
|
|
if (file_exists($filename)) { |
|
68
|
|
|
unlink($filename); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string $dir |
|
74
|
|
|
* @return void |
|
75
|
|
|
*/ |
|
76
|
|
|
public function clearDirectory(string $dir) |
|
77
|
|
|
{ |
|
78
|
|
|
$iterator = new RecursiveIteratorIterator( |
|
79
|
|
|
new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS), |
|
80
|
|
|
RecursiveIteratorIterator::CHILD_FIRST |
|
81
|
|
|
); |
|
82
|
|
|
foreach ($iterator as $file) { |
|
83
|
|
|
$path = $file->getPathname(); |
|
84
|
|
|
$file->isDir() ? rmdir($path) : unlink($path); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param mixed $data |
|
90
|
|
|
* @param bool $serialize |
|
91
|
|
|
*/ |
|
92
|
|
|
public function serialize(mixed $data, bool $serialize = true) |
|
93
|
|
|
{ |
|
94
|
|
|
if($serialize) { |
|
95
|
|
|
return serialize($data); |
|
96
|
|
|
} |
|
97
|
|
|
return unserialize($data); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param string $dir |
|
102
|
|
|
* @return array |
|
103
|
|
|
* @throws CacheFileException |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getFilesInDirectory(string $dir) |
|
106
|
|
|
{ |
|
107
|
|
|
if (!is_dir($dir)) { |
|
108
|
|
|
throw CacheFileException::create("Directory does not exist: {$dir}"); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$files = []; |
|
112
|
|
|
$iterator = new RecursiveIteratorIterator( |
|
113
|
|
|
new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS) |
|
114
|
|
|
); |
|
115
|
|
|
|
|
116
|
|
|
foreach ($iterator as $file) { |
|
117
|
|
|
if ($file->isFile()) { |
|
118
|
|
|
$files[] = $file->getPathname(); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return $files; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param string $dir |
|
127
|
|
|
* @return bool |
|
128
|
|
|
*/ |
|
129
|
|
|
public function directoryExists(string $dir) |
|
130
|
|
|
{ |
|
131
|
|
|
return is_dir($dir); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|