|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Blitz PHP framework. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2022 Dimitri Sitchet Tomkeu <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view |
|
9
|
|
|
* the LICENSE file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace BlitzPHP\Cache\Handlers; |
|
13
|
|
|
|
|
14
|
|
|
use DateInterval; |
|
15
|
|
|
|
|
16
|
|
|
final class FileVarExportHandler extends BaseHandler |
|
17
|
|
|
{ |
|
18
|
|
|
private string $path = FRAMEWORK_STORAGE_PATH . 'cache'; |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* {@inheritDoc} |
|
22
|
|
|
*/ |
|
23
|
|
|
public function isSupported(): bool |
|
24
|
|
|
{ |
|
25
|
|
|
return true; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritDoc} |
|
30
|
|
|
*/ |
|
31
|
|
|
public function init(array $config = []): bool |
|
32
|
|
|
{ |
|
33
|
|
|
return true; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritDoc} |
|
38
|
|
|
* |
|
39
|
|
|
* @param array|bool|float|int|object|string|null $value |
|
40
|
|
|
*/ |
|
41
|
|
|
public function set(string $key, mixed $value, null|DateInterval|int $ttl = null): bool |
|
42
|
|
|
{ |
|
43
|
|
|
$value = var_export($value, true); |
|
44
|
|
|
|
|
45
|
|
|
// Écrire d'abord dans le fichier temporaire pour assurer l'atomicité |
|
46
|
|
|
$tmp = $this->path . "/{$key}." . uniqid('', true) . '.tmp'; |
|
47
|
|
|
file_put_contents($tmp, '<?php return ' . $value . ';', LOCK_EX); |
|
48
|
|
|
|
|
49
|
|
|
return rename($tmp, $this->path . "/{$key}"); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritDoc} |
|
54
|
|
|
*/ |
|
55
|
|
|
public function setMultiple(iterable $values, null|DateInterval|int $ttl = null): bool |
|
56
|
|
|
{ |
|
57
|
|
|
return true; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritDoc} |
|
62
|
|
|
* |
|
63
|
|
|
* @return array|bool|float|int|object|string|null |
|
64
|
|
|
*/ |
|
65
|
|
|
public function get(string $key, mixed $default = null): mixed |
|
66
|
|
|
{ |
|
67
|
|
|
return @include $this->path . "/{$key}"; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritDoc} |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getMultiple(iterable $keys, mixed $default = null): iterable |
|
74
|
|
|
{ |
|
75
|
|
|
return []; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* {@inheritDoc} |
|
80
|
|
|
*/ |
|
81
|
|
|
public function increment(string $key, int $offset = 1) |
|
82
|
|
|
{ |
|
83
|
|
|
return 1; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* {@inheritDoc} |
|
88
|
|
|
*/ |
|
89
|
|
|
public function decrement(string $key, int $offset = 1) |
|
90
|
|
|
{ |
|
91
|
|
|
return 0; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* {@inheritDoc} |
|
96
|
|
|
*/ |
|
97
|
|
|
public function delete(string $key): bool |
|
98
|
|
|
{ |
|
99
|
|
|
return @unlink($this->path . "/{$key}"); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* {@inheritDoc} |
|
104
|
|
|
*/ |
|
105
|
|
|
public function deleteMultiple(iterable $keys): bool |
|
106
|
|
|
{ |
|
107
|
|
|
return true; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* {@inheritDoc} |
|
112
|
|
|
*/ |
|
113
|
|
|
public function clear(): bool |
|
114
|
|
|
{ |
|
115
|
|
|
if (! is_dir($this->path)) { |
|
116
|
|
|
return false; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$files = glob($this->path . '/*'); |
|
120
|
|
|
foreach ($files as $file) { |
|
121
|
|
|
if (is_file($file)) { |
|
122
|
|
|
unlink($file); |
|
123
|
|
|
} elseif (is_dir($file)) { |
|
124
|
|
|
$this->clear($file); |
|
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return rmdir($this->path); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* {@inheritDoc} |
|
133
|
|
|
*/ |
|
134
|
|
|
public function clearGroup(string $group): bool |
|
135
|
|
|
{ |
|
136
|
|
|
return true; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|