Completed
Push — master ( 3d7e33...391d5f )
by Arman
24s queued 20s
created

ViewCache::getCacheFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Quantum\Libraries\ResourceCache;
4
5
use Quantum\Exceptions\DatabaseException;
6
use Quantum\Libraries\Storage\FileSystem;
7
use Quantum\Exceptions\SessionException;
8
use Quantum\Exceptions\ConfigException;
9
use Quantum\Exceptions\LangException;
10
use Quantum\Exceptions\DiException;
11
use Quantum\Loader\Setup;
12
use ReflectionException;
13
use voku\helper\HtmlMin;
14
use Quantum\Di\Di;
15
use Exception;
16
17
class ViewCache
18
{
19
	/**
20
	 * @var string
21
	 */
22
	private $cacheDir;
23
24
	/**
25
	 * @var string
26
	 */
27
	private $sessionId;
28
29
	/**
30
	 * @var string
31
	 */
32
	private $mimeType = '.tmp';
33
34
	/**
35
	 * @var int
36
	 */
37
	private $ttl = 300;
38
39
	/**
40
	 * @var bool
41
	 */
42
	private $isEnabled;
43
44
	/**
45
	 * @var object
46
	 */
47
	private $fs;
48
49
	/**
50
	 * @var ViewCache
51
	 */
52
	private static $instance = null;
53
54
	public static function getInstance(): ViewCache
55
	{
56
		if (self::$instance === null) {
57
			self::$instance = new self();
58
		}
59
60
		return self::$instance;
61
	}
62
63
	/**
64
	 * @throws DiException
65
	 * @throws ReflectionException
66
	 * @throws Exception
67
	 */
68
	public function __construct()
69
	{
70
		if (config()->get('resource_cache')){
71
			config()->import(new Setup('config','view_cache'));
72
		}
73
74
		$this->fs = Di::get(FileSystem::class);
75
76
		$this->cacheDir = $this->getCacheDir();
77
78
		$configTtl  = config()->get('view_cache.ttl');
79
		if (is_int($configTtl)){
80
			$this->ttl = $configTtl;
81
		}
82
83
		$this->sessionId = session()->getId();
84
85
		if (!$this->fs->isDirectory($this->cacheDir)) {
86
			mkdir($this->cacheDir, 0777, true);
87
		}
88
	}
89
90
	/**
91
	 * @param string $key
92
	 * @param string $content
93
	 * @return ViewCache
94
	 */
95
	public function set(string $key, string $content): ViewCache
96
	{
97
		if (config()->has('view_cache.minify')) {
98
			$content = $this->minify($content);
99
		}
100
101
		$this->fs->put($this->getCacheFile($key), $content);
102
103
		return $this;
104
	}
105
106
	/**
107
	 * @param string $key
108
	 * @return mixed|null
109
	 */
110
	public function get(string $key): ?string
111
	{
112
		if (!$this->exists($key)) {
113
			return null;
114
		}
115
116
		return $this->fs->get($this->getCacheFile($key));
117
	}
118
119
	/**
120
	 * @param string $key
121
	 * @return void
122
	 */
123
	public function delete(string $key): void
124
	{
125
		$cacheFile = $this->getCacheFile($key);
126
		if ($this->fs->exists($cacheFile)) {
127
			$this->fs->remove($cacheFile);
128
		}
129
	}
130
131
	/**
132
	 * @param string $key
133
	 * @return bool
134
	 */
135
	public function exists(string $key): bool
136
	{
137
		$cacheFile = $this->getCacheFile($key);
138
139
		if (!$this->fs->exists($cacheFile) || $this->isExpired($cacheFile)) {
140
			return false;
141
		}
142
143
		return true;
144
	}
145
146
	/**
147
	 * @param $cacheFile
148
	 * @return bool
149
	 */
150
	public function isExpired($cacheFile): bool
151
	{
152
		if (time() > ($this->fs->lastModified($cacheFile) + $this->ttl)) {
153
			$this->fs->remove($cacheFile);
154
			return true;
155
		}
156
157
		return false;
158
	}
159
160
	/**
161
	 * @return bool
162
	 * @throws DiException
163
	 * @throws ReflectionException
164
	 * @throws ConfigException
165
	 * @throws DatabaseException
166
	 * @throws LangException
167
	 * @throws SessionException
168
	 */
169
	public function isEnabled(): bool
170
	{
171
		if (!is_null($this->isEnabled)){
0 ignored issues
show
introduced by
The condition is_null($this->isEnabled) is always false.
Loading history...
172
			return $this->isEnabled;
173
		}
174
175
		$resourceCache = config()->get('resource_cache');
176
177
		if (is_bool($resourceCache) && $resourceCache && !empty(session()->getId())){
178
			return true;
179
		}
180
181
		return false;
182
	}
183
184
	/**
185
	 * @param int $ttl
186
	 * @return void
187
	 */
188
	public function setTtl(int $ttl): void
189
	{
190
		$this->ttl = $ttl;
191
	}
192
193
	public function enableCaching(bool $state): void
194
	{
195
		$this->isEnabled = $state;
196
	}
197
198
	/**
199
	 * @return string
200
	 */
201
	private function getCacheDir(): string
202
	{
203
		$configCacheDir = config()->get('view_cache.cache_dir', 'cache');
204
205
		$cacheDir = base_dir() . DS . $configCacheDir . DS . 'views' . DS;
206
207
		if ($module = current_module()) {
208
			$cacheDir = base_dir() . DS . $configCacheDir . DS . 'views' . DS . strtolower($module) . DS;
209
		}
210
211
		return $cacheDir;
212
	}
213
214
	/**
215
	 * @param string $key
216
	 * @return string
217
	 */
218
	private function getCacheFile(string $key): string
219
	{
220
		return $this->cacheDir . md5($key . $this->sessionId) . $this->mimeType;
221
	}
222
223
	/**
224
	 * @param string $content
225
	 * @return string
226
	 */
227
	private function minify(string $content): string
228
	{
229
		return (new HtmlMin())->minify($content);
230
	}
231
}