@@ -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 |
@@ -99,7 +99,7 @@ |
||
| 99 | 99 | $this->importConfig(); |
| 100 | 100 | $this->initModule($this->getOption('module')); |
| 101 | 101 | $this->initType($this->getOption('type')); |
| 102 | - }catch (Exception $e){ |
|
| 102 | + } catch (Exception $e){ |
|
| 103 | 103 | $this->error($e->getMessage()); |
| 104 | 104 | return; |
| 105 | 105 | } |
@@ -30,214 +30,214 @@ |
||
| 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 = 'Clears resource cache'; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Command help text |
|
| 47 | - * @var string |
|
| 48 | - */ |
|
| 49 | - protected $help = 'The command will clear the resource cache'; |
|
| 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 | - try { |
|
| 99 | - $this->importConfig(); |
|
| 100 | - $this->initModule($this->getOption('module')); |
|
| 101 | - $this->initType($this->getOption('type')); |
|
| 102 | - }catch (Exception $e){ |
|
| 103 | - $this->error($e->getMessage()); |
|
| 104 | - return; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - $this->fs = Di::get(FileSystem::class); |
|
| 108 | - |
|
| 109 | - if (!$this->fs->isDirectory($this->cacheDir)) { |
|
| 110 | - $this->error('Cache directory does not exist or is not accessible.'); |
|
| 111 | - return; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - if ($this->module || $this->type) { |
|
| 115 | - $this->clearResourceModuleAndType($this->module, $this->type); |
|
| 116 | - } elseif (!empty($this->getOption('all'))) { |
|
| 117 | - $this->removeFilesFromDirectory($this->cacheDir); |
|
| 118 | - } else { |
|
| 119 | - $this->error('Please specify at least one of the following options: --all, --module=moduleName or --type=typeName!'); |
|
| 120 | - return; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - $this->info('Resource cache cleared successfully.'); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * @return void |
|
| 128 | - */ |
|
| 129 | - private function importModules() |
|
| 130 | - { |
|
| 131 | - try { |
|
| 132 | - if (!config()->has('modules')) { |
|
| 133 | - config()->import(new Setup('config', 'modules')); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - if (config()->has('modules') && is_array(config()->get('modules.modules'))){ |
|
| 137 | - $this->modules = array_keys(array_change_key_case(config()->get('modules.modules'))); |
|
| 138 | - } |
|
| 139 | - } catch (ConfigException|DiException|ReflectionException $e) { |
|
| 140 | - $this->error($e->getMessage()); |
|
| 141 | - return; |
|
| 142 | - } |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * @return void |
|
| 147 | - * @throws ConfigException |
|
| 148 | - * @throws DiException |
|
| 149 | - * @throws ReflectionException |
|
| 150 | - */ |
|
| 151 | - private function importConfig(): void |
|
| 152 | - { |
|
| 153 | - if (!config()->has('view_cache')) { |
|
| 154 | - config()->import(new Setup('config', 'view_cache')); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - $this->cacheDir = base_dir() . DS . config()->get('view_cache.cache_dir', 'cache'); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * @param string|null $moduleOption |
|
| 162 | - * @return void |
|
| 163 | - * @throws Exception |
|
| 164 | - */ |
|
| 165 | - private function initModule(?string $moduleOption): void |
|
| 166 | - { |
|
| 167 | - if (!empty($moduleOption)) { |
|
| 168 | - $this->importModules(); |
|
| 169 | - $module = strtolower($moduleOption); |
|
| 170 | - |
|
| 171 | - if (in_array($module, $this->modules)) { |
|
| 172 | - $this->module = $module; |
|
| 173 | - } else { |
|
| 174 | - throw new Exception('Module {'. $module .'} does not exist.'); |
|
| 175 | - } |
|
| 176 | - } |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * @param string|null $typeOption |
|
| 181 | - * @return void |
|
| 182 | - * @throws Exception |
|
| 183 | - */ |
|
| 184 | - private function initType(?string $typeOption): void |
|
| 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|null $moduleName |
|
| 199 | - * @param string|null $type |
|
| 200 | - * @return void |
|
| 201 | - */ |
|
| 202 | - private function clearResourceModuleAndType(?string $moduleName = null, ?string $type = null): void |
|
| 203 | - { |
|
| 204 | - $dir = $this->cacheDir; |
|
| 205 | - |
|
| 206 | - if ($type) { |
|
| 207 | - $dir = $dir . DS . strtolower($type); |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - if ($moduleName) { |
|
| 211 | - if (!$type) { |
|
| 212 | - $dir = $dir . DS . '*'; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - $dir = $dir . DS . strtolower($moduleName); |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - $this->removeFilesFromDirectory($dir); |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * @param string $dir |
|
| 223 | - * @return void |
|
| 224 | - */ |
|
| 225 | - private function removeFilesFromDirectory(string $dir): void |
|
| 226 | - { |
|
| 227 | - $entries = $this->fs->glob($dir . DS . '*'); |
|
| 228 | - |
|
| 229 | - foreach ($entries as $entry) { |
|
| 230 | - if ($this->fs->isDirectory($entry)) { |
|
| 231 | - $this->removeFilesFromDirectory($entry); |
|
| 232 | - |
|
| 233 | - if ($this->fs->fileName($entry) !== config()->get('view_cache.cache_dir', 'cache') && |
|
| 234 | - count($this->fs->glob($entry . DS . '*')) === 0 |
|
| 235 | - ) { |
|
| 236 | - $this->fs->removeDirectory($entry); |
|
| 237 | - } |
|
| 238 | - } else { |
|
| 239 | - $this->fs->remove($entry); |
|
| 240 | - } |
|
| 241 | - } |
|
| 242 | - } |
|
| 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 = 'Clears resource cache'; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Command help text |
|
| 47 | + * @var string |
|
| 48 | + */ |
|
| 49 | + protected $help = 'The command will clear the resource cache'; |
|
| 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 | + try { |
|
| 99 | + $this->importConfig(); |
|
| 100 | + $this->initModule($this->getOption('module')); |
|
| 101 | + $this->initType($this->getOption('type')); |
|
| 102 | + }catch (Exception $e){ |
|
| 103 | + $this->error($e->getMessage()); |
|
| 104 | + return; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + $this->fs = Di::get(FileSystem::class); |
|
| 108 | + |
|
| 109 | + if (!$this->fs->isDirectory($this->cacheDir)) { |
|
| 110 | + $this->error('Cache directory does not exist or is not accessible.'); |
|
| 111 | + return; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + if ($this->module || $this->type) { |
|
| 115 | + $this->clearResourceModuleAndType($this->module, $this->type); |
|
| 116 | + } elseif (!empty($this->getOption('all'))) { |
|
| 117 | + $this->removeFilesFromDirectory($this->cacheDir); |
|
| 118 | + } else { |
|
| 119 | + $this->error('Please specify at least one of the following options: --all, --module=moduleName or --type=typeName!'); |
|
| 120 | + return; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + $this->info('Resource cache cleared successfully.'); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * @return void |
|
| 128 | + */ |
|
| 129 | + private function importModules() |
|
| 130 | + { |
|
| 131 | + try { |
|
| 132 | + if (!config()->has('modules')) { |
|
| 133 | + config()->import(new Setup('config', 'modules')); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + if (config()->has('modules') && is_array(config()->get('modules.modules'))){ |
|
| 137 | + $this->modules = array_keys(array_change_key_case(config()->get('modules.modules'))); |
|
| 138 | + } |
|
| 139 | + } catch (ConfigException|DiException|ReflectionException $e) { |
|
| 140 | + $this->error($e->getMessage()); |
|
| 141 | + return; |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * @return void |
|
| 147 | + * @throws ConfigException |
|
| 148 | + * @throws DiException |
|
| 149 | + * @throws ReflectionException |
|
| 150 | + */ |
|
| 151 | + private function importConfig(): void |
|
| 152 | + { |
|
| 153 | + if (!config()->has('view_cache')) { |
|
| 154 | + config()->import(new Setup('config', 'view_cache')); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + $this->cacheDir = base_dir() . DS . config()->get('view_cache.cache_dir', 'cache'); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * @param string|null $moduleOption |
|
| 162 | + * @return void |
|
| 163 | + * @throws Exception |
|
| 164 | + */ |
|
| 165 | + private function initModule(?string $moduleOption): void |
|
| 166 | + { |
|
| 167 | + if (!empty($moduleOption)) { |
|
| 168 | + $this->importModules(); |
|
| 169 | + $module = strtolower($moduleOption); |
|
| 170 | + |
|
| 171 | + if (in_array($module, $this->modules)) { |
|
| 172 | + $this->module = $module; |
|
| 173 | + } else { |
|
| 174 | + throw new Exception('Module {'. $module .'} does not exist.'); |
|
| 175 | + } |
|
| 176 | + } |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * @param string|null $typeOption |
|
| 181 | + * @return void |
|
| 182 | + * @throws Exception |
|
| 183 | + */ |
|
| 184 | + private function initType(?string $typeOption): void |
|
| 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|null $moduleName |
|
| 199 | + * @param string|null $type |
|
| 200 | + * @return void |
|
| 201 | + */ |
|
| 202 | + private function clearResourceModuleAndType(?string $moduleName = null, ?string $type = null): void |
|
| 203 | + { |
|
| 204 | + $dir = $this->cacheDir; |
|
| 205 | + |
|
| 206 | + if ($type) { |
|
| 207 | + $dir = $dir . DS . strtolower($type); |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + if ($moduleName) { |
|
| 211 | + if (!$type) { |
|
| 212 | + $dir = $dir . DS . '*'; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + $dir = $dir . DS . strtolower($moduleName); |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + $this->removeFilesFromDirectory($dir); |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * @param string $dir |
|
| 223 | + * @return void |
|
| 224 | + */ |
|
| 225 | + private function removeFilesFromDirectory(string $dir): void |
|
| 226 | + { |
|
| 227 | + $entries = $this->fs->glob($dir . DS . '*'); |
|
| 228 | + |
|
| 229 | + foreach ($entries as $entry) { |
|
| 230 | + if ($this->fs->isDirectory($entry)) { |
|
| 231 | + $this->removeFilesFromDirectory($entry); |
|
| 232 | + |
|
| 233 | + if ($this->fs->fileName($entry) !== config()->get('view_cache.cache_dir', 'cache') && |
|
| 234 | + count($this->fs->glob($entry . DS . '*')) === 0 |
|
| 235 | + ) { |
|
| 236 | + $this->fs->removeDirectory($entry); |
|
| 237 | + } |
|
| 238 | + } else { |
|
| 239 | + $this->fs->remove($entry); |
|
| 240 | + } |
|
| 241 | + } |
|
| 242 | + } |
|
| 243 | 243 | } |
@@ -99,7 +99,7 @@ discard block |
||
| 99 | 99 | $this->importConfig(); |
| 100 | 100 | $this->initModule($this->getOption('module')); |
| 101 | 101 | $this->initType($this->getOption('type')); |
| 102 | - }catch (Exception $e){ |
|
| 102 | + } catch (Exception $e) { |
|
| 103 | 103 | $this->error($e->getMessage()); |
| 104 | 104 | return; |
| 105 | 105 | } |
@@ -133,10 +133,10 @@ discard block |
||
| 133 | 133 | config()->import(new Setup('config', 'modules')); |
| 134 | 134 | } |
| 135 | 135 | |
| 136 | - if (config()->has('modules') && is_array(config()->get('modules.modules'))){ |
|
| 136 | + if (config()->has('modules') && is_array(config()->get('modules.modules'))) { |
|
| 137 | 137 | $this->modules = array_keys(array_change_key_case(config()->get('modules.modules'))); |
| 138 | 138 | } |
| 139 | - } catch (ConfigException|DiException|ReflectionException $e) { |
|
| 139 | + } catch (ConfigException | DiException | ReflectionException $e) { |
|
| 140 | 140 | $this->error($e->getMessage()); |
| 141 | 141 | return; |
| 142 | 142 | } |
@@ -171,7 +171,7 @@ discard block |
||
| 171 | 171 | if (in_array($module, $this->modules)) { |
| 172 | 172 | $this->module = $module; |
| 173 | 173 | } else { |
| 174 | - throw new Exception('Module {'. $module .'} does not exist.'); |
|
| 174 | + throw new Exception('Module {' . $module . '} does not exist.'); |
|
| 175 | 175 | } |
| 176 | 176 | } |
| 177 | 177 | } |
@@ -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 | } |
@@ -130,7 +130,7 @@ |
||
| 130 | 130 | */ |
| 131 | 131 | function route_cache_settings(): ?array |
| 132 | 132 | { |
| 133 | - return RouteController::getCurrentRoute()['cache_settings'] ?? null; |
|
| 133 | + return RouteController::getCurrentRoute()['cache_settings'] ?? null; |
|
| 134 | 134 | } |
| 135 | 135 | |
| 136 | 136 | /** |
@@ -114,9 +114,9 @@ |
||
| 114 | 114 | |
| 115 | 115 | $currentRoute = $this->currentRoute(); |
| 116 | 116 | |
| 117 | - if (is_array($currentRoute) && key_exists('shouldCache', $currentRoute)){ |
|
| 118 | - ViewCache::getInstance()->setIsEnabled($currentRoute['shouldCache']); |
|
| 119 | - } |
|
| 117 | + if (is_array($currentRoute) && key_exists('shouldCache', $currentRoute)){ |
|
| 118 | + ViewCache::getInstance()->setIsEnabled($currentRoute['shouldCache']); |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | 121 | if (!$currentRoute) { |
| 122 | 122 | throw RouteException::incorrectMethod($this->request->getMethod()); |
@@ -103,7 +103,7 @@ discard block |
||
| 103 | 103 | $this->matchedRoutes = $this->findMatches($uri); |
| 104 | 104 | |
| 105 | 105 | if (empty($this->matchedRoutes)) { |
| 106 | - stop(function () { |
|
| 106 | + stop(function() { |
|
| 107 | 107 | $this->handleNotFound(); |
| 108 | 108 | }); |
| 109 | 109 | } |
@@ -114,7 +114,7 @@ discard block |
||
| 114 | 114 | |
| 115 | 115 | $currentRoute = $this->currentRoute(); |
| 116 | 116 | |
| 117 | - if (is_array($currentRoute) && key_exists('shouldCache', $currentRoute)){ |
|
| 117 | + if (is_array($currentRoute) && key_exists('shouldCache', $currentRoute)) { |
|
| 118 | 118 | ViewCache::getInstance()->setIsEnabled($currentRoute['shouldCache']); |
| 119 | 119 | } |
| 120 | 120 | |
@@ -208,7 +208,7 @@ discard block |
||
| 208 | 208 | $routePattern = '(\/)?'; |
| 209 | 209 | $routeParams = []; |
| 210 | 210 | |
| 211 | - $lastIndex = (int)array_key_last($routeSegments); |
|
| 211 | + $lastIndex = (int) array_key_last($routeSegments); |
|
| 212 | 212 | |
| 213 | 213 | foreach ($routeSegments as $index => $segment) { |
| 214 | 214 | $segmentParam = $this->getSegmentParam($segment, $index, $lastIndex); |
@@ -31,288 +31,288 @@ |
||
| 31 | 31 | class Route |
| 32 | 32 | { |
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Current module name |
|
| 36 | - * @var string |
|
| 37 | - */ |
|
| 38 | - private $moduleName; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Module options |
|
| 42 | - * @var array |
|
| 43 | - */ |
|
| 44 | - private $moduleOptions = []; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Identifies the group middleware |
|
| 48 | - * @var bool |
|
| 49 | - */ |
|
| 50 | - private $isGroupMiddlewares; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Identifies the group |
|
| 54 | - * @var boolean |
|
| 55 | - */ |
|
| 56 | - private $isGroup = false; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Current group name |
|
| 60 | - * @var string |
|
| 61 | - */ |
|
| 62 | - private $currentGroupName = null; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Current route |
|
| 66 | - * @var array |
|
| 67 | - */ |
|
| 68 | - private $currentRoute = []; |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Virtual routes |
|
| 72 | - * @var array |
|
| 73 | - */ |
|
| 74 | - private $virtualRoutes = []; |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * @var ViewCache |
|
| 78 | - */ |
|
| 79 | - private $viewCacheInstance; |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * @param array $module |
|
| 83 | - * @param ViewCache $viewCache |
|
| 84 | - */ |
|
| 85 | - public function __construct(array $module, ViewCache $viewCache) |
|
| 86 | - { |
|
| 87 | - $this->virtualRoutes['*'] = []; |
|
| 88 | - $this->moduleName = key($module); |
|
| 89 | - $this->moduleOptions = $module[key($module)]; |
|
| 90 | - $this->viewCacheInstance = $viewCache; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * @param string $route |
|
| 95 | - * @param string $method |
|
| 96 | - * @param ...$params |
|
| 97 | - * @return $this |
|
| 98 | - */ |
|
| 99 | - public function add(string $route, string $method, ...$params): Route |
|
| 100 | - { |
|
| 101 | - $this->currentRoute = [ |
|
| 102 | - 'route' => !empty($this->moduleOptions['prefix']) ? $this->moduleOptions['prefix'] . '/' . $route : $route, |
|
| 103 | - 'prefix' => $this->moduleOptions['prefix'], |
|
| 104 | - 'method' => $method, |
|
| 105 | - 'module' => $this->moduleName |
|
| 106 | - ]; |
|
| 107 | - |
|
| 108 | - if (key_exists('cacheable', $this->moduleOptions)){ |
|
| 109 | - $enabled = true; |
|
| 110 | - if (!$this->moduleOptions['cacheable']){ |
|
| 111 | - $enabled = false; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - $this->currentRoute['shouldCache'] = $enabled; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - if (is_callable($params[0])) { |
|
| 118 | - $this->currentRoute['callback'] = $params[0]; |
|
| 119 | - } else { |
|
| 120 | - $this->currentRoute['controller'] = $params[0]; |
|
| 121 | - $this->currentRoute['action'] = $params[1]; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - if ($this->currentGroupName) { |
|
| 125 | - $this->currentRoute['group'] = $this->currentGroupName; |
|
| 126 | - $this->virtualRoutes[$this->currentGroupName][] = $this->currentRoute; |
|
| 127 | - } else { |
|
| 128 | - $this->isGroup = false; |
|
| 129 | - $this->isGroupMiddlewares = false; |
|
| 130 | - $this->virtualRoutes['*'][] = $this->currentRoute; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - return $this; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * @param string $route |
|
| 138 | - * @param ...$params |
|
| 139 | - * @return $this |
|
| 140 | - */ |
|
| 141 | - public function get(string $route, ...$params): Route |
|
| 142 | - { |
|
| 143 | - return $this->add($route, 'GET', ...$params); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * @param string $route |
|
| 148 | - * @param ...$params |
|
| 149 | - * @return $this |
|
| 150 | - */ |
|
| 151 | - public function post(string $route, ...$params): Route |
|
| 152 | - { |
|
| 153 | - return $this->add($route, 'POST', ...$params); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * Starts a named group of routes |
|
| 158 | - * @param string $groupName |
|
| 159 | - * @param Closure $callback |
|
| 160 | - * @return Route |
|
| 161 | - */ |
|
| 162 | - public function group(string $groupName, Closure $callback): Route |
|
| 163 | - { |
|
| 164 | - $this->currentGroupName = $groupName; |
|
| 165 | - |
|
| 166 | - $this->isGroup = true; |
|
| 167 | - $this->isGroupMiddlewares = false; |
|
| 168 | - $callback($this); |
|
| 169 | - $this->isGroupMiddlewares = true; |
|
| 170 | - $this->currentGroupName = null; |
|
| 171 | - |
|
| 172 | - return $this; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * Adds middlewares to routes and route groups |
|
| 177 | - * @param array $middlewares |
|
| 178 | - * @return Route |
|
| 179 | - */ |
|
| 180 | - public function middlewares(array $middlewares = []): Route |
|
| 181 | - { |
|
| 182 | - if (!$this->isGroup) { |
|
| 183 | - end($this->virtualRoutes['*']); |
|
| 184 | - $lastKey = key($this->virtualRoutes['*']); |
|
| 185 | - $this->assignMiddlewaresToRoute($this->virtualRoutes['*'][$lastKey], $middlewares); |
|
| 186 | - return $this; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - end($this->virtualRoutes); |
|
| 190 | - $lastKeyOfFirstRound = key($this->virtualRoutes); |
|
| 191 | - |
|
| 192 | - if (!$this->isGroupMiddlewares) { |
|
| 193 | - end($this->virtualRoutes[$lastKeyOfFirstRound]); |
|
| 194 | - $lastKeyOfSecondRound = key($this->virtualRoutes[$lastKeyOfFirstRound]); |
|
| 195 | - $this->assignMiddlewaresToRoute($this->virtualRoutes[$lastKeyOfFirstRound][$lastKeyOfSecondRound], $middlewares); |
|
| 196 | - return $this; |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - foreach ($this->virtualRoutes[$lastKeyOfFirstRound] as &$route) { |
|
| 200 | - $this->assignMiddlewaresToRoute($route, $middlewares); |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - return $this; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * @param bool $shouldCache |
|
| 208 | - * @param int|null $ttl |
|
| 209 | - * @return $this |
|
| 210 | - * @throws ConfigException |
|
| 211 | - * @throws DatabaseException |
|
| 212 | - * @throws DiException |
|
| 213 | - * @throws LangException |
|
| 214 | - * @throws ReflectionException |
|
| 215 | - * @throws SessionException |
|
| 216 | - */ |
|
| 217 | - public function cacheable(bool $shouldCache, int $ttl = null): Route |
|
| 218 | - { |
|
| 219 | - if (empty(session()->getId())){ |
|
| 220 | - return $this; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - if ($ttl) { |
|
| 224 | - $this->viewCacheInstance->setTtl($ttl); |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - if (!$this->isGroup){ |
|
| 228 | - end($this->virtualRoutes['*']); |
|
| 229 | - $lastKey = key($this->virtualRoutes['*']); |
|
| 230 | - |
|
| 231 | - $this->virtualRoutes['*'][$lastKey]['shouldCache'] = $shouldCache; |
|
| 232 | - |
|
| 233 | - return $this; |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - end($this->virtualRoutes); |
|
| 237 | - $lastKeyOfFirstRound = key($this->virtualRoutes); |
|
| 238 | - |
|
| 239 | - foreach ($this->virtualRoutes[$lastKeyOfFirstRound] as &$route) { |
|
| 240 | - $route['shouldCache'] = $shouldCache; |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - return $this; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * Sets a unique name for a route |
|
| 248 | - * @param string $name |
|
| 249 | - * @return Route |
|
| 250 | - * @throws RouteException |
|
| 251 | - */ |
|
| 252 | - public function name(string $name): Route |
|
| 253 | - { |
|
| 254 | - if (empty($this->currentRoute)) { |
|
| 255 | - throw RouteException::nameBeforeDefinition(); |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - if ($this->isGroupMiddlewares) { |
|
| 259 | - throw RouteException::nameOnGroup(); |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - foreach ($this->virtualRoutes as &$virtualRoute) { |
|
| 263 | - foreach ($virtualRoute as &$route) { |
|
| 264 | - if (isset($route['name']) && $route['name'] == $name) { |
|
| 265 | - throw RouteException::nonUniqueName(); |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - if ($route['route'] == $this->currentRoute['route']) { |
|
| 269 | - $route['name'] = $name; |
|
| 270 | - } |
|
| 271 | - } |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - return $this; |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - /** |
|
| 278 | - * Gets the run-time routes |
|
| 279 | - * @return array |
|
| 280 | - */ |
|
| 281 | - public function getRuntimeRoutes(): array |
|
| 282 | - { |
|
| 283 | - $runtimeRoutes = []; |
|
| 284 | - foreach ($this->virtualRoutes as $virtualRoute) { |
|
| 285 | - foreach ($virtualRoute as $route) { |
|
| 286 | - $runtimeRoutes[] = $route; |
|
| 287 | - } |
|
| 288 | - } |
|
| 289 | - return $runtimeRoutes; |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - /** |
|
| 293 | - * Gets the virtual routes |
|
| 294 | - * @return array |
|
| 295 | - */ |
|
| 296 | - public function getVirtualRoutes(): array |
|
| 297 | - { |
|
| 298 | - return $this->virtualRoutes; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * Assigns middlewares to the route |
|
| 303 | - * @param array $route |
|
| 304 | - * @param array $middlewares |
|
| 305 | - */ |
|
| 306 | - private function assignMiddlewaresToRoute(array &$route, array $middlewares) |
|
| 307 | - { |
|
| 308 | - if (!key_exists('middlewares', $route)) { |
|
| 309 | - $route['middlewares'] = $middlewares; |
|
| 310 | - } else { |
|
| 311 | - $middlewares = array_reverse($middlewares); |
|
| 312 | - |
|
| 313 | - foreach ($middlewares as $middleware) { |
|
| 314 | - array_unshift($route['middlewares'], $middleware); |
|
| 315 | - } |
|
| 316 | - } |
|
| 317 | - } |
|
| 34 | + /** |
|
| 35 | + * Current module name |
|
| 36 | + * @var string |
|
| 37 | + */ |
|
| 38 | + private $moduleName; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Module options |
|
| 42 | + * @var array |
|
| 43 | + */ |
|
| 44 | + private $moduleOptions = []; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Identifies the group middleware |
|
| 48 | + * @var bool |
|
| 49 | + */ |
|
| 50 | + private $isGroupMiddlewares; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Identifies the group |
|
| 54 | + * @var boolean |
|
| 55 | + */ |
|
| 56 | + private $isGroup = false; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Current group name |
|
| 60 | + * @var string |
|
| 61 | + */ |
|
| 62 | + private $currentGroupName = null; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Current route |
|
| 66 | + * @var array |
|
| 67 | + */ |
|
| 68 | + private $currentRoute = []; |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Virtual routes |
|
| 72 | + * @var array |
|
| 73 | + */ |
|
| 74 | + private $virtualRoutes = []; |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * @var ViewCache |
|
| 78 | + */ |
|
| 79 | + private $viewCacheInstance; |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * @param array $module |
|
| 83 | + * @param ViewCache $viewCache |
|
| 84 | + */ |
|
| 85 | + public function __construct(array $module, ViewCache $viewCache) |
|
| 86 | + { |
|
| 87 | + $this->virtualRoutes['*'] = []; |
|
| 88 | + $this->moduleName = key($module); |
|
| 89 | + $this->moduleOptions = $module[key($module)]; |
|
| 90 | + $this->viewCacheInstance = $viewCache; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * @param string $route |
|
| 95 | + * @param string $method |
|
| 96 | + * @param ...$params |
|
| 97 | + * @return $this |
|
| 98 | + */ |
|
| 99 | + public function add(string $route, string $method, ...$params): Route |
|
| 100 | + { |
|
| 101 | + $this->currentRoute = [ |
|
| 102 | + 'route' => !empty($this->moduleOptions['prefix']) ? $this->moduleOptions['prefix'] . '/' . $route : $route, |
|
| 103 | + 'prefix' => $this->moduleOptions['prefix'], |
|
| 104 | + 'method' => $method, |
|
| 105 | + 'module' => $this->moduleName |
|
| 106 | + ]; |
|
| 107 | + |
|
| 108 | + if (key_exists('cacheable', $this->moduleOptions)){ |
|
| 109 | + $enabled = true; |
|
| 110 | + if (!$this->moduleOptions['cacheable']){ |
|
| 111 | + $enabled = false; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + $this->currentRoute['shouldCache'] = $enabled; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + if (is_callable($params[0])) { |
|
| 118 | + $this->currentRoute['callback'] = $params[0]; |
|
| 119 | + } else { |
|
| 120 | + $this->currentRoute['controller'] = $params[0]; |
|
| 121 | + $this->currentRoute['action'] = $params[1]; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + if ($this->currentGroupName) { |
|
| 125 | + $this->currentRoute['group'] = $this->currentGroupName; |
|
| 126 | + $this->virtualRoutes[$this->currentGroupName][] = $this->currentRoute; |
|
| 127 | + } else { |
|
| 128 | + $this->isGroup = false; |
|
| 129 | + $this->isGroupMiddlewares = false; |
|
| 130 | + $this->virtualRoutes['*'][] = $this->currentRoute; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + return $this; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * @param string $route |
|
| 138 | + * @param ...$params |
|
| 139 | + * @return $this |
|
| 140 | + */ |
|
| 141 | + public function get(string $route, ...$params): Route |
|
| 142 | + { |
|
| 143 | + return $this->add($route, 'GET', ...$params); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * @param string $route |
|
| 148 | + * @param ...$params |
|
| 149 | + * @return $this |
|
| 150 | + */ |
|
| 151 | + public function post(string $route, ...$params): Route |
|
| 152 | + { |
|
| 153 | + return $this->add($route, 'POST', ...$params); |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * Starts a named group of routes |
|
| 158 | + * @param string $groupName |
|
| 159 | + * @param Closure $callback |
|
| 160 | + * @return Route |
|
| 161 | + */ |
|
| 162 | + public function group(string $groupName, Closure $callback): Route |
|
| 163 | + { |
|
| 164 | + $this->currentGroupName = $groupName; |
|
| 165 | + |
|
| 166 | + $this->isGroup = true; |
|
| 167 | + $this->isGroupMiddlewares = false; |
|
| 168 | + $callback($this); |
|
| 169 | + $this->isGroupMiddlewares = true; |
|
| 170 | + $this->currentGroupName = null; |
|
| 171 | + |
|
| 172 | + return $this; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * Adds middlewares to routes and route groups |
|
| 177 | + * @param array $middlewares |
|
| 178 | + * @return Route |
|
| 179 | + */ |
|
| 180 | + public function middlewares(array $middlewares = []): Route |
|
| 181 | + { |
|
| 182 | + if (!$this->isGroup) { |
|
| 183 | + end($this->virtualRoutes['*']); |
|
| 184 | + $lastKey = key($this->virtualRoutes['*']); |
|
| 185 | + $this->assignMiddlewaresToRoute($this->virtualRoutes['*'][$lastKey], $middlewares); |
|
| 186 | + return $this; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + end($this->virtualRoutes); |
|
| 190 | + $lastKeyOfFirstRound = key($this->virtualRoutes); |
|
| 191 | + |
|
| 192 | + if (!$this->isGroupMiddlewares) { |
|
| 193 | + end($this->virtualRoutes[$lastKeyOfFirstRound]); |
|
| 194 | + $lastKeyOfSecondRound = key($this->virtualRoutes[$lastKeyOfFirstRound]); |
|
| 195 | + $this->assignMiddlewaresToRoute($this->virtualRoutes[$lastKeyOfFirstRound][$lastKeyOfSecondRound], $middlewares); |
|
| 196 | + return $this; |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + foreach ($this->virtualRoutes[$lastKeyOfFirstRound] as &$route) { |
|
| 200 | + $this->assignMiddlewaresToRoute($route, $middlewares); |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + return $this; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * @param bool $shouldCache |
|
| 208 | + * @param int|null $ttl |
|
| 209 | + * @return $this |
|
| 210 | + * @throws ConfigException |
|
| 211 | + * @throws DatabaseException |
|
| 212 | + * @throws DiException |
|
| 213 | + * @throws LangException |
|
| 214 | + * @throws ReflectionException |
|
| 215 | + * @throws SessionException |
|
| 216 | + */ |
|
| 217 | + public function cacheable(bool $shouldCache, int $ttl = null): Route |
|
| 218 | + { |
|
| 219 | + if (empty(session()->getId())){ |
|
| 220 | + return $this; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + if ($ttl) { |
|
| 224 | + $this->viewCacheInstance->setTtl($ttl); |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + if (!$this->isGroup){ |
|
| 228 | + end($this->virtualRoutes['*']); |
|
| 229 | + $lastKey = key($this->virtualRoutes['*']); |
|
| 230 | + |
|
| 231 | + $this->virtualRoutes['*'][$lastKey]['shouldCache'] = $shouldCache; |
|
| 232 | + |
|
| 233 | + return $this; |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + end($this->virtualRoutes); |
|
| 237 | + $lastKeyOfFirstRound = key($this->virtualRoutes); |
|
| 238 | + |
|
| 239 | + foreach ($this->virtualRoutes[$lastKeyOfFirstRound] as &$route) { |
|
| 240 | + $route['shouldCache'] = $shouldCache; |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + return $this; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * Sets a unique name for a route |
|
| 248 | + * @param string $name |
|
| 249 | + * @return Route |
|
| 250 | + * @throws RouteException |
|
| 251 | + */ |
|
| 252 | + public function name(string $name): Route |
|
| 253 | + { |
|
| 254 | + if (empty($this->currentRoute)) { |
|
| 255 | + throw RouteException::nameBeforeDefinition(); |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + if ($this->isGroupMiddlewares) { |
|
| 259 | + throw RouteException::nameOnGroup(); |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + foreach ($this->virtualRoutes as &$virtualRoute) { |
|
| 263 | + foreach ($virtualRoute as &$route) { |
|
| 264 | + if (isset($route['name']) && $route['name'] == $name) { |
|
| 265 | + throw RouteException::nonUniqueName(); |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + if ($route['route'] == $this->currentRoute['route']) { |
|
| 269 | + $route['name'] = $name; |
|
| 270 | + } |
|
| 271 | + } |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + return $this; |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + /** |
|
| 278 | + * Gets the run-time routes |
|
| 279 | + * @return array |
|
| 280 | + */ |
|
| 281 | + public function getRuntimeRoutes(): array |
|
| 282 | + { |
|
| 283 | + $runtimeRoutes = []; |
|
| 284 | + foreach ($this->virtualRoutes as $virtualRoute) { |
|
| 285 | + foreach ($virtualRoute as $route) { |
|
| 286 | + $runtimeRoutes[] = $route; |
|
| 287 | + } |
|
| 288 | + } |
|
| 289 | + return $runtimeRoutes; |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + /** |
|
| 293 | + * Gets the virtual routes |
|
| 294 | + * @return array |
|
| 295 | + */ |
|
| 296 | + public function getVirtualRoutes(): array |
|
| 297 | + { |
|
| 298 | + return $this->virtualRoutes; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * Assigns middlewares to the route |
|
| 303 | + * @param array $route |
|
| 304 | + * @param array $middlewares |
|
| 305 | + */ |
|
| 306 | + private function assignMiddlewaresToRoute(array &$route, array $middlewares) |
|
| 307 | + { |
|
| 308 | + if (!key_exists('middlewares', $route)) { |
|
| 309 | + $route['middlewares'] = $middlewares; |
|
| 310 | + } else { |
|
| 311 | + $middlewares = array_reverse($middlewares); |
|
| 312 | + |
|
| 313 | + foreach ($middlewares as $middleware) { |
|
| 314 | + array_unshift($route['middlewares'], $middleware); |
|
| 315 | + } |
|
| 316 | + } |
|
| 317 | + } |
|
| 318 | 318 | } |
@@ -105,9 +105,9 @@ discard block |
||
| 105 | 105 | 'module' => $this->moduleName |
| 106 | 106 | ]; |
| 107 | 107 | |
| 108 | - if (key_exists('cacheable', $this->moduleOptions)){ |
|
| 108 | + if (key_exists('cacheable', $this->moduleOptions)) { |
|
| 109 | 109 | $enabled = true; |
| 110 | - if (!$this->moduleOptions['cacheable']){ |
|
| 110 | + if (!$this->moduleOptions['cacheable']) { |
|
| 111 | 111 | $enabled = false; |
| 112 | 112 | } |
| 113 | 113 | |
@@ -216,7 +216,7 @@ discard block |
||
| 216 | 216 | */ |
| 217 | 217 | public function cacheable(bool $shouldCache, int $ttl = null): Route |
| 218 | 218 | { |
| 219 | - if (empty(session()->getId())){ |
|
| 219 | + if (empty(session()->getId())) { |
|
| 220 | 220 | return $this; |
| 221 | 221 | } |
| 222 | 222 | |
@@ -224,7 +224,7 @@ discard block |
||
| 224 | 224 | $this->viewCacheInstance->setTtl($ttl); |
| 225 | 225 | } |
| 226 | 226 | |
| 227 | - if (!$this->isGroup){ |
|
| 227 | + if (!$this->isGroup) { |
|
| 228 | 228 | end($this->virtualRoutes['*']); |
| 229 | 229 | $lastKey = key($this->virtualRoutes['*']); |
| 230 | 230 | |
@@ -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,13 +179,13 @@ 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 (ViewCache::getInstance()->isEnabled()){ |
|
| 185 | - $content = ViewCache::getInstance() |
|
| 186 | - ->set(route_uri(), $content) |
|
| 187 | - ->get(route_uri()); |
|
| 188 | - } |
|
| 184 | + if (ViewCache::getInstance()->isEnabled()){ |
|
| 185 | + $content = ViewCache::getInstance() |
|
| 186 | + ->set(route_uri(), $content) |
|
| 187 | + ->get(route_uri()); |
|
| 188 | + } |
|
| 189 | 189 | |
| 190 | 190 | return $content; |
| 191 | 191 | } |
@@ -175,14 +175,14 @@ |
||
| 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 (ViewCache::getInstance()->isEnabled()){ |
|
| 185 | - $content = ViewCache::getInstance() |
|
| 184 | + if (ViewCache::getInstance()->isEnabled()) { |
|
| 185 | + $content = ViewCache::getInstance() |
|
| 186 | 186 | ->set(route_uri(), $content) |
| 187 | 187 | ->get(route_uri()); |
| 188 | 188 | } |
@@ -39,122 +39,122 @@ |
||
| 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 (ViewCache::getInstance()->isEnabled() && |
|
| 66 | - ViewCache::getInstance()->exists(route_uri()) |
|
| 67 | - ){ |
|
| 68 | - $content = ViewCache::getInstance()->get(route_uri()); |
|
| 69 | - $response->html($content); |
|
| 70 | - }elseif ($callback) { |
|
| 71 | - call_user_func_array($callback, self::getArgs($callback)); |
|
| 72 | - } else { |
|
| 73 | - $controller = self::getController(); |
|
| 74 | - $action = self::getAction($controller); |
|
| 75 | - |
|
| 76 | - if ($controller->csrfVerification && in_array($request->getMethod(), Csrf::METHODS)) { |
|
| 77 | - csrf()->checkToken($request); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - if (method_exists($controller, '__before')) { |
|
| 81 | - call_user_func_array([$controller, '__before'], self::getArgs([$controller, '__before'])); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - call_user_func_array([$controller, $action], self::getArgs([$controller, $action])); |
|
| 85 | - |
|
| 86 | - if (method_exists($controller, '__after')) { |
|
| 87 | - call_user_func_array([$controller, '__after'], self::getArgs([$controller, '__after'])); |
|
| 88 | - } |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Get Controller |
|
| 94 | - * @return QtController |
|
| 95 | - * @throws DiException |
|
| 96 | - * @throws ReflectionException |
|
| 97 | - * @throws ControllerException |
|
| 98 | - */ |
|
| 99 | - private static function getController(): QtController |
|
| 100 | - { |
|
| 101 | - $fs = Di::get(FileSystem::class); |
|
| 102 | - |
|
| 103 | - $controllerPath = modules_dir() . DS . current_module() . DS . 'Controllers' . DS . current_controller() . '.php'; |
|
| 104 | - |
|
| 105 | - if (!$fs->exists($controllerPath)) { |
|
| 106 | - throw ControllerException::controllerNotFound(current_controller()); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - require_once $controllerPath; |
|
| 110 | - |
|
| 111 | - $controllerClass = '\\Modules\\' . current_module() . '\\Controllers\\' . current_controller(); |
|
| 112 | - |
|
| 113 | - if (!class_exists($controllerClass, false)) { |
|
| 114 | - throw ControllerException::controllerNotDefined(current_controller()); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - return new $controllerClass(); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Get Action |
|
| 122 | - * @param QtController $controller |
|
| 123 | - * @return string|null |
|
| 124 | - * @throws ControllerException |
|
| 125 | - */ |
|
| 126 | - private static function getAction(QtController $controller): ?string |
|
| 127 | - { |
|
| 128 | - $action = current_action(); |
|
| 129 | - |
|
| 130 | - if ($action && !method_exists($controller, $action)) { |
|
| 131 | - throw ControllerException::actionNotDefined($action); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - return $action; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Get arguments |
|
| 139 | - * @param callable $callable |
|
| 140 | - * @return array |
|
| 141 | - * @throws DiException |
|
| 142 | - * @throws ReflectionException |
|
| 143 | - */ |
|
| 144 | - private static function getArgs(callable $callable): array |
|
| 145 | - { |
|
| 146 | - return Di::autowire($callable, self::routeParams()); |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Gets the route parameters |
|
| 151 | - * @return array |
|
| 152 | - */ |
|
| 153 | - private static function routeParams(): array |
|
| 154 | - { |
|
| 155 | - return array_map(function ($param) { |
|
| 156 | - return $param['value']; |
|
| 157 | - }, route_params()); |
|
| 158 | - } |
|
| 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 (ViewCache::getInstance()->isEnabled() && |
|
| 66 | + ViewCache::getInstance()->exists(route_uri()) |
|
| 67 | + ){ |
|
| 68 | + $content = ViewCache::getInstance()->get(route_uri()); |
|
| 69 | + $response->html($content); |
|
| 70 | + }elseif ($callback) { |
|
| 71 | + call_user_func_array($callback, self::getArgs($callback)); |
|
| 72 | + } else { |
|
| 73 | + $controller = self::getController(); |
|
| 74 | + $action = self::getAction($controller); |
|
| 75 | + |
|
| 76 | + if ($controller->csrfVerification && in_array($request->getMethod(), Csrf::METHODS)) { |
|
| 77 | + csrf()->checkToken($request); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + if (method_exists($controller, '__before')) { |
|
| 81 | + call_user_func_array([$controller, '__before'], self::getArgs([$controller, '__before'])); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + call_user_func_array([$controller, $action], self::getArgs([$controller, $action])); |
|
| 85 | + |
|
| 86 | + if (method_exists($controller, '__after')) { |
|
| 87 | + call_user_func_array([$controller, '__after'], self::getArgs([$controller, '__after'])); |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Get Controller |
|
| 94 | + * @return QtController |
|
| 95 | + * @throws DiException |
|
| 96 | + * @throws ReflectionException |
|
| 97 | + * @throws ControllerException |
|
| 98 | + */ |
|
| 99 | + private static function getController(): QtController |
|
| 100 | + { |
|
| 101 | + $fs = Di::get(FileSystem::class); |
|
| 102 | + |
|
| 103 | + $controllerPath = modules_dir() . DS . current_module() . DS . 'Controllers' . DS . current_controller() . '.php'; |
|
| 104 | + |
|
| 105 | + if (!$fs->exists($controllerPath)) { |
|
| 106 | + throw ControllerException::controllerNotFound(current_controller()); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + require_once $controllerPath; |
|
| 110 | + |
|
| 111 | + $controllerClass = '\\Modules\\' . current_module() . '\\Controllers\\' . current_controller(); |
|
| 112 | + |
|
| 113 | + if (!class_exists($controllerClass, false)) { |
|
| 114 | + throw ControllerException::controllerNotDefined(current_controller()); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + return new $controllerClass(); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Get Action |
|
| 122 | + * @param QtController $controller |
|
| 123 | + * @return string|null |
|
| 124 | + * @throws ControllerException |
|
| 125 | + */ |
|
| 126 | + private static function getAction(QtController $controller): ?string |
|
| 127 | + { |
|
| 128 | + $action = current_action(); |
|
| 129 | + |
|
| 130 | + if ($action && !method_exists($controller, $action)) { |
|
| 131 | + throw ControllerException::actionNotDefined($action); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + return $action; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Get arguments |
|
| 139 | + * @param callable $callable |
|
| 140 | + * @return array |
|
| 141 | + * @throws DiException |
|
| 142 | + * @throws ReflectionException |
|
| 143 | + */ |
|
| 144 | + private static function getArgs(callable $callable): array |
|
| 145 | + { |
|
| 146 | + return Di::autowire($callable, self::routeParams()); |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Gets the route parameters |
|
| 151 | + * @return array |
|
| 152 | + */ |
|
| 153 | + private static function routeParams(): array |
|
| 154 | + { |
|
| 155 | + return array_map(function ($param) { |
|
| 156 | + return $param['value']; |
|
| 157 | + }, route_params()); |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | 160 | } |
@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | |
| 65 | 65 | if (ViewCache::getInstance()->isEnabled() && |
| 66 | 66 | ViewCache::getInstance()->exists(route_uri()) |
| 67 | - ){ |
|
| 67 | + ) { |
|
| 68 | 68 | $content = ViewCache::getInstance()->get(route_uri()); |
| 69 | 69 | $response->html($content); |
| 70 | 70 | }elseif ($callback) { |
@@ -152,7 +152,7 @@ discard block |
||
| 152 | 152 | */ |
| 153 | 153 | private static function routeParams(): array |
| 154 | 154 | { |
| 155 | - return array_map(function ($param) { |
|
| 155 | + return array_map(function($param) { |
|
| 156 | 156 | return $param['value']; |
| 157 | 157 | }, route_params()); |
| 158 | 158 | } |
@@ -67,7 +67,7 @@ |
||
| 67 | 67 | ){ |
| 68 | 68 | $content = ViewCache::getInstance()->get(route_uri()); |
| 69 | 69 | $response->html($content); |
| 70 | - }elseif ($callback) { |
|
| 70 | + } elseif ($callback) { |
|
| 71 | 71 | call_user_func_array($callback, self::getArgs($callback)); |
| 72 | 72 | } else { |
| 73 | 73 | $controller = self::getController(); |
@@ -16,218 +16,218 @@ |
||
| 16 | 16 | |
| 17 | 17 | class ViewCache |
| 18 | 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)){ |
|
| 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 | - } |
|
| 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)){ |
|
| 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 | 233 | } |
| 234 | 234 | \ No newline at end of file |
@@ -67,10 +67,10 @@ discard block |
||
| 67 | 67 | */ |
| 68 | 68 | public function __construct() |
| 69 | 69 | { |
| 70 | - if (config()->get('resource_cache')){ |
|
| 70 | + if (config()->get('resource_cache')) { |
|
| 71 | 71 | try { |
| 72 | - config()->import(new Setup('config','view_cache')); |
|
| 73 | - }catch (Exception $exception){ |
|
| 72 | + config()->import(new Setup('config', 'view_cache')); |
|
| 73 | + } catch (Exception $exception) { |
|
| 74 | 74 | throw new Exception($exception->getMessage()); |
| 75 | 75 | } |
| 76 | 76 | } |
@@ -172,11 +172,11 @@ discard block |
||
| 172 | 172 | */ |
| 173 | 173 | public function isEnabled(): bool |
| 174 | 174 | { |
| 175 | - if (!is_null($this->isEnabled)){ |
|
| 175 | + if (!is_null($this->isEnabled)) { |
|
| 176 | 176 | return $this->isEnabled; |
| 177 | 177 | } |
| 178 | 178 | |
| 179 | - if (is_bool(config()->get('resource_cache')) && config()->get('resource_cache') && !empty(session()->getId())){ |
|
| 179 | + if (is_bool(config()->get('resource_cache')) && config()->get('resource_cache') && !empty(session()->getId())) { |
|
| 180 | 180 | return true; |
| 181 | 181 | } |
| 182 | 182 | |
@@ -70,7 +70,7 @@ |
||
| 70 | 70 | if (config()->get('resource_cache')){ |
| 71 | 71 | try { |
| 72 | 72 | config()->import(new Setup('config','view_cache')); |
| 73 | - }catch (Exception $exception){ |
|
| 73 | + } catch (Exception $exception){ |
|
| 74 | 74 | throw new Exception($exception->getMessage()); |
| 75 | 75 | } |
| 76 | 76 | } |