1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Quantum\Libraries\ResourceCache; |
4
|
|
|
|
5
|
|
|
use Quantum\Libraries\Storage\FileSystem; |
6
|
|
|
use Quantum\Exceptions\ConfigException; |
7
|
|
|
use Quantum\Exceptions\DiException; |
8
|
|
|
use ReflectionException; |
9
|
|
|
use voku\helper\HtmlMin; |
10
|
|
|
use Quantum\Di\Di; |
11
|
|
|
|
12
|
|
|
class ViewCache |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private $cacheDir; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $mimeType = '.tmp'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var object |
26
|
|
|
*/ |
27
|
|
|
private static $fs; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param bool $fromCommand |
31
|
|
|
* @throws ConfigException |
32
|
|
|
* @throws DiException |
33
|
|
|
* @throws ReflectionException |
34
|
|
|
*/ |
35
|
|
|
public function __construct(bool $fromCommand = false) |
36
|
|
|
{ |
37
|
|
|
self::$fs = Di::get(FileSystem::class); |
38
|
|
|
|
39
|
|
|
if (!config()->has('view_cache') && !$fromCommand) { |
40
|
|
|
throw ConfigException::configCollision('view_cache'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$configCacheDir = config()->get('view_cache.cache_dir', 'cache'); |
44
|
|
|
$module = strtolower(current_module()); |
|
|
|
|
45
|
|
|
|
46
|
|
|
$this->cacheDir = base_dir() . DS . $configCacheDir . DS . 'views' . DS; |
47
|
|
|
|
48
|
|
|
if ($module) { |
49
|
|
|
$this->cacheDir = base_dir() . DS . $configCacheDir . DS . 'views' . DS . $module . DS; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (!self::$fs->isDirectory($this->cacheDir) && !$fromCommand) { |
53
|
|
|
mkdir($this->cacheDir, 0777, true); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $key |
59
|
|
|
* @param string $content |
60
|
|
|
* @param string $sessionId |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
|
|
public function set(string $key, string $content, string $sessionId): void |
64
|
|
|
{ |
65
|
|
|
if (config()->has('view_cache.minify')) { |
66
|
|
|
$content = $this->minify($content); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$cacheFile = $this->getCacheFile($key, $sessionId); |
70
|
|
|
file_put_contents($cacheFile, $content); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $key |
75
|
|
|
* @param string $sessionId |
76
|
|
|
* @param int $ttl |
77
|
|
|
* @return mixed|null |
78
|
|
|
*/ |
79
|
|
|
public function get(string $key, string $sessionId, int $ttl): ?string |
80
|
|
|
{ |
81
|
|
|
$cacheFile = $this->getCacheFile($key, $sessionId); |
82
|
|
|
if (!file_exists($cacheFile)) { |
83
|
|
|
return null; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$data = file_get_contents($cacheFile); |
87
|
|
|
if (time() > (self::$fs->lastModified($cacheFile) + $ttl)) { |
88
|
|
|
self::$fs->remove($cacheFile); |
89
|
|
|
return null; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $data; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $key |
97
|
|
|
* @param string $sessionId |
98
|
|
|
* @return void |
99
|
|
|
*/ |
100
|
|
|
public function delete(string $key, string $sessionId): void |
101
|
|
|
{ |
102
|
|
|
$cacheFile = $this->getCacheFile($key, $sessionId); |
103
|
|
|
if (file_exists($cacheFile)) { |
104
|
|
|
self::$fs->remove($cacheFile); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $key |
110
|
|
|
* @param string $sessionId |
111
|
|
|
* @param int $ttl |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
|
|
public static function exists(string $key, string $sessionId, int $ttl): bool |
115
|
|
|
{ |
116
|
|
|
$cacheFile = (new self())->getCacheFile($key, $sessionId); |
117
|
|
|
|
118
|
|
|
if (!file_exists($cacheFile)) { |
119
|
|
|
return false; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if (time() > (self::$fs->lastModified($cacheFile) + $ttl)) { |
123
|
|
|
self::$fs->remove($cacheFile); |
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return true; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $key |
132
|
|
|
* @param string $sessionId |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
private function getCacheFile(string $key, string $sessionId): string |
136
|
|
|
{ |
137
|
|
|
return $this->cacheDir . md5($key . $sessionId) . $this->mimeType; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param string $content |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
private function minify(string $content): string |
145
|
|
|
{ |
146
|
|
|
return (new HtmlMin())->minify($content); |
147
|
|
|
} |
148
|
|
|
} |