1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* This file is part of phpFastCache. |
5
|
|
|
* |
6
|
|
|
* @license MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* For full copyright and license information, please see the docs/CREDITS.txt file. |
9
|
|
|
* |
10
|
|
|
* @author Khoa Bui (khoaofgod) <[email protected]> http://www.phpfastcache.com |
11
|
|
|
* @author Georges.L (Geolim4) <[email protected]> |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace phpFastCache\Drivers\Files; |
16
|
|
|
|
17
|
|
|
use phpFastCache\Core\Pool\DriverBaseTrait; |
18
|
|
|
use phpFastCache\Core\Pool\ExtendedCacheItemPoolInterface; |
19
|
|
|
use phpFastCache\Core\Pool\IO\IOHelperTrait; |
20
|
|
|
use phpFastCache\Exceptions\phpFastCacheDriverCheckException; |
21
|
|
|
use phpFastCache\Exceptions\phpFastCacheDriverException; |
22
|
|
|
use phpFastCache\Exceptions\phpFastCacheInvalidArgumentException; |
23
|
|
|
use phpFastCache\Util\Directory; |
24
|
|
|
use Psr\Cache\CacheItemInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class Driver |
28
|
|
|
* @package phpFastCache\Drivers |
29
|
|
|
*/ |
30
|
|
|
class Driver implements ExtendedCacheItemPoolInterface |
31
|
|
|
{ |
32
|
|
|
use DriverBaseTrait, IOHelperTrait; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* |
36
|
|
|
*/ |
37
|
|
|
const FILE_DIR = 'files'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Driver constructor. |
41
|
|
|
* @param array $config |
42
|
|
|
* @throws phpFastCacheDriverException |
43
|
|
|
*/ |
44
|
|
|
public function __construct(array $config = []) |
45
|
|
|
{ |
46
|
|
|
$this->setup($config); |
47
|
|
|
|
48
|
|
|
if (!$this->driverCheck()) { |
49
|
|
|
throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName())); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
public function driverCheck() |
57
|
|
|
{ |
58
|
|
|
return is_writable($this->getPath()) || @mkdir($this->getPath(), $this->getDefaultChmod(), true); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
63
|
|
|
* @return mixed |
64
|
|
|
* @throws phpFastCacheInvalidArgumentException |
65
|
|
|
*/ |
66
|
|
|
protected function driverWrite(CacheItemInterface $item) |
67
|
|
|
{ |
68
|
|
|
/** |
69
|
|
|
* Check for Cross-Driver type confusion |
70
|
|
|
*/ |
71
|
|
|
if ($item instanceof Item) { |
72
|
|
|
$file_path = $this->getFilePath($item->getKey()); |
73
|
|
|
$data = $this->encode($this->driverPreWrap($item)); |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Force write |
77
|
|
|
*/ |
78
|
|
|
try { |
79
|
|
|
return $this->writefile($file_path, $data, $this->config['secureFileManipulation']); |
80
|
|
|
} catch (\Exception $e) { |
81
|
|
|
return false; |
82
|
|
|
} |
83
|
|
|
} else { |
84
|
|
|
throw new phpFastCacheInvalidArgumentException('Cross-Driver type confusion detected'); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
90
|
|
|
* @return mixed |
91
|
|
|
*/ |
92
|
|
|
protected function driverRead(CacheItemInterface $item) |
93
|
|
|
{ |
94
|
|
|
/** |
95
|
|
|
* Check for Cross-Driver type confusion |
96
|
|
|
*/ |
97
|
|
|
$file_path = $this->getFilePath($item->getKey()); |
98
|
|
|
if (!file_exists($file_path)) { |
99
|
|
|
return null; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$content = $this->readfile($file_path); |
103
|
|
|
|
104
|
|
|
return $this->decode($content); |
105
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
110
|
|
|
* @return bool |
111
|
|
|
* @throws phpFastCacheInvalidArgumentException |
112
|
|
|
*/ |
113
|
|
|
protected function driverDelete(CacheItemInterface $item) |
114
|
|
|
{ |
115
|
|
|
/** |
116
|
|
|
* Check for Cross-Driver type confusion |
117
|
|
|
*/ |
118
|
|
|
if ($item instanceof Item) { |
119
|
|
|
$file_path = $this->getFilePath($item->getKey(), true); |
120
|
|
|
if (file_exists($file_path) && @unlink($file_path)) { |
121
|
|
|
$dir = dirname($file_path); |
122
|
|
|
if (!(new \FilesystemIterator($dir))->valid()) { |
123
|
|
|
rmdir($dir); |
124
|
|
|
} |
125
|
|
|
return true; |
126
|
|
|
} else { |
127
|
|
|
return false; |
128
|
|
|
} |
129
|
|
|
} else { |
130
|
|
|
throw new phpFastCacheInvalidArgumentException('Cross-Driver type confusion detected'); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
|
|
protected function driverClear() |
138
|
|
|
{ |
139
|
|
|
return (bool) Directory::rrmdir($this->getPath(true)); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return bool |
144
|
|
|
*/ |
145
|
|
|
protected function driverConnect() |
146
|
|
|
{ |
147
|
|
|
return true; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param string $optionName |
152
|
|
|
* @param mixed $optionValue |
153
|
|
|
* @return bool |
154
|
|
|
* @throws phpFastCacheInvalidArgumentException |
155
|
|
|
*/ |
156
|
|
|
public static function isValidOption($optionName, $optionValue) |
157
|
|
|
{ |
158
|
|
|
DriverBaseTrait::isValidOption($optionName, $optionValue); |
159
|
|
|
switch ($optionName) { |
160
|
|
|
case 'path': |
161
|
|
|
return is_string($optionValue); |
162
|
|
|
break; |
163
|
|
|
|
164
|
|
|
case 'default_chmod': |
165
|
|
|
return is_numeric($optionValue); |
166
|
|
|
break; |
167
|
|
|
|
168
|
|
|
case 'securityKey': |
169
|
|
|
return is_string($optionValue); |
170
|
|
|
break; |
171
|
|
|
case 'htaccess': |
172
|
|
|
return is_bool($optionValue); |
173
|
|
|
break; |
174
|
|
|
|
175
|
|
|
case 'secureFileManipulation': |
176
|
|
|
return is_bool($optionValue); |
177
|
|
|
break; |
178
|
|
|
|
179
|
|
|
default: |
180
|
|
|
return false; |
181
|
|
|
break; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return array |
187
|
|
|
*/ |
188
|
|
|
public static function getValidOptions() |
189
|
|
|
{ |
190
|
|
|
return ['path', 'default_chmod', 'securityKey', 'htaccess', 'secureFileManipulation']; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return array |
195
|
|
|
*/ |
196
|
|
|
public static function getRequiredOptions() |
197
|
|
|
{ |
198
|
|
|
return ['path']; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|