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
|
|
|
|
45
|
|
|
$this->cacheDir = base_dir() . DS . $configCacheDir . DS . 'views' . DS; |
46
|
|
|
|
47
|
|
|
if ($module = current_module()) { |
48
|
|
|
$this->cacheDir = base_dir() . DS . $configCacheDir . DS . 'views' . DS . strtolower($module) . DS; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (!self::$fs->isDirectory($this->cacheDir) && !$fromCommand) { |
52
|
|
|
mkdir($this->cacheDir, 0777, true); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $key |
58
|
|
|
* @param string $content |
59
|
|
|
* @param string $sessionId |
60
|
|
|
* @return ViewCache |
61
|
|
|
*/ |
62
|
|
|
public function set(string $key, string $content, string $sessionId): ViewCache |
63
|
|
|
{ |
64
|
|
|
if (config()->has('view_cache.minify')) { |
65
|
|
|
$content = $this->minify($content); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$cacheFile = $this->getCacheFile($key, $sessionId); |
69
|
|
|
self::$fs->put($cacheFile, $content); |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $key |
76
|
|
|
* @param string $sessionId |
77
|
|
|
* @param int $ttl |
78
|
|
|
* @return mixed|null |
79
|
|
|
*/ |
80
|
|
|
public function get(string $key, string $sessionId, int $ttl): ?string |
81
|
|
|
{ |
82
|
|
|
$cacheFile = $this->getCacheFile($key, $sessionId); |
83
|
|
|
if (!self::$fs->exists($cacheFile)) { |
84
|
|
|
return null; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$data = self::$fs->get($cacheFile); |
88
|
|
|
if (time() > (self::$fs->lastModified($cacheFile) + $ttl)) { |
89
|
|
|
self::$fs->remove($cacheFile); |
90
|
|
|
return null; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $data; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $key |
98
|
|
|
* @param string $sessionId |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
|
|
public function delete(string $key, string $sessionId): void |
102
|
|
|
{ |
103
|
|
|
$cacheFile = $this->getCacheFile($key, $sessionId); |
104
|
|
|
if (self::$fs->exists($cacheFile)) { |
105
|
|
|
self::$fs->remove($cacheFile); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $key |
111
|
|
|
* @param string $sessionId |
112
|
|
|
* @param int $ttl |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
|
|
public static function exists(string $key, string $sessionId, int $ttl): bool |
116
|
|
|
{ |
117
|
|
|
$cacheFile = (new self())->getCacheFile($key, $sessionId); |
118
|
|
|
|
119
|
|
|
if (!self::$fs->exists($cacheFile)) { |
120
|
|
|
return false; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if (time() > (self::$fs->lastModified($cacheFile) + $ttl)) { |
124
|
|
|
self::$fs->remove($cacheFile); |
125
|
|
|
return false; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return true; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param string $key |
133
|
|
|
* @param string $sessionId |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
private function getCacheFile(string $key, string $sessionId): string |
137
|
|
|
{ |
138
|
|
|
return $this->cacheDir . md5($key . $sessionId) . $this->mimeType; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string $content |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
|
|
private function minify(string $content): string |
146
|
|
|
{ |
147
|
|
|
return (new HtmlMin())->minify($content); |
148
|
|
|
} |
149
|
|
|
} |