Passed
Pull Request — master (#178)
by
unknown
04:29 queued 01:31
created

ViewCache::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 12
rs 10
1
<?php
2
3
namespace Quantum\Libraries\ResourceCache;
4
5
use Quantum\Libraries\Storage\FileSystem;
6
use Quantum\Exceptions\DiException;
7
use ReflectionException;
8
use voku\helper\HtmlMin;
9
use Quantum\Di\Di;
10
use Exception;
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 $fs;
28
29
	/**
30
	 * @var ViewCache
31
	 */
32
	private static $instance = null;
33
34
	public static function getInstance(): ViewCache
35
	{
36
		if (self::$instance === null) {
37
			self::$instance = new self();
38
		}
39
40
		return self::$instance;
41
	}
42
43
	/**
44
	 * @throws DiException
45
	 * @throws ReflectionException
46
	 * @throws Exception
47
	 */
48
	public function __construct()
49
	{
50
		$this->fs = Di::get(FileSystem::class);
51
52
		if (!config()->has('view_cache')) {
53
			throw new Exception('The config "view_cache" does not exists.');
54
		}
55
56
		$this->cacheDir = self::getCacheDir();
57
58
		if (!$this->fs->isDirectory($this->cacheDir)) {
59
			mkdir($this->cacheDir, 0777, true);
60
		}
61
	}
62
63
	/**
64
	 * @param string $key
65
	 * @param string $content
66
	 * @param string $sessionId
67
	 * @return ViewCache
68
	 */
69
	public function set(string $key, string $content, string $sessionId): ViewCache
70
	{
71
		if (config()->has('view_cache.minify')) {
72
			$content = $this->minify($content);
73
		}
74
75
		$cacheFile = $this->getCacheFile($key, $sessionId);
76
		$this->fs->put($cacheFile, $content);
77
78
		return $this;
79
	}
80
81
	/**
82
	 * @param string $key
83
	 * @param string $sessionId
84
	 * @param int $ttl
85
	 * @return mixed|null
86
	 */
87
	public function get(string $key, string $sessionId, int $ttl): ?string
88
	{
89
		$cacheFile = $this->getCacheFile($key, $sessionId);
90
		if (!$this->fs->exists($cacheFile)) {
91
			return null;
92
		}
93
94
		$data = $this->fs->get($cacheFile);
95
		if (time() > ($this->fs->lastModified($cacheFile) + $ttl)) {
96
			$this->fs->remove($cacheFile);
97
			return null;
98
		}
99
100
		return $data;
101
	}
102
103
	/**
104
	 * @param string $key
105
	 * @param string $sessionId
106
	 * @return void
107
	 */
108
	public function delete(string $key, string $sessionId): void
109
	{
110
		$cacheFile = $this->getCacheFile($key, $sessionId);
111
		if ($this->fs->exists($cacheFile)) {
112
			$this->fs->remove($cacheFile);
113
		}
114
	}
115
116
	/**
117
	 * @param string $key
118
	 * @param string $sessionId
119
	 * @param int $ttl
120
	 * @return bool
121
	 */
122
	public function exists(string $key, string $sessionId, int $ttl): bool
123
	{
124
		$cacheFile = $this->getCacheFile($key, $sessionId);
125
126
		if (!$this->fs->exists($cacheFile)) {
127
			return false;
128
		}
129
130
		if (time() > ($this->fs->lastModified($cacheFile) + $ttl)) {
131
			$this->fs->remove($cacheFile);
132
			return false;
133
		}
134
135
		return true;
136
	}
137
138
	private static function getCacheDir(): string
139
	{
140
		$configCacheDir = config()->get('view_cache.cache_dir', 'cache');
141
142
		$cacheDir = base_dir() . DS . $configCacheDir . DS . 'views' . DS;
143
144
		if ($module = current_module()) {
145
			$cacheDir = base_dir() . DS . $configCacheDir . DS . 'views' . DS . strtolower($module) . DS;
146
		}
147
148
		return $cacheDir;
149
	}
150
151
	/**
152
	 * @param string $key
153
	 * @param string $sessionId
154
	 * @return string
155
	 */
156
	private function getCacheFile(string $key, string $sessionId): string
157
	{
158
		return $this->cacheDir . md5($key . $sessionId) . $this->mimeType;
159
	}
160
161
	/**
162
	 * @param string $content
163
	 * @return string
164
	 */
165
	private function minify(string $content): string
166
	{
167
		return (new HtmlMin())->minify($content);
168
	}
169
}