@@ -209,7 +209,7 @@ discard block |
||
209 | 209 | |
210 | 210 | if (!$shouldCache && key_exists('cache', $this->virtualRoutes['*'][$lastKey])) { |
211 | 211 | unset($this->virtualRoutes['*'][$lastKey]['cache']); |
212 | - }else{ |
|
212 | + } else{ |
|
213 | 213 | $this->assignCacheToCurrentRoute($this->virtualRoutes['*'][$lastKey], $shouldCache, $ttl); |
214 | 214 | } |
215 | 215 | |
@@ -222,7 +222,7 @@ discard block |
||
222 | 222 | foreach ($this->virtualRoutes[$lastKeyOfFirstRound] as &$route) { |
223 | 223 | if (!$shouldCache && key_exists('cache', $route)) { |
224 | 224 | unset($route['cache']); |
225 | - }else{ |
|
225 | + } else{ |
|
226 | 226 | $this->assignCacheToCurrentRoute($route, $shouldCache, $ttl); |
227 | 227 | } |
228 | 228 | } |
@@ -77,9 +77,9 @@ discard block |
||
77 | 77 | $this->moduleName = key($module); |
78 | 78 | $this->moduleOptions = $module[key($module)]; |
79 | 79 | |
80 | - if (config()->has('resource_cache') && !config()->has('view_cache')){ |
|
81 | - config()->import(new Setup('config', 'view_cache')); |
|
82 | - } |
|
80 | + if (config()->has('resource_cache') && !config()->has('view_cache')){ |
|
81 | + config()->import(new Setup('config', 'view_cache')); |
|
82 | + } |
|
83 | 83 | } |
84 | 84 | |
85 | 85 | /** |
@@ -98,12 +98,12 @@ discard block |
||
98 | 98 | 'module' => $this->moduleName |
99 | 99 | ]; |
100 | 100 | |
101 | - if ($this->canSetCacheToCurrentRoute()){ |
|
102 | - $this->currentRoute['cache'] = [ |
|
103 | - 'shouldCache' => true, |
|
104 | - 'ttl' => config()->get('view_cache.ttl', 300), |
|
105 | - ]; |
|
106 | - } |
|
101 | + if ($this->canSetCacheToCurrentRoute()){ |
|
102 | + $this->currentRoute['cache'] = [ |
|
103 | + 'shouldCache' => true, |
|
104 | + 'ttl' => config()->get('view_cache.ttl', 300), |
|
105 | + ]; |
|
106 | + } |
|
107 | 107 | |
108 | 108 | if (is_callable($params[0])) { |
109 | 109 | $this->currentRoute['callback'] = $params[0]; |
@@ -196,40 +196,40 @@ discard block |
||
196 | 196 | return $this; |
197 | 197 | } |
198 | 198 | |
199 | - public function cacheable(bool $shouldCache, int $ttl = null): Route |
|
200 | - { |
|
201 | - if (!$ttl) { |
|
202 | - $ttl = config()->get('view_cache.ttl', 300); |
|
203 | - } |
|
204 | - |
|
205 | - if (!empty(session()->getId())){ |
|
206 | - if (!$this->isGroup){ |
|
207 | - end($this->virtualRoutes['*']); |
|
208 | - $lastKey = key($this->virtualRoutes['*']); |
|
209 | - |
|
210 | - if (!$shouldCache && key_exists('cache', $this->virtualRoutes['*'][$lastKey])) { |
|
211 | - unset($this->virtualRoutes['*'][$lastKey]['cache']); |
|
212 | - }else{ |
|
213 | - $this->assignCacheToCurrentRoute($this->virtualRoutes['*'][$lastKey], $shouldCache, $ttl); |
|
214 | - } |
|
215 | - |
|
216 | - return $this; |
|
217 | - } |
|
218 | - |
|
219 | - end($this->virtualRoutes); |
|
220 | - $lastKeyOfFirstRound = key($this->virtualRoutes); |
|
221 | - |
|
222 | - foreach ($this->virtualRoutes[$lastKeyOfFirstRound] as &$route) { |
|
223 | - if (!$shouldCache && key_exists('cache', $route)) { |
|
224 | - unset($route['cache']); |
|
225 | - }else{ |
|
226 | - $this->assignCacheToCurrentRoute($route, $shouldCache, $ttl); |
|
227 | - } |
|
228 | - } |
|
229 | - } |
|
230 | - |
|
231 | - return $this; |
|
232 | - } |
|
199 | + public function cacheable(bool $shouldCache, int $ttl = null): Route |
|
200 | + { |
|
201 | + if (!$ttl) { |
|
202 | + $ttl = config()->get('view_cache.ttl', 300); |
|
203 | + } |
|
204 | + |
|
205 | + if (!empty(session()->getId())){ |
|
206 | + if (!$this->isGroup){ |
|
207 | + end($this->virtualRoutes['*']); |
|
208 | + $lastKey = key($this->virtualRoutes['*']); |
|
209 | + |
|
210 | + if (!$shouldCache && key_exists('cache', $this->virtualRoutes['*'][$lastKey])) { |
|
211 | + unset($this->virtualRoutes['*'][$lastKey]['cache']); |
|
212 | + }else{ |
|
213 | + $this->assignCacheToCurrentRoute($this->virtualRoutes['*'][$lastKey], $shouldCache, $ttl); |
|
214 | + } |
|
215 | + |
|
216 | + return $this; |
|
217 | + } |
|
218 | + |
|
219 | + end($this->virtualRoutes); |
|
220 | + $lastKeyOfFirstRound = key($this->virtualRoutes); |
|
221 | + |
|
222 | + foreach ($this->virtualRoutes[$lastKeyOfFirstRound] as &$route) { |
|
223 | + if (!$shouldCache && key_exists('cache', $route)) { |
|
224 | + unset($route['cache']); |
|
225 | + }else{ |
|
226 | + $this->assignCacheToCurrentRoute($route, $shouldCache, $ttl); |
|
227 | + } |
|
228 | + } |
|
229 | + } |
|
230 | + |
|
231 | + return $this; |
|
232 | + } |
|
233 | 233 | |
234 | 234 | /** |
235 | 235 | * Sets a unique name for a route |
@@ -304,19 +304,19 @@ discard block |
||
304 | 304 | } |
305 | 305 | } |
306 | 306 | |
307 | - private function assignCacheToCurrentRoute(array &$route, bool $shouldCache, int $ttl) |
|
308 | - { |
|
309 | - $route['cache'] = [ |
|
310 | - 'shouldCache' => $shouldCache, |
|
311 | - 'ttl' => $ttl, |
|
312 | - ]; |
|
313 | - } |
|
314 | - |
|
315 | - private function canSetCacheToCurrentRoute(): bool |
|
316 | - { |
|
317 | - return !empty(session()->getId()) && |
|
318 | - config()->get('resource_cache') && |
|
319 | - !empty($this->moduleOptions['cacheable']); |
|
320 | - } |
|
307 | + private function assignCacheToCurrentRoute(array &$route, bool $shouldCache, int $ttl) |
|
308 | + { |
|
309 | + $route['cache'] = [ |
|
310 | + 'shouldCache' => $shouldCache, |
|
311 | + 'ttl' => $ttl, |
|
312 | + ]; |
|
313 | + } |
|
314 | + |
|
315 | + private function canSetCacheToCurrentRoute(): bool |
|
316 | + { |
|
317 | + return !empty(session()->getId()) && |
|
318 | + config()->get('resource_cache') && |
|
319 | + !empty($this->moduleOptions['cacheable']); |
|
320 | + } |
|
321 | 321 | |
322 | 322 | } |
@@ -77,7 +77,7 @@ discard block |
||
77 | 77 | $this->moduleName = key($module); |
78 | 78 | $this->moduleOptions = $module[key($module)]; |
79 | 79 | |
80 | - if (config()->has('resource_cache') && !config()->has('view_cache')){ |
|
80 | + if (config()->has('resource_cache') && !config()->has('view_cache')) { |
|
81 | 81 | config()->import(new Setup('config', 'view_cache')); |
82 | 82 | } |
83 | 83 | } |
@@ -98,7 +98,7 @@ discard block |
||
98 | 98 | 'module' => $this->moduleName |
99 | 99 | ]; |
100 | 100 | |
101 | - if ($this->canSetCacheToCurrentRoute()){ |
|
101 | + if ($this->canSetCacheToCurrentRoute()) { |
|
102 | 102 | $this->currentRoute['cache'] = [ |
103 | 103 | 'shouldCache' => true, |
104 | 104 | 'ttl' => config()->get('view_cache.ttl', 300), |
@@ -202,14 +202,14 @@ discard block |
||
202 | 202 | $ttl = config()->get('view_cache.ttl', 300); |
203 | 203 | } |
204 | 204 | |
205 | - if (!empty(session()->getId())){ |
|
206 | - if (!$this->isGroup){ |
|
205 | + if (!empty(session()->getId())) { |
|
206 | + if (!$this->isGroup) { |
|
207 | 207 | end($this->virtualRoutes['*']); |
208 | 208 | $lastKey = key($this->virtualRoutes['*']); |
209 | 209 | |
210 | 210 | if (!$shouldCache && key_exists('cache', $this->virtualRoutes['*'][$lastKey])) { |
211 | 211 | unset($this->virtualRoutes['*'][$lastKey]['cache']); |
212 | - }else{ |
|
212 | + } else { |
|
213 | 213 | $this->assignCacheToCurrentRoute($this->virtualRoutes['*'][$lastKey], $shouldCache, $ttl); |
214 | 214 | } |
215 | 215 | |
@@ -222,7 +222,7 @@ discard block |
||
222 | 222 | foreach ($this->virtualRoutes[$lastKeyOfFirstRound] as &$route) { |
223 | 223 | if (!$shouldCache && key_exists('cache', $route)) { |
224 | 224 | unset($route['cache']); |
225 | - }else{ |
|
225 | + } else { |
|
226 | 226 | $this->assignCacheToCurrentRoute($route, $shouldCache, $ttl); |
227 | 227 | } |
228 | 228 | } |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | if (filter_var(config()->get('2FA'), FILTER_VALIDATE_BOOLEAN)) { |
100 | 100 | return $this->twoStepVerification($user); |
101 | 101 | } else { |
102 | - session()->regenerateId(); |
|
102 | + session()->regenerateId(); |
|
103 | 103 | session()->set($this->authUserKey, $this->getVisibleFields($user)); |
104 | 104 | return true; |
105 | 105 | } |
@@ -119,7 +119,7 @@ discard block |
||
119 | 119 | { |
120 | 120 | if (session()->has($this->authUserKey)) { |
121 | 121 | session()->delete($this->authUserKey); |
122 | - session()->regenerateId(); |
|
122 | + session()->regenerateId(); |
|
123 | 123 | $this->removeRememberToken(); |
124 | 124 | |
125 | 125 | return true; |
@@ -4,9 +4,9 @@ |
||
4 | 4 | |
5 | 5 | interface ResourceCacheInterface |
6 | 6 | { |
7 | - public function put(); |
|
7 | + public function put(); |
|
8 | 8 | |
9 | - public function get(); |
|
9 | + public function get(); |
|
10 | 10 | |
11 | - public function delete(); |
|
11 | + public function delete(); |
|
12 | 12 | } |
13 | 13 | \ No newline at end of file |
@@ -30,215 +30,215 @@ |
||
30 | 30 | class ResourceCacheClearCommand extends QtCommand |
31 | 31 | { |
32 | 32 | |
33 | - /** |
|
34 | - * Command name |
|
35 | - * @var string |
|
36 | - */ |
|
37 | - protected $name = 'cache:clear'; |
|
38 | - |
|
39 | - /** |
|
40 | - * Command description |
|
41 | - * @var string |
|
42 | - */ |
|
43 | - protected $description = 'Clearing resource caches'; |
|
44 | - |
|
45 | - /** |
|
46 | - * Command help text |
|
47 | - * @var string |
|
48 | - */ |
|
49 | - protected $help = 'The command will clear the resource caches.'; |
|
50 | - |
|
51 | - /** |
|
52 | - * Command options |
|
53 | - * @var array |
|
54 | - */ |
|
55 | - protected $options = [ |
|
56 | - ['all', 'all', 'none', ''], |
|
57 | - ['type', 't', 'required', ''], |
|
58 | - ['module', 'm', 'required', ''] |
|
59 | - ]; |
|
60 | - |
|
61 | - /** |
|
62 | - * @var array |
|
63 | - */ |
|
64 | - protected $types = ['views', 'asserts']; |
|
65 | - |
|
66 | - /** |
|
67 | - * @var array |
|
68 | - */ |
|
69 | - protected $modules; |
|
70 | - |
|
71 | - /** |
|
72 | - * @var string|null |
|
73 | - */ |
|
74 | - protected $type = null; |
|
75 | - |
|
76 | - /** |
|
77 | - * @var string|null |
|
78 | - */ |
|
79 | - protected $module = null; |
|
80 | - |
|
81 | - /** |
|
82 | - * @var string |
|
83 | - */ |
|
84 | - protected $cacheDir; |
|
85 | - |
|
86 | - /** |
|
87 | - * @var object |
|
88 | - */ |
|
89 | - protected $fs; |
|
90 | - |
|
91 | - /** |
|
92 | - * @return void |
|
93 | - * @throws DiException |
|
94 | - * @throws ReflectionException |
|
95 | - */ |
|
96 | - public function exec() |
|
97 | - { |
|
98 | - $this->fs = Di::get(FileSystem::class); |
|
99 | - |
|
100 | - try { |
|
101 | - $this->importConfig(); |
|
102 | - $this->initModule(); |
|
103 | - $this->initType(); |
|
104 | - }catch (Exception $e){ |
|
105 | - $this->error($e->getMessage()); |
|
106 | - return; |
|
107 | - } |
|
108 | - |
|
109 | - |
|
110 | - if ($this->fs->isDirectory($this->cacheDir)) { |
|
111 | - if ($this->module || $this->type) { |
|
112 | - $this->clearResourceFiles($this->cacheDir, $this->module, $this->type); |
|
113 | - } elseif (!empty($this->getOption('all'))) { |
|
114 | - $this->removeFilesInDirectory($this->cacheDir); |
|
115 | - } else { |
|
116 | - $this->error('You must set at least one of these options {--all, --module=moduleName, --type=typeName}!'); |
|
117 | - return; |
|
118 | - } |
|
119 | - } else { |
|
120 | - $this->error('Cache directory does not exist or is not accessible.'); |
|
121 | - return; |
|
122 | - } |
|
123 | - |
|
124 | - $this->info('Resource cache cleared successfully.'); |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * @return void |
|
129 | - */ |
|
130 | - private function importModules() |
|
131 | - { |
|
132 | - try { |
|
133 | - if (!config()->has('modules')) { |
|
134 | - config()->import(new Setup('config', 'modules')); |
|
135 | - } |
|
136 | - $this->modules = array_keys(array_change_key_case(config()->get('modules.modules'))); |
|
137 | - } catch (ConfigException|DiException|ReflectionException $e) { |
|
138 | - $this->error($e->getMessage()); |
|
139 | - return; |
|
140 | - } |
|
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * @return void |
|
145 | - * @throws ConfigException |
|
146 | - * @throws DiException |
|
147 | - * @throws ReflectionException |
|
148 | - */ |
|
149 | - private function importConfig(): void |
|
150 | - { |
|
151 | - if (!config()->has('view_cache')) { |
|
152 | - config()->import(new Setup('config', 'view_cache')); |
|
153 | - } |
|
154 | - |
|
155 | - $this->cacheDir = base_dir() . DS . config()->get('view_cache.cache_dir', 'cache'); |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * @return void |
|
160 | - * @throws Exception |
|
161 | - */ |
|
162 | - private function initModule(): void |
|
163 | - { |
|
164 | - $moduleOption = $this->getOption('module'); |
|
165 | - |
|
166 | - if (!empty($moduleOption)) { |
|
167 | - $this->importModules(); |
|
168 | - $module = strtolower($moduleOption); |
|
169 | - |
|
170 | - if (in_array($module, $this->modules)) { |
|
171 | - $this->module = $module; |
|
172 | - } else { |
|
173 | - throw new Exception('Module {'. $module .'} does not exist.'); |
|
174 | - } |
|
175 | - } |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * @return void |
|
180 | - * @throws Exception |
|
181 | - */ |
|
182 | - private function initType(): void |
|
183 | - { |
|
184 | - $typeOption = $this->getOption('type'); |
|
185 | - |
|
186 | - if (!empty($typeOption)) { |
|
187 | - $type = strtolower($typeOption); |
|
188 | - |
|
189 | - if (in_array($type, $this->types)) { |
|
190 | - $this->type = $type; |
|
191 | - } else { |
|
192 | - throw new Exception('Cache type {'. $type .'} is invalid.'); |
|
193 | - } |
|
194 | - } |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * @param string $dir |
|
199 | - * @param string|null $moduleName |
|
200 | - * @param string|null $type |
|
201 | - * @return void |
|
202 | - */ |
|
203 | - private function clearResourceFiles(string $dir, ?string $moduleName = null, ?string $type = null): void |
|
204 | - { |
|
205 | - if ($type) { |
|
206 | - $dir = $dir . DS . strtolower($type); |
|
207 | - } |
|
208 | - |
|
209 | - if ($moduleName) { |
|
210 | - if (!$type) { |
|
211 | - $dir = $dir . DS . '*'; |
|
212 | - } |
|
213 | - $dir = $dir . DS . strtolower($moduleName); |
|
214 | - } |
|
215 | - |
|
216 | - $this->removeFilesInDirectory($dir); |
|
217 | - } |
|
218 | - |
|
219 | - /** |
|
220 | - * @param string $dir |
|
221 | - * @return void |
|
222 | - */ |
|
223 | - private function removeFilesInDirectory(string $dir): void |
|
224 | - { |
|
225 | - $folders = $this->fs->glob($dir); |
|
226 | - $files = $this->fs->glob($dir . DS . '*'); |
|
227 | - |
|
228 | - foreach ($files as $file) { |
|
229 | - if ($this->fs->isDirectory($file)) { |
|
230 | - $this->removeFilesInDirectory($file); |
|
231 | - } else { |
|
232 | - $this->fs->remove($file); |
|
233 | - } |
|
234 | - } |
|
235 | - |
|
236 | - foreach ($folders as $folder) { |
|
237 | - if (count($this->fs->glob($folder . DS . '*')) === 0 && |
|
238 | - basename($dir) !== config()->get('view_cache.cache_dir', 'cache') |
|
239 | - ) { |
|
240 | - $this->fs->removeDirectory($folder); |
|
241 | - } |
|
242 | - } |
|
243 | - } |
|
33 | + /** |
|
34 | + * Command name |
|
35 | + * @var string |
|
36 | + */ |
|
37 | + protected $name = 'cache:clear'; |
|
38 | + |
|
39 | + /** |
|
40 | + * Command description |
|
41 | + * @var string |
|
42 | + */ |
|
43 | + protected $description = 'Clearing resource caches'; |
|
44 | + |
|
45 | + /** |
|
46 | + * Command help text |
|
47 | + * @var string |
|
48 | + */ |
|
49 | + protected $help = 'The command will clear the resource caches.'; |
|
50 | + |
|
51 | + /** |
|
52 | + * Command options |
|
53 | + * @var array |
|
54 | + */ |
|
55 | + protected $options = [ |
|
56 | + ['all', 'all', 'none', ''], |
|
57 | + ['type', 't', 'required', ''], |
|
58 | + ['module', 'm', 'required', ''] |
|
59 | + ]; |
|
60 | + |
|
61 | + /** |
|
62 | + * @var array |
|
63 | + */ |
|
64 | + protected $types = ['views', 'asserts']; |
|
65 | + |
|
66 | + /** |
|
67 | + * @var array |
|
68 | + */ |
|
69 | + protected $modules; |
|
70 | + |
|
71 | + /** |
|
72 | + * @var string|null |
|
73 | + */ |
|
74 | + protected $type = null; |
|
75 | + |
|
76 | + /** |
|
77 | + * @var string|null |
|
78 | + */ |
|
79 | + protected $module = null; |
|
80 | + |
|
81 | + /** |
|
82 | + * @var string |
|
83 | + */ |
|
84 | + protected $cacheDir; |
|
85 | + |
|
86 | + /** |
|
87 | + * @var object |
|
88 | + */ |
|
89 | + protected $fs; |
|
90 | + |
|
91 | + /** |
|
92 | + * @return void |
|
93 | + * @throws DiException |
|
94 | + * @throws ReflectionException |
|
95 | + */ |
|
96 | + public function exec() |
|
97 | + { |
|
98 | + $this->fs = Di::get(FileSystem::class); |
|
99 | + |
|
100 | + try { |
|
101 | + $this->importConfig(); |
|
102 | + $this->initModule(); |
|
103 | + $this->initType(); |
|
104 | + }catch (Exception $e){ |
|
105 | + $this->error($e->getMessage()); |
|
106 | + return; |
|
107 | + } |
|
108 | + |
|
109 | + |
|
110 | + if ($this->fs->isDirectory($this->cacheDir)) { |
|
111 | + if ($this->module || $this->type) { |
|
112 | + $this->clearResourceFiles($this->cacheDir, $this->module, $this->type); |
|
113 | + } elseif (!empty($this->getOption('all'))) { |
|
114 | + $this->removeFilesInDirectory($this->cacheDir); |
|
115 | + } else { |
|
116 | + $this->error('You must set at least one of these options {--all, --module=moduleName, --type=typeName}!'); |
|
117 | + return; |
|
118 | + } |
|
119 | + } else { |
|
120 | + $this->error('Cache directory does not exist or is not accessible.'); |
|
121 | + return; |
|
122 | + } |
|
123 | + |
|
124 | + $this->info('Resource cache cleared successfully.'); |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * @return void |
|
129 | + */ |
|
130 | + private function importModules() |
|
131 | + { |
|
132 | + try { |
|
133 | + if (!config()->has('modules')) { |
|
134 | + config()->import(new Setup('config', 'modules')); |
|
135 | + } |
|
136 | + $this->modules = array_keys(array_change_key_case(config()->get('modules.modules'))); |
|
137 | + } catch (ConfigException|DiException|ReflectionException $e) { |
|
138 | + $this->error($e->getMessage()); |
|
139 | + return; |
|
140 | + } |
|
141 | + } |
|
142 | + |
|
143 | + /** |
|
144 | + * @return void |
|
145 | + * @throws ConfigException |
|
146 | + * @throws DiException |
|
147 | + * @throws ReflectionException |
|
148 | + */ |
|
149 | + private function importConfig(): void |
|
150 | + { |
|
151 | + if (!config()->has('view_cache')) { |
|
152 | + config()->import(new Setup('config', 'view_cache')); |
|
153 | + } |
|
154 | + |
|
155 | + $this->cacheDir = base_dir() . DS . config()->get('view_cache.cache_dir', 'cache'); |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * @return void |
|
160 | + * @throws Exception |
|
161 | + */ |
|
162 | + private function initModule(): void |
|
163 | + { |
|
164 | + $moduleOption = $this->getOption('module'); |
|
165 | + |
|
166 | + if (!empty($moduleOption)) { |
|
167 | + $this->importModules(); |
|
168 | + $module = strtolower($moduleOption); |
|
169 | + |
|
170 | + if (in_array($module, $this->modules)) { |
|
171 | + $this->module = $module; |
|
172 | + } else { |
|
173 | + throw new Exception('Module {'. $module .'} does not exist.'); |
|
174 | + } |
|
175 | + } |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * @return void |
|
180 | + * @throws Exception |
|
181 | + */ |
|
182 | + private function initType(): void |
|
183 | + { |
|
184 | + $typeOption = $this->getOption('type'); |
|
185 | + |
|
186 | + if (!empty($typeOption)) { |
|
187 | + $type = strtolower($typeOption); |
|
188 | + |
|
189 | + if (in_array($type, $this->types)) { |
|
190 | + $this->type = $type; |
|
191 | + } else { |
|
192 | + throw new Exception('Cache type {'. $type .'} is invalid.'); |
|
193 | + } |
|
194 | + } |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * @param string $dir |
|
199 | + * @param string|null $moduleName |
|
200 | + * @param string|null $type |
|
201 | + * @return void |
|
202 | + */ |
|
203 | + private function clearResourceFiles(string $dir, ?string $moduleName = null, ?string $type = null): void |
|
204 | + { |
|
205 | + if ($type) { |
|
206 | + $dir = $dir . DS . strtolower($type); |
|
207 | + } |
|
208 | + |
|
209 | + if ($moduleName) { |
|
210 | + if (!$type) { |
|
211 | + $dir = $dir . DS . '*'; |
|
212 | + } |
|
213 | + $dir = $dir . DS . strtolower($moduleName); |
|
214 | + } |
|
215 | + |
|
216 | + $this->removeFilesInDirectory($dir); |
|
217 | + } |
|
218 | + |
|
219 | + /** |
|
220 | + * @param string $dir |
|
221 | + * @return void |
|
222 | + */ |
|
223 | + private function removeFilesInDirectory(string $dir): void |
|
224 | + { |
|
225 | + $folders = $this->fs->glob($dir); |
|
226 | + $files = $this->fs->glob($dir . DS . '*'); |
|
227 | + |
|
228 | + foreach ($files as $file) { |
|
229 | + if ($this->fs->isDirectory($file)) { |
|
230 | + $this->removeFilesInDirectory($file); |
|
231 | + } else { |
|
232 | + $this->fs->remove($file); |
|
233 | + } |
|
234 | + } |
|
235 | + |
|
236 | + foreach ($folders as $folder) { |
|
237 | + if (count($this->fs->glob($folder . DS . '*')) === 0 && |
|
238 | + basename($dir) !== config()->get('view_cache.cache_dir', 'cache') |
|
239 | + ) { |
|
240 | + $this->fs->removeDirectory($folder); |
|
241 | + } |
|
242 | + } |
|
243 | + } |
|
244 | 244 | } |
@@ -101,7 +101,7 @@ discard block |
||
101 | 101 | $this->importConfig(); |
102 | 102 | $this->initModule(); |
103 | 103 | $this->initType(); |
104 | - }catch (Exception $e){ |
|
104 | + } catch (Exception $e) { |
|
105 | 105 | $this->error($e->getMessage()); |
106 | 106 | return; |
107 | 107 | } |
@@ -134,7 +134,7 @@ discard block |
||
134 | 134 | config()->import(new Setup('config', 'modules')); |
135 | 135 | } |
136 | 136 | $this->modules = array_keys(array_change_key_case(config()->get('modules.modules'))); |
137 | - } catch (ConfigException|DiException|ReflectionException $e) { |
|
137 | + } catch (ConfigException | DiException | ReflectionException $e) { |
|
138 | 138 | $this->error($e->getMessage()); |
139 | 139 | return; |
140 | 140 | } |
@@ -170,7 +170,7 @@ discard block |
||
170 | 170 | if (in_array($module, $this->modules)) { |
171 | 171 | $this->module = $module; |
172 | 172 | } else { |
173 | - throw new Exception('Module {'. $module .'} does not exist.'); |
|
173 | + throw new Exception('Module {' . $module . '} does not exist.'); |
|
174 | 174 | } |
175 | 175 | } |
176 | 176 | } |
@@ -189,7 +189,7 @@ discard block |
||
189 | 189 | if (in_array($type, $this->types)) { |
190 | 190 | $this->type = $type; |
191 | 191 | } else { |
192 | - throw new Exception('Cache type {'. $type .'} is invalid.'); |
|
192 | + throw new Exception('Cache type {' . $type . '} is invalid.'); |
|
193 | 193 | } |
194 | 194 | } |
195 | 195 | } |
@@ -101,7 +101,7 @@ |
||
101 | 101 | $this->importConfig(); |
102 | 102 | $this->initModule(); |
103 | 103 | $this->initType(); |
104 | - }catch (Exception $e){ |
|
104 | + } catch (Exception $e){ |
|
105 | 105 | $this->error($e->getMessage()); |
106 | 106 | return; |
107 | 107 | } |
@@ -143,22 +143,22 @@ discard block |
||
143 | 143 | $this->params = []; |
144 | 144 | } |
145 | 145 | |
146 | - /** |
|
147 | - * @param string $view |
|
148 | - * @param array $params |
|
149 | - * @return string|null |
|
150 | - * @throws AssetException |
|
151 | - * @throws DiException |
|
152 | - * @throws LangException |
|
153 | - * @throws LoaderError |
|
154 | - * @throws ReflectionException |
|
155 | - * @throws RuntimeError |
|
156 | - * @throws SyntaxError |
|
157 | - * @throws ViewException |
|
158 | - * @throws ConfigException |
|
159 | - * @throws DatabaseException |
|
160 | - * @throws SessionException |
|
161 | - */ |
|
146 | + /** |
|
147 | + * @param string $view |
|
148 | + * @param array $params |
|
149 | + * @return string|null |
|
150 | + * @throws AssetException |
|
151 | + * @throws DiException |
|
152 | + * @throws LangException |
|
153 | + * @throws LoaderError |
|
154 | + * @throws ReflectionException |
|
155 | + * @throws RuntimeError |
|
156 | + * @throws SyntaxError |
|
157 | + * @throws ViewException |
|
158 | + * @throws ConfigException |
|
159 | + * @throws DatabaseException |
|
160 | + * @throws SessionException |
|
161 | + */ |
|
162 | 162 | public function render(string $view, array $params = []): ?string |
163 | 163 | { |
164 | 164 | if (!$this->layout) { |
@@ -179,11 +179,11 @@ discard block |
||
179 | 179 | AssetManager::getInstance()->register($this->assets); |
180 | 180 | } |
181 | 181 | |
182 | - $content = $this->renderFile($this->layout); |
|
182 | + $content = $this->renderFile($this->layout); |
|
183 | 183 | |
184 | - if (route_cache()){ |
|
185 | - (new ViewCache())->set(route_uri(), $content, session()->getId()); |
|
186 | - } |
|
184 | + if (route_cache()){ |
|
185 | + (new ViewCache())->set(route_uri(), $content, session()->getId()); |
|
186 | + } |
|
187 | 187 | |
188 | 188 | return $content; |
189 | 189 | } |
@@ -175,13 +175,13 @@ |
||
175 | 175 | Debugger::updateStoreCell(Debugger::ROUTES, LogLevel::INFO, ['View' => current_module() . '/Views/' . $view]); |
176 | 176 | } |
177 | 177 | |
178 | - if(!empty($this->assets)) { |
|
178 | + if (!empty($this->assets)) { |
|
179 | 179 | AssetManager::getInstance()->register($this->assets); |
180 | 180 | } |
181 | 181 | |
182 | 182 | $content = $this->renderFile($this->layout); |
183 | 183 | |
184 | - if (route_cache()){ |
|
184 | + if (route_cache()) { |
|
185 | 185 | (new ViewCache())->set(route_uri(), $content, session()->getId()); |
186 | 186 | } |
187 | 187 |
@@ -39,125 +39,125 @@ |
||
39 | 39 | class MvcManager |
40 | 40 | { |
41 | 41 | |
42 | - /** |
|
43 | - * @param Request $request |
|
44 | - * @param Response $response |
|
45 | - * @return void |
|
46 | - * @throws ConfigException |
|
47 | - * @throws ControllerException |
|
48 | - * @throws CryptorException |
|
49 | - * @throws CsrfException |
|
50 | - * @throws DiException |
|
51 | - * @throws MiddlewareException |
|
52 | - * @throws ReflectionException |
|
53 | - * @throws DatabaseException |
|
54 | - * @throws LangException |
|
55 | - * @throws SessionException |
|
56 | - */ |
|
57 | - public static function handle(Request $request, Response $response) |
|
58 | - { |
|
59 | - if (current_middlewares()) { |
|
60 | - list($request, $response) = (new MiddlewareManager())->applyMiddlewares($request, $response); |
|
61 | - } |
|
62 | - |
|
63 | - $callback = route_callback(); |
|
64 | - |
|
65 | - if (route_cache() && |
|
66 | - ViewCache::exists(route_uri(), session()->getId(), route_cache()['ttl']) |
|
67 | - ){ |
|
68 | - call_user_func_array(function () use (&$response) { |
|
69 | - $content = (new ViewCache())->get(route_uri(), session()->getId(), route_cache()['ttl']); |
|
70 | - $response->html($content); |
|
71 | - }, []); |
|
72 | - |
|
73 | - }elseif ($callback) { |
|
74 | - call_user_func_array($callback, self::getArgs($callback)); |
|
75 | - } else { |
|
76 | - $controller = self::getController(); |
|
77 | - $action = self::getAction($controller); |
|
78 | - |
|
79 | - if ($controller->csrfVerification && in_array($request->getMethod(), Csrf::METHODS)) { |
|
80 | - csrf()->checkToken($request); |
|
81 | - } |
|
82 | - |
|
83 | - if (method_exists($controller, '__before')) { |
|
84 | - call_user_func_array([$controller, '__before'], self::getArgs([$controller, '__before'])); |
|
85 | - } |
|
86 | - |
|
87 | - call_user_func_array([$controller, $action], self::getArgs([$controller, $action])); |
|
88 | - |
|
89 | - if (method_exists($controller, '__after')) { |
|
90 | - call_user_func_array([$controller, '__after'], self::getArgs([$controller, '__after'])); |
|
91 | - } |
|
92 | - } |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * Get Controller |
|
97 | - * @return QtController |
|
98 | - * @throws DiException |
|
99 | - * @throws ReflectionException |
|
100 | - * @throws ControllerException |
|
101 | - */ |
|
102 | - private static function getController(): QtController |
|
103 | - { |
|
104 | - $fs = Di::get(FileSystem::class); |
|
105 | - |
|
106 | - $controllerPath = modules_dir() . DS . current_module() . DS . 'Controllers' . DS . current_controller() . '.php'; |
|
107 | - |
|
108 | - if (!$fs->exists($controllerPath)) { |
|
109 | - throw ControllerException::controllerNotFound(current_controller()); |
|
110 | - } |
|
111 | - |
|
112 | - require_once $controllerPath; |
|
113 | - |
|
114 | - $controllerClass = '\\Modules\\' . current_module() . '\\Controllers\\' . current_controller(); |
|
115 | - |
|
116 | - if (!class_exists($controllerClass, false)) { |
|
117 | - throw ControllerException::controllerNotDefined(current_controller()); |
|
118 | - } |
|
119 | - |
|
120 | - return new $controllerClass(); |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * Get Action |
|
125 | - * @param QtController $controller |
|
126 | - * @return string|null |
|
127 | - * @throws ControllerException |
|
128 | - */ |
|
129 | - private static function getAction(QtController $controller): ?string |
|
130 | - { |
|
131 | - $action = current_action(); |
|
132 | - |
|
133 | - if ($action && !method_exists($controller, $action)) { |
|
134 | - throw ControllerException::actionNotDefined($action); |
|
135 | - } |
|
136 | - |
|
137 | - return $action; |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Get arguments |
|
142 | - * @param callable $callable |
|
143 | - * @return array |
|
144 | - * @throws DiException |
|
145 | - * @throws ReflectionException |
|
146 | - */ |
|
147 | - private static function getArgs(callable $callable): array |
|
148 | - { |
|
149 | - return Di::autowire($callable, self::routeParams()); |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Gets the route parameters |
|
154 | - * @return array |
|
155 | - */ |
|
156 | - private static function routeParams(): array |
|
157 | - { |
|
158 | - return array_map(function ($param) { |
|
159 | - return $param['value']; |
|
160 | - }, route_params()); |
|
161 | - } |
|
42 | + /** |
|
43 | + * @param Request $request |
|
44 | + * @param Response $response |
|
45 | + * @return void |
|
46 | + * @throws ConfigException |
|
47 | + * @throws ControllerException |
|
48 | + * @throws CryptorException |
|
49 | + * @throws CsrfException |
|
50 | + * @throws DiException |
|
51 | + * @throws MiddlewareException |
|
52 | + * @throws ReflectionException |
|
53 | + * @throws DatabaseException |
|
54 | + * @throws LangException |
|
55 | + * @throws SessionException |
|
56 | + */ |
|
57 | + public static function handle(Request $request, Response $response) |
|
58 | + { |
|
59 | + if (current_middlewares()) { |
|
60 | + list($request, $response) = (new MiddlewareManager())->applyMiddlewares($request, $response); |
|
61 | + } |
|
62 | + |
|
63 | + $callback = route_callback(); |
|
64 | + |
|
65 | + if (route_cache() && |
|
66 | + ViewCache::exists(route_uri(), session()->getId(), route_cache()['ttl']) |
|
67 | + ){ |
|
68 | + call_user_func_array(function () use (&$response) { |
|
69 | + $content = (new ViewCache())->get(route_uri(), session()->getId(), route_cache()['ttl']); |
|
70 | + $response->html($content); |
|
71 | + }, []); |
|
72 | + |
|
73 | + }elseif ($callback) { |
|
74 | + call_user_func_array($callback, self::getArgs($callback)); |
|
75 | + } else { |
|
76 | + $controller = self::getController(); |
|
77 | + $action = self::getAction($controller); |
|
78 | + |
|
79 | + if ($controller->csrfVerification && in_array($request->getMethod(), Csrf::METHODS)) { |
|
80 | + csrf()->checkToken($request); |
|
81 | + } |
|
82 | + |
|
83 | + if (method_exists($controller, '__before')) { |
|
84 | + call_user_func_array([$controller, '__before'], self::getArgs([$controller, '__before'])); |
|
85 | + } |
|
86 | + |
|
87 | + call_user_func_array([$controller, $action], self::getArgs([$controller, $action])); |
|
88 | + |
|
89 | + if (method_exists($controller, '__after')) { |
|
90 | + call_user_func_array([$controller, '__after'], self::getArgs([$controller, '__after'])); |
|
91 | + } |
|
92 | + } |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * Get Controller |
|
97 | + * @return QtController |
|
98 | + * @throws DiException |
|
99 | + * @throws ReflectionException |
|
100 | + * @throws ControllerException |
|
101 | + */ |
|
102 | + private static function getController(): QtController |
|
103 | + { |
|
104 | + $fs = Di::get(FileSystem::class); |
|
105 | + |
|
106 | + $controllerPath = modules_dir() . DS . current_module() . DS . 'Controllers' . DS . current_controller() . '.php'; |
|
107 | + |
|
108 | + if (!$fs->exists($controllerPath)) { |
|
109 | + throw ControllerException::controllerNotFound(current_controller()); |
|
110 | + } |
|
111 | + |
|
112 | + require_once $controllerPath; |
|
113 | + |
|
114 | + $controllerClass = '\\Modules\\' . current_module() . '\\Controllers\\' . current_controller(); |
|
115 | + |
|
116 | + if (!class_exists($controllerClass, false)) { |
|
117 | + throw ControllerException::controllerNotDefined(current_controller()); |
|
118 | + } |
|
119 | + |
|
120 | + return new $controllerClass(); |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * Get Action |
|
125 | + * @param QtController $controller |
|
126 | + * @return string|null |
|
127 | + * @throws ControllerException |
|
128 | + */ |
|
129 | + private static function getAction(QtController $controller): ?string |
|
130 | + { |
|
131 | + $action = current_action(); |
|
132 | + |
|
133 | + if ($action && !method_exists($controller, $action)) { |
|
134 | + throw ControllerException::actionNotDefined($action); |
|
135 | + } |
|
136 | + |
|
137 | + return $action; |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Get arguments |
|
142 | + * @param callable $callable |
|
143 | + * @return array |
|
144 | + * @throws DiException |
|
145 | + * @throws ReflectionException |
|
146 | + */ |
|
147 | + private static function getArgs(callable $callable): array |
|
148 | + { |
|
149 | + return Di::autowire($callable, self::routeParams()); |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Gets the route parameters |
|
154 | + * @return array |
|
155 | + */ |
|
156 | + private static function routeParams(): array |
|
157 | + { |
|
158 | + return array_map(function ($param) { |
|
159 | + return $param['value']; |
|
160 | + }, route_params()); |
|
161 | + } |
|
162 | 162 | |
163 | 163 | } |
@@ -64,8 +64,8 @@ discard block |
||
64 | 64 | |
65 | 65 | if (route_cache() && |
66 | 66 | ViewCache::exists(route_uri(), session()->getId(), route_cache()['ttl']) |
67 | - ){ |
|
68 | - call_user_func_array(function () use (&$response) { |
|
67 | + ) { |
|
68 | + call_user_func_array(function() use (&$response) { |
|
69 | 69 | $content = (new ViewCache())->get(route_uri(), session()->getId(), route_cache()['ttl']); |
70 | 70 | $response->html($content); |
71 | 71 | }, []); |
@@ -155,7 +155,7 @@ discard block |
||
155 | 155 | */ |
156 | 156 | private static function routeParams(): array |
157 | 157 | { |
158 | - return array_map(function ($param) { |
|
158 | + return array_map(function($param) { |
|
159 | 159 | return $param['value']; |
160 | 160 | }, route_params()); |
161 | 161 | } |
@@ -70,7 +70,7 @@ |
||
70 | 70 | $response->html($content); |
71 | 71 | }, []); |
72 | 72 | |
73 | - }elseif ($callback) { |
|
73 | + } elseif ($callback) { |
|
74 | 74 | call_user_func_array($callback, self::getArgs($callback)); |
75 | 75 | } else { |
76 | 76 | $controller = self::getController(); |
@@ -130,7 +130,7 @@ |
||
130 | 130 | */ |
131 | 131 | function route_cache() |
132 | 132 | { |
133 | - return RouteController::getCurrentRoute()['cache'] ?? null; |
|
133 | + return RouteController::getCurrentRoute()['cache'] ?? null; |
|
134 | 134 | } |
135 | 135 | |
136 | 136 | /** |
@@ -11,138 +11,138 @@ |
||
11 | 11 | |
12 | 12 | class ViewCache |
13 | 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 | - } |
|
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 | 148 | } |
149 | 149 | \ No newline at end of file |