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]> https://www.phpfastcache.com |
11
|
|
|
* @author Georges.L (Geolim4) <[email protected]> |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
declare(strict_types=1); |
15
|
|
|
|
16
|
|
|
namespace Phpfastcache\Drivers\Files; |
17
|
|
|
|
18
|
|
|
use Phpfastcache\Core\Pool\{ |
19
|
|
|
DriverBaseTrait, ExtendedCacheItemPoolInterface, IO\IOHelperTrait |
20
|
|
|
}; |
21
|
|
|
use Phpfastcache\Exceptions\{ |
22
|
|
|
PhpfastcacheInvalidArgumentException |
23
|
|
|
}; |
24
|
|
|
use Phpfastcache\Util\Directory; |
25
|
|
|
use Psr\Cache\CacheItemInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class Driver |
29
|
|
|
* @package phpFastCache\Drivers |
30
|
|
|
* @property Config $config Config object |
31
|
|
|
* @method Config getConfig() Return the config object |
32
|
|
|
*/ |
33
|
|
|
class Driver implements ExtendedCacheItemPoolInterface |
34
|
|
|
{ |
35
|
|
|
use IOHelperTrait; |
36
|
|
|
use DriverBaseTrait { |
37
|
|
|
DriverBaseTrait::__construct as private __parentConstruct; |
38
|
|
|
} |
39
|
|
|
/** |
40
|
|
|
* |
41
|
|
|
*/ |
42
|
|
|
const FILE_DIR = 'files'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Driver constructor. |
46
|
|
|
* @param Config $config |
47
|
|
|
* @param string $instanceId |
48
|
|
|
*/ |
49
|
|
|
public function __construct(Config $config, string $instanceId) |
50
|
|
|
{ |
51
|
|
|
$this->__parentConstruct($config, $instanceId); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
public function driverCheck(): bool |
58
|
|
|
{ |
59
|
|
|
return \is_writable($this->getPath()) || @\mkdir($this->getPath(), $this->getDefaultChmod(), true); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
|
|
protected function driverConnect(): bool |
66
|
|
|
{ |
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
72
|
|
|
* @return null|array |
73
|
|
|
*/ |
74
|
|
|
protected function driverRead(CacheItemInterface $item) |
75
|
|
|
{ |
76
|
|
|
/** |
77
|
|
|
* Check for Cross-Driver type confusion |
78
|
|
|
*/ |
79
|
|
|
$file_path = $this->getFilePath($item->getKey(), true); |
80
|
|
|
if (!\file_exists($file_path)) { |
81
|
|
|
return null; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$content = $this->readfile($file_path); |
85
|
|
|
|
86
|
|
|
return $this->decode($content); |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
92
|
|
|
* @return bool |
93
|
|
|
* @throws PhpfastcacheInvalidArgumentException |
94
|
|
|
*/ |
95
|
|
|
protected function driverWrite(CacheItemInterface $item): bool |
96
|
|
|
{ |
97
|
|
|
/** |
98
|
|
|
* Check for Cross-Driver type confusion |
99
|
|
|
*/ |
100
|
|
|
if ($item instanceof Item) { |
101
|
|
|
$file_path = $this->getFilePath($item->getKey()); |
102
|
|
|
$data = $this->encode($this->driverPreWrap($item)); |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Force write |
106
|
|
|
*/ |
107
|
|
|
try { |
108
|
|
|
return $this->writefile($file_path, $data, $this->getConfig()->isSecureFileManipulation()); |
109
|
|
|
} catch (\Exception $e) { |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
119
|
|
|
* @return bool |
120
|
|
|
* @throws PhpfastcacheInvalidArgumentException |
121
|
|
|
*/ |
122
|
|
|
protected function driverDelete(CacheItemInterface $item): bool |
123
|
|
|
{ |
124
|
|
|
/** |
125
|
|
|
* Check for Cross-Driver type confusion |
126
|
|
|
*/ |
127
|
|
|
if ($item instanceof Item) { |
128
|
|
|
$file_path = $this->getFilePath($item->getKey(), true); |
129
|
|
|
if (\file_exists($file_path) && @\unlink($file_path)) { |
130
|
|
|
\clearstatcache(true, $file_path); |
131
|
|
|
$dir = \dirname($file_path); |
132
|
|
|
if (!(new \FilesystemIterator($dir))->valid()) { |
133
|
|
|
\rmdir($dir); |
134
|
|
|
} |
135
|
|
|
return true; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return false; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
|
|
protected function driverClear(): bool |
148
|
|
|
{ |
149
|
|
|
return Directory::rrmdir($this->getPath(true)); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|