Passed
Pull Request — master (#178)
by
unknown
02:51
created

ViewCache::__construct()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 11
c 1
b 0
f 0
nc 9
nop 0
dl 0
loc 20
rs 9.6111
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;
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
			try {
72
				config()->import(new Setup('config','view_cache'));
73
			}catch (Exception $exception){
74
				throw new Exception($exception->getMessage());
75
			}
76
		}
77
78
		$this->fs = Di::get(FileSystem::class);
79
80
		$this->cacheDir = $this->getCacheDir();
81
82
		$this->ttl = is_int(config()->get('view_cache.ttl')) ? config()->get('view_cache.ttl') : 300;
83
84
		$this->sessionId = session()->getId();
85
86
		if (!$this->fs->isDirectory($this->cacheDir)) {
87
			mkdir($this->cacheDir, 0777, true);
88
		}
89
	}
90
91
	/**
92
	 * @param string $key
93
	 * @param string $content
94
	 * @return ViewCache
95
	 */
96
	public function set(string $key, string $content): ViewCache
97
	{
98
		if (config()->has('view_cache.minify')) {
99
			$content = $this->minify($content);
100
		}
101
102
		$cacheFile = $this->getCacheFile($key);
103
		$this->fs->put($cacheFile, $content);
104
105
		return $this;
106
	}
107
108
	/**
109
	 * @param string $key
110
	 * @return mixed|null
111
	 */
112
	public function get(string $key): ?string
113
	{
114
		$cacheFile = $this->getCacheFile($key);
115
116
		if (!$this->exists($key)) {
117
			return null;
118
		}
119
120
		return $this->fs->get($cacheFile);
121
	}
122
123
	/**
124
	 * @param string $key
125
	 * @return void
126
	 */
127
	public function delete(string $key): void
128
	{
129
		$cacheFile = $this->getCacheFile($key);
130
		if ($this->fs->exists($cacheFile)) {
131
			$this->fs->remove($cacheFile);
132
		}
133
	}
134
135
	/**
136
	 * @param string $key
137
	 * @return bool
138
	 */
139
	public function exists(string $key): bool
140
	{
141
		$cacheFile = $this->getCacheFile($key);
142
143
		if (!$this->fs->exists($cacheFile) || $this->isExpired($cacheFile)) {
144
			return false;
145
		}
146
147
		return true;
148
	}
149
150
	/**
151
	 * @param $cacheFile
152
	 * @return bool
153
	 */
154
	public function isExpired($cacheFile): bool
155
	{
156
		if (time() > ($this->fs->lastModified($cacheFile) + $this->ttl)) {
157
			$this->fs->remove($cacheFile);
158
			return true;
159
		}
160
161
		return false;
162
	}
163
164
	/**
165
	 * @return bool
166
	 * @throws DiException
167
	 * @throws ReflectionException
168
	 * @throws ConfigException
169
	 * @throws DatabaseException
170
	 * @throws LangException
171
	 * @throws SessionException
172
	 */
173
	public function isEnabled(): bool
174
	{
175
		if (!is_null($this->isEnabled)){
0 ignored issues
show
introduced by
The condition is_null($this->isEnabled) is always false.
Loading history...
176
			return $this->isEnabled;
177
		}
178
179
		if (is_bool(config()->get('resource_cache')) && config()->get('resource_cache') && !empty(session()->getId())){
180
			return true;
181
		}
182
183
		return false;
184
	}
185
186
	/**
187
	 * @param int $ttl
188
	 * @return void
189
	 */
190
	public function setTtl(int $ttl): void
191
	{
192
		$this->ttl = $ttl;
193
	}
194
195
	public function setIsEnabled(bool $enabled): void
196
	{
197
		$this->isEnabled = $enabled;
198
	}
199
200
	/**
201
	 * @return string
202
	 */
203
	private function getCacheDir(): string
204
	{
205
		$configCacheDir = config()->get('view_cache.cache_dir', 'cache');
206
207
		$cacheDir = base_dir() . DS . $configCacheDir . DS . 'views' . DS;
208
209
		if ($module = current_module()) {
210
			$cacheDir = base_dir() . DS . $configCacheDir . DS . 'views' . DS . strtolower($module) . DS;
211
		}
212
213
		return $cacheDir;
214
	}
215
216
	/**
217
	 * @param string $key
218
	 * @return string
219
	 */
220
	private function getCacheFile(string $key): string
221
	{
222
		return $this->cacheDir . md5($key . $this->sessionId) . $this->mimeType;
223
	}
224
225
	/**
226
	 * @param string $content
227
	 * @return string
228
	 */
229
	private function minify(string $content): string
230
	{
231
		return (new HtmlMin())->minify($content);
232
	}
233
}