-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Driver.php
143 lines (124 loc) · 3.89 KB
/
Driver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Phpfastcache\Drivers\Phparray;
use Exception;
use FilesystemIterator;
use Phpfastcache\Cluster\AggregatablePoolInterface;
use Phpfastcache\Core\Pool\{DriverBaseTrait, ExtendedCacheItemPoolInterface, IO\IOHelperTrait};
use Phpfastcache\Exceptions\{PhpfastcacheInvalidArgumentException, PhpfastcacheIOException};
use Phpfastcache\Util\Directory;
use Psr\Cache\CacheItemInterface;
/**
* Class Driver
* @package phpFastCache\Drivers
* @property Config $config Config object
* @method Config getConfig() Return the config object
*
* Important NOTE:
* We are using getKey instead of getEncodedKey since this backend create filename that are
* managed by defaultFileNameHashFunction and not defaultKeyHashFunction
*/
class Driver implements ExtendedCacheItemPoolInterface, AggregatablePoolInterface
{
use IOHelperTrait;
use DriverBaseTrait {
DriverBaseTrait::__construct as private __parentConstruct;
}
/**
* Driver constructor.
* @param Config $config
* @param string $instanceId
*/
public function __construct(Config $config, string $instanceId)
{
$this->__parentConstruct($config, $instanceId);
}
/**
* @return bool
*/
public function driverCheck(): bool
{
return is_writable($this->getPath()) || mkdir($concurrentDirectory = $this->getPath(), $this->getDefaultChmod(), true) || is_dir($concurrentDirectory);
}
/**
* @return bool
*/
protected function driverConnect(): bool
{
return true;
}
/**
* @param CacheItemInterface $item
* @return null|array
*/
protected function driverRead(CacheItemInterface $item)
{
$file_path = $this->getFilePath($item->getKey(), true);
try{
$content = include $file_path;
}catch (PhpfastcacheIOException $e){
return null;
}
return $content;
}
/**
* @param CacheItemInterface $item
* @return bool
* @throws PhpfastcacheInvalidArgumentException
*/
protected function driverWrite(CacheItemInterface $item): bool
{
/**
* Check for Cross-Driver type confusion
*/
if ($item instanceof Item) {
$file_path = $this->getFilePath($item->getKey());
$data = $this->driverPreWrap($item);
/**
* Force write
*/
try {
return $this->writefile($file_path, "<?php\n" . "return " . var_export($data, true) . ";\n", $this->getConfig()->isSecureFileManipulation());
} catch (Exception $e) {
return false;
}
}
throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected');
}
/**
* @param CacheItemInterface $item
* @return bool
* @throws PhpfastcacheInvalidArgumentException
*/
protected function driverDelete(CacheItemInterface $item): bool
{
/**
* Check for Cross-Driver type confusion
*/
if ($item instanceof Item) {
$file_path = $this->getFilePath($item->getKey(), true);
if (\file_exists($file_path) && @\unlink($file_path)) {
\clearstatcache(true, $file_path);
$dir = \dirname($file_path);
if (!(new FilesystemIterator($dir))->valid()) {
\rmdir($dir);
}
return true;
}
return false;
}
throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected');
}
/**
* @return bool
* @throws \Phpfastcache\Exceptions\PhpfastcacheIOException
*/
protected function driverClear(): bool
{
return Directory::rrmdir($this->getPath(true));
}
}