@@ -27,198 +27,198 @@ |
||
27 | 27 | class ResourceCacheClearCommand extends QtCommand |
28 | 28 | { |
29 | 29 | |
30 | - /** |
|
31 | - * Command name |
|
32 | - * @var string |
|
33 | - */ |
|
34 | - protected $name = 'resource_cache:clear'; |
|
35 | - |
|
36 | - /** |
|
37 | - * Command description |
|
38 | - * @var string |
|
39 | - */ |
|
40 | - protected $description = 'Clearing resource caches'; |
|
41 | - |
|
42 | - /** |
|
43 | - * Command help text |
|
44 | - * @var string |
|
45 | - */ |
|
46 | - protected $help = 'The command will clear the resource caches.'; |
|
47 | - |
|
48 | - /** |
|
49 | - * Command options |
|
50 | - * @var array |
|
51 | - */ |
|
52 | - protected $options = [ |
|
53 | - ['all', 'all', 'none', ''], |
|
54 | - ['type', 't', 'required', ''], |
|
55 | - ['module', 'm', 'required', ''] |
|
56 | - ]; |
|
57 | - |
|
58 | - /** |
|
59 | - * @var array |
|
60 | - */ |
|
61 | - protected $types = ['views', 'asserts']; |
|
62 | - |
|
63 | - /** |
|
64 | - * @var array |
|
65 | - */ |
|
66 | - protected $modules; |
|
67 | - |
|
68 | - /** |
|
69 | - * @var string|null |
|
70 | - */ |
|
71 | - protected $type = null; |
|
72 | - |
|
73 | - /** |
|
74 | - * @var string|null |
|
75 | - */ |
|
76 | - protected $module = null; |
|
77 | - |
|
78 | - /** |
|
79 | - * @var string |
|
80 | - */ |
|
81 | - protected $cacheDir; |
|
82 | - |
|
83 | - /** |
|
84 | - * @return void |
|
85 | - */ |
|
86 | - public function exec() |
|
87 | - { |
|
88 | - $this->importConfig(); |
|
89 | - $this->initModule(); |
|
90 | - $this->initType(); |
|
91 | - |
|
92 | - if (is_dir($this->cacheDir)) { |
|
93 | - if ($this->module || $this->type) { |
|
94 | - $this->clearResourceFiles($this->cacheDir, $this->module, $this->type); |
|
95 | - } elseif (!empty($this->getOption('all'))) { |
|
96 | - $this->removeFilesInDirectory($this->cacheDir); |
|
97 | - } |
|
98 | - } else { |
|
99 | - $this->error('Cache directory does not exist or is not accessible.'); |
|
100 | - exit(); |
|
101 | - } |
|
102 | - |
|
103 | - $this->info('Resource cache cleared successfully.'); |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * @return void |
|
108 | - */ |
|
109 | - private function importModules() |
|
110 | - { |
|
111 | - try { |
|
112 | - if (!config()->has('modules')) { |
|
113 | - config()->import(new Setup('config', 'modules')); |
|
114 | - } |
|
115 | - $this->modules = array_keys(array_change_key_case(config()->get('modules')['modules'])); |
|
116 | - } catch (ConfigException|DiException|ReflectionException $e) { |
|
117 | - $this->error($e->getMessage()); |
|
118 | - exit(); |
|
119 | - } |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * @return void |
|
124 | - */ |
|
125 | - private function importConfig(): void |
|
126 | - { |
|
127 | - if (!config()->has('view_cache')) { |
|
128 | - try { |
|
129 | - config()->import(new Setup('config', 'view_cache')); |
|
130 | - } catch (\Exception $e) { |
|
131 | - $this->error('Error loading configuration for view_cache.'); |
|
132 | - } |
|
133 | - } |
|
134 | - |
|
135 | - $this->cacheDir = base_dir() . DS . config()->get('view_cache.cache_dir', 'cache'); |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * @return void |
|
140 | - */ |
|
141 | - private function initModule(): void |
|
142 | - { |
|
143 | - $moduleOption = $this->getOption('module'); |
|
144 | - |
|
145 | - if (!empty($moduleOption)) { |
|
146 | - $this->importModules(); |
|
147 | - $module = strtolower($moduleOption); |
|
148 | - |
|
149 | - if (in_array($module, $this->modules)) { |
|
150 | - $this->module = $module; |
|
151 | - } else { |
|
152 | - $this->error("Module '{$module}' does not exist."); |
|
153 | - exit(); |
|
154 | - } |
|
155 | - } |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * @return void |
|
160 | - */ |
|
161 | - private function initType(): void |
|
162 | - { |
|
163 | - $typeOption = $this->getOption('type'); |
|
164 | - |
|
165 | - if (!empty($typeOption)) { |
|
166 | - $type = strtolower($typeOption); |
|
167 | - |
|
168 | - if (in_array($type, $this->types)) { |
|
169 | - $this->type = $type; |
|
170 | - } else { |
|
171 | - $this->error("Cache type '{$type}' is invalid."); |
|
172 | - exit(); |
|
173 | - } |
|
174 | - } |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * @param string $dir |
|
179 | - * @param string|null $moduleName |
|
180 | - * @param string|null $type |
|
181 | - * @return void |
|
182 | - */ |
|
183 | - private function clearResourceFiles(string $dir, ?string $moduleName = null, ?string $type = null): void |
|
184 | - { |
|
185 | - if ($type) { |
|
186 | - $dir = $dir . DS . strtolower($type); |
|
187 | - } |
|
188 | - |
|
189 | - if ($moduleName) { |
|
190 | - if (!$type) { |
|
191 | - $dir = $dir . DS . '*'; |
|
192 | - } |
|
193 | - $dir = $dir . DS . strtolower($moduleName); |
|
194 | - } |
|
195 | - |
|
196 | - $this->removeFilesInDirectory($dir); |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * @param string $dir |
|
201 | - * @return void |
|
202 | - */ |
|
203 | - private function removeFilesInDirectory(string $dir): void |
|
204 | - { |
|
205 | - $folders = glob($dir); |
|
206 | - $files = glob($dir . '/*'); |
|
207 | - |
|
208 | - foreach ($files as $file) { |
|
209 | - if (is_dir($file)) { |
|
210 | - $this->removeFilesInDirectory($file); |
|
211 | - } else { |
|
212 | - unlink($file); |
|
213 | - } |
|
214 | - } |
|
215 | - |
|
216 | - foreach ($folders as $folder) { |
|
217 | - if (count(glob($folder . '/*')) === 0 && |
|
218 | - basename($dir) !== config()->get('view_cache.cache_dir', 'cache') |
|
219 | - ) { |
|
220 | - rmdir($folder); |
|
221 | - } |
|
222 | - } |
|
223 | - } |
|
30 | + /** |
|
31 | + * Command name |
|
32 | + * @var string |
|
33 | + */ |
|
34 | + protected $name = 'resource_cache:clear'; |
|
35 | + |
|
36 | + /** |
|
37 | + * Command description |
|
38 | + * @var string |
|
39 | + */ |
|
40 | + protected $description = 'Clearing resource caches'; |
|
41 | + |
|
42 | + /** |
|
43 | + * Command help text |
|
44 | + * @var string |
|
45 | + */ |
|
46 | + protected $help = 'The command will clear the resource caches.'; |
|
47 | + |
|
48 | + /** |
|
49 | + * Command options |
|
50 | + * @var array |
|
51 | + */ |
|
52 | + protected $options = [ |
|
53 | + ['all', 'all', 'none', ''], |
|
54 | + ['type', 't', 'required', ''], |
|
55 | + ['module', 'm', 'required', ''] |
|
56 | + ]; |
|
57 | + |
|
58 | + /** |
|
59 | + * @var array |
|
60 | + */ |
|
61 | + protected $types = ['views', 'asserts']; |
|
62 | + |
|
63 | + /** |
|
64 | + * @var array |
|
65 | + */ |
|
66 | + protected $modules; |
|
67 | + |
|
68 | + /** |
|
69 | + * @var string|null |
|
70 | + */ |
|
71 | + protected $type = null; |
|
72 | + |
|
73 | + /** |
|
74 | + * @var string|null |
|
75 | + */ |
|
76 | + protected $module = null; |
|
77 | + |
|
78 | + /** |
|
79 | + * @var string |
|
80 | + */ |
|
81 | + protected $cacheDir; |
|
82 | + |
|
83 | + /** |
|
84 | + * @return void |
|
85 | + */ |
|
86 | + public function exec() |
|
87 | + { |
|
88 | + $this->importConfig(); |
|
89 | + $this->initModule(); |
|
90 | + $this->initType(); |
|
91 | + |
|
92 | + if (is_dir($this->cacheDir)) { |
|
93 | + if ($this->module || $this->type) { |
|
94 | + $this->clearResourceFiles($this->cacheDir, $this->module, $this->type); |
|
95 | + } elseif (!empty($this->getOption('all'))) { |
|
96 | + $this->removeFilesInDirectory($this->cacheDir); |
|
97 | + } |
|
98 | + } else { |
|
99 | + $this->error('Cache directory does not exist or is not accessible.'); |
|
100 | + exit(); |
|
101 | + } |
|
102 | + |
|
103 | + $this->info('Resource cache cleared successfully.'); |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * @return void |
|
108 | + */ |
|
109 | + private function importModules() |
|
110 | + { |
|
111 | + try { |
|
112 | + if (!config()->has('modules')) { |
|
113 | + config()->import(new Setup('config', 'modules')); |
|
114 | + } |
|
115 | + $this->modules = array_keys(array_change_key_case(config()->get('modules')['modules'])); |
|
116 | + } catch (ConfigException|DiException|ReflectionException $e) { |
|
117 | + $this->error($e->getMessage()); |
|
118 | + exit(); |
|
119 | + } |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * @return void |
|
124 | + */ |
|
125 | + private function importConfig(): void |
|
126 | + { |
|
127 | + if (!config()->has('view_cache')) { |
|
128 | + try { |
|
129 | + config()->import(new Setup('config', 'view_cache')); |
|
130 | + } catch (\Exception $e) { |
|
131 | + $this->error('Error loading configuration for view_cache.'); |
|
132 | + } |
|
133 | + } |
|
134 | + |
|
135 | + $this->cacheDir = base_dir() . DS . config()->get('view_cache.cache_dir', 'cache'); |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * @return void |
|
140 | + */ |
|
141 | + private function initModule(): void |
|
142 | + { |
|
143 | + $moduleOption = $this->getOption('module'); |
|
144 | + |
|
145 | + if (!empty($moduleOption)) { |
|
146 | + $this->importModules(); |
|
147 | + $module = strtolower($moduleOption); |
|
148 | + |
|
149 | + if (in_array($module, $this->modules)) { |
|
150 | + $this->module = $module; |
|
151 | + } else { |
|
152 | + $this->error("Module '{$module}' does not exist."); |
|
153 | + exit(); |
|
154 | + } |
|
155 | + } |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * @return void |
|
160 | + */ |
|
161 | + private function initType(): void |
|
162 | + { |
|
163 | + $typeOption = $this->getOption('type'); |
|
164 | + |
|
165 | + if (!empty($typeOption)) { |
|
166 | + $type = strtolower($typeOption); |
|
167 | + |
|
168 | + if (in_array($type, $this->types)) { |
|
169 | + $this->type = $type; |
|
170 | + } else { |
|
171 | + $this->error("Cache type '{$type}' is invalid."); |
|
172 | + exit(); |
|
173 | + } |
|
174 | + } |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * @param string $dir |
|
179 | + * @param string|null $moduleName |
|
180 | + * @param string|null $type |
|
181 | + * @return void |
|
182 | + */ |
|
183 | + private function clearResourceFiles(string $dir, ?string $moduleName = null, ?string $type = null): void |
|
184 | + { |
|
185 | + if ($type) { |
|
186 | + $dir = $dir . DS . strtolower($type); |
|
187 | + } |
|
188 | + |
|
189 | + if ($moduleName) { |
|
190 | + if (!$type) { |
|
191 | + $dir = $dir . DS . '*'; |
|
192 | + } |
|
193 | + $dir = $dir . DS . strtolower($moduleName); |
|
194 | + } |
|
195 | + |
|
196 | + $this->removeFilesInDirectory($dir); |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * @param string $dir |
|
201 | + * @return void |
|
202 | + */ |
|
203 | + private function removeFilesInDirectory(string $dir): void |
|
204 | + { |
|
205 | + $folders = glob($dir); |
|
206 | + $files = glob($dir . '/*'); |
|
207 | + |
|
208 | + foreach ($files as $file) { |
|
209 | + if (is_dir($file)) { |
|
210 | + $this->removeFilesInDirectory($file); |
|
211 | + } else { |
|
212 | + unlink($file); |
|
213 | + } |
|
214 | + } |
|
215 | + |
|
216 | + foreach ($folders as $folder) { |
|
217 | + if (count(glob($folder . '/*')) === 0 && |
|
218 | + basename($dir) !== config()->get('view_cache.cache_dir', 'cache') |
|
219 | + ) { |
|
220 | + rmdir($folder); |
|
221 | + } |
|
222 | + } |
|
223 | + } |
|
224 | 224 | } |
@@ -113,7 +113,7 @@ |
||
113 | 113 | config()->import(new Setup('config', 'modules')); |
114 | 114 | } |
115 | 115 | $this->modules = array_keys(array_change_key_case(config()->get('modules')['modules'])); |
116 | - } catch (ConfigException|DiException|ReflectionException $e) { |
|
116 | + } catch (ConfigException | DiException | ReflectionException $e) { |
|
117 | 117 | $this->error($e->getMessage()); |
118 | 118 | exit(); |
119 | 119 | } |
@@ -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 (isset($_COOKIE['PHPSESSID'])){ |
|
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 (isset($_COOKIE['PHPSESSID'])){ |
|
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,22 +304,22 @@ 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 isset($_COOKIE['PHPSESSID']) && |
|
318 | - config()->has('resource_cache') && |
|
319 | - config()->get('resource_cache') && |
|
320 | - !empty($this->moduleOptions) && |
|
321 | - isset($this->moduleOptions['cacheable']) && |
|
322 | - $this->moduleOptions['cacheable']; |
|
323 | - } |
|
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 isset($_COOKIE['PHPSESSID']) && |
|
318 | + config()->has('resource_cache') && |
|
319 | + config()->get('resource_cache') && |
|
320 | + !empty($this->moduleOptions) && |
|
321 | + isset($this->moduleOptions['cacheable']) && |
|
322 | + $this->moduleOptions['cacheable']; |
|
323 | + } |
|
324 | 324 | |
325 | 325 | } |
@@ -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 (isset($_COOKIE['PHPSESSID'])){ |
|
206 | - if (!$this->isGroup){ |
|
205 | + if (isset($_COOKIE['PHPSESSID'])) { |
|
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 | } |
@@ -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 | } |
@@ -175,13 +175,13 @@ |
||
175 | 175 | AssetManager::getInstance()->register($this->assets); |
176 | 176 | } |
177 | 177 | |
178 | - $content = $this->renderFile($this->layout); |
|
178 | + $content = $this->renderFile($this->layout); |
|
179 | 179 | |
180 | - $currentRoute = RouteController::getCurrentRoute(); |
|
180 | + $currentRoute = RouteController::getCurrentRoute(); |
|
181 | 181 | |
182 | - if (isset($currentRoute['cache'])){ |
|
183 | - (new ViewCache())->set($currentRoute['uri'], $content, $_COOKIE['PHPSESSID'], $currentRoute['cache']['ttl']); |
|
184 | - } |
|
182 | + if (isset($currentRoute['cache'])){ |
|
183 | + (new ViewCache())->set($currentRoute['uri'], $content, $_COOKIE['PHPSESSID'], $currentRoute['cache']['ttl']); |
|
184 | + } |
|
185 | 185 | |
186 | 186 | return $content; |
187 | 187 | } |
@@ -171,7 +171,7 @@ discard block |
||
171 | 171 | Debugger::updateStoreCell(Debugger::ROUTES, LogLevel::INFO, ['View' => current_module() . '/Views/' . $view]); |
172 | 172 | } |
173 | 173 | |
174 | - if(!empty($this->assets)) { |
|
174 | + if (!empty($this->assets)) { |
|
175 | 175 | AssetManager::getInstance()->register($this->assets); |
176 | 176 | } |
177 | 177 | |
@@ -179,7 +179,7 @@ discard block |
||
179 | 179 | |
180 | 180 | $currentRoute = RouteController::getCurrentRoute(); |
181 | 181 | |
182 | - if (isset($currentRoute['cache'])){ |
|
182 | + if (isset($currentRoute['cache'])) { |
|
183 | 183 | (new ViewCache())->set($currentRoute['uri'], $content, $_COOKIE['PHPSESSID'], $currentRoute['cache']['ttl']); |
184 | 184 | } |
185 | 185 |
@@ -53,19 +53,19 @@ |
||
53 | 53 | list($request, $response) = (new MiddlewareManager())->applyMiddlewares($request, $response); |
54 | 54 | } |
55 | 55 | |
56 | - $callback = route_callback(); |
|
56 | + $callback = route_callback(); |
|
57 | 57 | |
58 | - $currentRoute = RouteController::getCurrentRoute(); |
|
58 | + $currentRoute = RouteController::getCurrentRoute(); |
|
59 | 59 | |
60 | - if (isset($currentRoute['cache']) && |
|
61 | - (new ViewCache())->exists($currentRoute['uri'], $_COOKIE['PHPSESSID']) |
|
62 | - ){ |
|
63 | - call_user_func_array(function () use ($currentRoute, &$response) { |
|
64 | - $content = (new ViewCache())->get($currentRoute['uri'], $_COOKIE['PHPSESSID']); |
|
65 | - $response->html($content); |
|
66 | - }, []); |
|
60 | + if (isset($currentRoute['cache']) && |
|
61 | + (new ViewCache())->exists($currentRoute['uri'], $_COOKIE['PHPSESSID']) |
|
62 | + ){ |
|
63 | + call_user_func_array(function () use ($currentRoute, &$response) { |
|
64 | + $content = (new ViewCache())->get($currentRoute['uri'], $_COOKIE['PHPSESSID']); |
|
65 | + $response->html($content); |
|
66 | + }, []); |
|
67 | 67 | |
68 | - }elseif ($callback) { |
|
68 | + }elseif ($callback) { |
|
69 | 69 | call_user_func_array($callback, self::getArgs($callback)); |
70 | 70 | } else { |
71 | 71 | $controller = self::getController(); |
@@ -59,8 +59,8 @@ discard block |
||
59 | 59 | |
60 | 60 | if (isset($currentRoute['cache']) && |
61 | 61 | (new ViewCache())->exists($currentRoute['uri'], $_COOKIE['PHPSESSID']) |
62 | - ){ |
|
63 | - call_user_func_array(function () use ($currentRoute, &$response) { |
|
62 | + ) { |
|
63 | + call_user_func_array(function() use ($currentRoute, &$response) { |
|
64 | 64 | $content = (new ViewCache())->get($currentRoute['uri'], $_COOKIE['PHPSESSID']); |
65 | 65 | $response->html($content); |
66 | 66 | }, []); |
@@ -150,7 +150,7 @@ discard block |
||
150 | 150 | */ |
151 | 151 | private static function routeParams(): array |
152 | 152 | { |
153 | - return array_map(function ($param) { |
|
153 | + return array_map(function($param) { |
|
154 | 154 | return $param['value']; |
155 | 155 | }, route_params()); |
156 | 156 | } |
@@ -65,7 +65,7 @@ |
||
65 | 65 | $response->html($content); |
66 | 66 | }, []); |
67 | 67 | |
68 | - }elseif ($callback) { |
|
68 | + } elseif ($callback) { |
|
69 | 69 | call_user_func_array($callback, self::getArgs($callback)); |
70 | 70 | } else { |
71 | 71 | $controller = self::getController(); |
@@ -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 |
@@ -7,136 +7,136 @@ |
||
7 | 7 | |
8 | 8 | class ViewCache |
9 | 9 | { |
10 | - /** |
|
11 | - * @var string |
|
12 | - */ |
|
13 | - private $cacheDir; |
|
14 | - |
|
15 | - /** |
|
16 | - * @var string |
|
17 | - */ |
|
18 | - private $mimeType = '.tmp'; |
|
19 | - |
|
20 | - /** |
|
21 | - * @param bool $fromCommand |
|
22 | - * @throws ConfigException |
|
23 | - */ |
|
24 | - public function __construct(bool $fromCommand = false) |
|
25 | - { |
|
26 | - if (!config()->has('view_cache') && !$fromCommand){ |
|
27 | - throw ConfigException::configCollision('view_cache'); |
|
28 | - } |
|
29 | - |
|
30 | - $configCacheDir = config()->get('view_cache.cache_dir', 'cache'); |
|
31 | - $module = strtolower(current_module()); |
|
32 | - |
|
33 | - $this->cacheDir = base_dir() . DS . $configCacheDir . DS . 'views/'; |
|
34 | - |
|
35 | - if ($module){ |
|
36 | - $this->cacheDir = base_dir() . DS . $configCacheDir . DS . 'views' . DS . $module . '/'; |
|
37 | - } |
|
10 | + /** |
|
11 | + * @var string |
|
12 | + */ |
|
13 | + private $cacheDir; |
|
14 | + |
|
15 | + /** |
|
16 | + * @var string |
|
17 | + */ |
|
18 | + private $mimeType = '.tmp'; |
|
19 | + |
|
20 | + /** |
|
21 | + * @param bool $fromCommand |
|
22 | + * @throws ConfigException |
|
23 | + */ |
|
24 | + public function __construct(bool $fromCommand = false) |
|
25 | + { |
|
26 | + if (!config()->has('view_cache') && !$fromCommand){ |
|
27 | + throw ConfigException::configCollision('view_cache'); |
|
28 | + } |
|
29 | + |
|
30 | + $configCacheDir = config()->get('view_cache.cache_dir', 'cache'); |
|
31 | + $module = strtolower(current_module()); |
|
32 | + |
|
33 | + $this->cacheDir = base_dir() . DS . $configCacheDir . DS . 'views/'; |
|
34 | + |
|
35 | + if ($module){ |
|
36 | + $this->cacheDir = base_dir() . DS . $configCacheDir . DS . 'views' . DS . $module . '/'; |
|
37 | + } |
|
38 | 38 | |
39 | - if (!is_dir($this->cacheDir) && !$fromCommand) { |
|
40 | - mkdir($this->cacheDir, 0777, true); |
|
41 | - } |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * @param string $key |
|
46 | - * @param string $content |
|
47 | - * @param string $sessionId |
|
48 | - * @param int $ttl |
|
49 | - * @return void |
|
50 | - */ |
|
51 | - public function set(string $key, string $content, string $sessionId, int $ttl = 300): void |
|
52 | - { |
|
53 | - if (config()->has('view_cache.minify')) { |
|
54 | - $content = $this->minify($content); |
|
55 | - } |
|
56 | - |
|
57 | - $cacheFile = $this->getCacheFile($key, $sessionId); |
|
58 | - |
|
59 | - $data = [ |
|
60 | - 'content' => $content, |
|
61 | - 'expires_at' => time() + $ttl, |
|
62 | - ]; |
|
63 | - |
|
64 | - file_put_contents($cacheFile, serialize($data)); |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * @param string $key |
|
69 | - * @param string $sessionId |
|
70 | - * @return mixed|null |
|
71 | - */ |
|
72 | - public function get(string $key, string $sessionId): ?string |
|
73 | - { |
|
74 | - $cacheFile = $this->getCacheFile($key, $sessionId); |
|
75 | - if (!file_exists($cacheFile)) { |
|
76 | - return null; |
|
77 | - } |
|
78 | - |
|
79 | - $data = unserialize(file_get_contents($cacheFile)); |
|
80 | - if (time() > $data['expires_at']) { |
|
81 | - unlink($cacheFile); |
|
82 | - return null; |
|
83 | - } |
|
84 | - |
|
85 | - return $data['content']; |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * @param string $key |
|
90 | - * @param string $sessionId |
|
91 | - * @return void |
|
92 | - */ |
|
93 | - public function delete(string $key, string $sessionId): void |
|
94 | - { |
|
95 | - $cacheFile = $this->getCacheFile($key, $sessionId); |
|
96 | - if (file_exists($cacheFile)) { |
|
97 | - unlink($cacheFile); |
|
98 | - } |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * @param string $key |
|
103 | - * @param string $sessionId |
|
104 | - * @return bool |
|
105 | - */ |
|
106 | - public function exists(string $key, string $sessionId): bool |
|
107 | - { |
|
108 | - $cacheFile = $this->getCacheFile($key, $sessionId); |
|
109 | - |
|
110 | - if (!file_exists($cacheFile)) { |
|
111 | - return false; |
|
112 | - } |
|
113 | - |
|
114 | - $data = unserialize(file_get_contents($cacheFile)); |
|
115 | - |
|
116 | - if (time() > $data['expires_at']) { |
|
117 | - unlink($cacheFile); |
|
118 | - return false; |
|
119 | - } |
|
120 | - |
|
121 | - return true; |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * @param string $key |
|
126 | - * @param string $sessionId |
|
127 | - * @return string |
|
128 | - */ |
|
129 | - private function getCacheFile(string $key, string $sessionId): string |
|
130 | - { |
|
131 | - return $this->cacheDir . md5($key . $sessionId) . $this->mimeType; |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * @param string $content |
|
136 | - * @return string |
|
137 | - */ |
|
138 | - private function minify(string $content): string |
|
139 | - { |
|
140 | - return (new HtmlMin())->minify($content); |
|
141 | - } |
|
39 | + if (!is_dir($this->cacheDir) && !$fromCommand) { |
|
40 | + mkdir($this->cacheDir, 0777, true); |
|
41 | + } |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * @param string $key |
|
46 | + * @param string $content |
|
47 | + * @param string $sessionId |
|
48 | + * @param int $ttl |
|
49 | + * @return void |
|
50 | + */ |
|
51 | + public function set(string $key, string $content, string $sessionId, int $ttl = 300): void |
|
52 | + { |
|
53 | + if (config()->has('view_cache.minify')) { |
|
54 | + $content = $this->minify($content); |
|
55 | + } |
|
56 | + |
|
57 | + $cacheFile = $this->getCacheFile($key, $sessionId); |
|
58 | + |
|
59 | + $data = [ |
|
60 | + 'content' => $content, |
|
61 | + 'expires_at' => time() + $ttl, |
|
62 | + ]; |
|
63 | + |
|
64 | + file_put_contents($cacheFile, serialize($data)); |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * @param string $key |
|
69 | + * @param string $sessionId |
|
70 | + * @return mixed|null |
|
71 | + */ |
|
72 | + public function get(string $key, string $sessionId): ?string |
|
73 | + { |
|
74 | + $cacheFile = $this->getCacheFile($key, $sessionId); |
|
75 | + if (!file_exists($cacheFile)) { |
|
76 | + return null; |
|
77 | + } |
|
78 | + |
|
79 | + $data = unserialize(file_get_contents($cacheFile)); |
|
80 | + if (time() > $data['expires_at']) { |
|
81 | + unlink($cacheFile); |
|
82 | + return null; |
|
83 | + } |
|
84 | + |
|
85 | + return $data['content']; |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * @param string $key |
|
90 | + * @param string $sessionId |
|
91 | + * @return void |
|
92 | + */ |
|
93 | + public function delete(string $key, string $sessionId): void |
|
94 | + { |
|
95 | + $cacheFile = $this->getCacheFile($key, $sessionId); |
|
96 | + if (file_exists($cacheFile)) { |
|
97 | + unlink($cacheFile); |
|
98 | + } |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * @param string $key |
|
103 | + * @param string $sessionId |
|
104 | + * @return bool |
|
105 | + */ |
|
106 | + public function exists(string $key, string $sessionId): bool |
|
107 | + { |
|
108 | + $cacheFile = $this->getCacheFile($key, $sessionId); |
|
109 | + |
|
110 | + if (!file_exists($cacheFile)) { |
|
111 | + return false; |
|
112 | + } |
|
113 | + |
|
114 | + $data = unserialize(file_get_contents($cacheFile)); |
|
115 | + |
|
116 | + if (time() > $data['expires_at']) { |
|
117 | + unlink($cacheFile); |
|
118 | + return false; |
|
119 | + } |
|
120 | + |
|
121 | + return true; |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * @param string $key |
|
126 | + * @param string $sessionId |
|
127 | + * @return string |
|
128 | + */ |
|
129 | + private function getCacheFile(string $key, string $sessionId): string |
|
130 | + { |
|
131 | + return $this->cacheDir . md5($key . $sessionId) . $this->mimeType; |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * @param string $content |
|
136 | + * @return string |
|
137 | + */ |
|
138 | + private function minify(string $content): string |
|
139 | + { |
|
140 | + return (new HtmlMin())->minify($content); |
|
141 | + } |
|
142 | 142 | } |
143 | 143 | \ No newline at end of file |
@@ -23,7 +23,7 @@ discard block |
||
23 | 23 | */ |
24 | 24 | public function __construct(bool $fromCommand = false) |
25 | 25 | { |
26 | - if (!config()->has('view_cache') && !$fromCommand){ |
|
26 | + if (!config()->has('view_cache') && !$fromCommand) { |
|
27 | 27 | throw ConfigException::configCollision('view_cache'); |
28 | 28 | } |
29 | 29 | |
@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | |
33 | 33 | $this->cacheDir = base_dir() . DS . $configCacheDir . DS . 'views/'; |
34 | 34 | |
35 | - if ($module){ |
|
35 | + if ($module) { |
|
36 | 36 | $this->cacheDir = base_dir() . DS . $configCacheDir . DS . 'views' . DS . $module . '/'; |
37 | 37 | } |
38 | 38 |