@@ -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 |
@@ -70,7 +70,7 @@ |
||
| 70 | 70 | $response->html($content); |
| 71 | 71 | }, []); |
| 72 | 72 | |
| 73 | - }elseif ($callback) { |
|
| 73 | + } elseif ($callback) { |
|
| 74 | 74 | call_user_func_array($callback, self::getArgs($callback)); |
| 75 | 75 | } else { |
| 76 | 76 | $controller = self::getController(); |
@@ -39,127 +39,127 @@ |
||
| 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 | - $cacheSettings = route_cache_settings(); |
|
| 65 | - |
|
| 66 | - if ($cacheSettings && |
|
| 67 | - session()->getId() && |
|
| 68 | - ViewCache::getInstance()->exists(route_uri(), session()->getId(), $cacheSettings['ttl']) |
|
| 69 | - ){ |
|
| 70 | - call_user_func_array(function () use (&$response, $cacheSettings) { |
|
| 71 | - $content = ViewCache::getInstance()->get(route_uri(), session()->getId(), $cacheSettings['ttl']); |
|
| 72 | - $response->html($content); |
|
| 73 | - }, []); |
|
| 74 | - |
|
| 75 | - }elseif ($callback) { |
|
| 76 | - call_user_func_array($callback, self::getArgs($callback)); |
|
| 77 | - } else { |
|
| 78 | - $controller = self::getController(); |
|
| 79 | - $action = self::getAction($controller); |
|
| 80 | - |
|
| 81 | - if ($controller->csrfVerification && in_array($request->getMethod(), Csrf::METHODS)) { |
|
| 82 | - csrf()->checkToken($request); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - if (method_exists($controller, '__before')) { |
|
| 86 | - call_user_func_array([$controller, '__before'], self::getArgs([$controller, '__before'])); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - call_user_func_array([$controller, $action], self::getArgs([$controller, $action])); |
|
| 90 | - |
|
| 91 | - if (method_exists($controller, '__after')) { |
|
| 92 | - call_user_func_array([$controller, '__after'], self::getArgs([$controller, '__after'])); |
|
| 93 | - } |
|
| 94 | - } |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Get Controller |
|
| 99 | - * @return QtController |
|
| 100 | - * @throws DiException |
|
| 101 | - * @throws ReflectionException |
|
| 102 | - * @throws ControllerException |
|
| 103 | - */ |
|
| 104 | - private static function getController(): QtController |
|
| 105 | - { |
|
| 106 | - $fs = Di::get(FileSystem::class); |
|
| 107 | - |
|
| 108 | - $controllerPath = modules_dir() . DS . current_module() . DS . 'Controllers' . DS . current_controller() . '.php'; |
|
| 109 | - |
|
| 110 | - if (!$fs->exists($controllerPath)) { |
|
| 111 | - throw ControllerException::controllerNotFound(current_controller()); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - require_once $controllerPath; |
|
| 115 | - |
|
| 116 | - $controllerClass = '\\Modules\\' . current_module() . '\\Controllers\\' . current_controller(); |
|
| 117 | - |
|
| 118 | - if (!class_exists($controllerClass, false)) { |
|
| 119 | - throw ControllerException::controllerNotDefined(current_controller()); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - return new $controllerClass(); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Get Action |
|
| 127 | - * @param QtController $controller |
|
| 128 | - * @return string|null |
|
| 129 | - * @throws ControllerException |
|
| 130 | - */ |
|
| 131 | - private static function getAction(QtController $controller): ?string |
|
| 132 | - { |
|
| 133 | - $action = current_action(); |
|
| 134 | - |
|
| 135 | - if ($action && !method_exists($controller, $action)) { |
|
| 136 | - throw ControllerException::actionNotDefined($action); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - return $action; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * Get arguments |
|
| 144 | - * @param callable $callable |
|
| 145 | - * @return array |
|
| 146 | - * @throws DiException |
|
| 147 | - * @throws ReflectionException |
|
| 148 | - */ |
|
| 149 | - private static function getArgs(callable $callable): array |
|
| 150 | - { |
|
| 151 | - return Di::autowire($callable, self::routeParams()); |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * Gets the route parameters |
|
| 156 | - * @return array |
|
| 157 | - */ |
|
| 158 | - private static function routeParams(): array |
|
| 159 | - { |
|
| 160 | - return array_map(function ($param) { |
|
| 161 | - return $param['value']; |
|
| 162 | - }, route_params()); |
|
| 163 | - } |
|
| 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 | + $cacheSettings = route_cache_settings(); |
|
| 65 | + |
|
| 66 | + if ($cacheSettings && |
|
| 67 | + session()->getId() && |
|
| 68 | + ViewCache::getInstance()->exists(route_uri(), session()->getId(), $cacheSettings['ttl']) |
|
| 69 | + ){ |
|
| 70 | + call_user_func_array(function () use (&$response, $cacheSettings) { |
|
| 71 | + $content = ViewCache::getInstance()->get(route_uri(), session()->getId(), $cacheSettings['ttl']); |
|
| 72 | + $response->html($content); |
|
| 73 | + }, []); |
|
| 74 | + |
|
| 75 | + }elseif ($callback) { |
|
| 76 | + call_user_func_array($callback, self::getArgs($callback)); |
|
| 77 | + } else { |
|
| 78 | + $controller = self::getController(); |
|
| 79 | + $action = self::getAction($controller); |
|
| 80 | + |
|
| 81 | + if ($controller->csrfVerification && in_array($request->getMethod(), Csrf::METHODS)) { |
|
| 82 | + csrf()->checkToken($request); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + if (method_exists($controller, '__before')) { |
|
| 86 | + call_user_func_array([$controller, '__before'], self::getArgs([$controller, '__before'])); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + call_user_func_array([$controller, $action], self::getArgs([$controller, $action])); |
|
| 90 | + |
|
| 91 | + if (method_exists($controller, '__after')) { |
|
| 92 | + call_user_func_array([$controller, '__after'], self::getArgs([$controller, '__after'])); |
|
| 93 | + } |
|
| 94 | + } |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Get Controller |
|
| 99 | + * @return QtController |
|
| 100 | + * @throws DiException |
|
| 101 | + * @throws ReflectionException |
|
| 102 | + * @throws ControllerException |
|
| 103 | + */ |
|
| 104 | + private static function getController(): QtController |
|
| 105 | + { |
|
| 106 | + $fs = Di::get(FileSystem::class); |
|
| 107 | + |
|
| 108 | + $controllerPath = modules_dir() . DS . current_module() . DS . 'Controllers' . DS . current_controller() . '.php'; |
|
| 109 | + |
|
| 110 | + if (!$fs->exists($controllerPath)) { |
|
| 111 | + throw ControllerException::controllerNotFound(current_controller()); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + require_once $controllerPath; |
|
| 115 | + |
|
| 116 | + $controllerClass = '\\Modules\\' . current_module() . '\\Controllers\\' . current_controller(); |
|
| 117 | + |
|
| 118 | + if (!class_exists($controllerClass, false)) { |
|
| 119 | + throw ControllerException::controllerNotDefined(current_controller()); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + return new $controllerClass(); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Get Action |
|
| 127 | + * @param QtController $controller |
|
| 128 | + * @return string|null |
|
| 129 | + * @throws ControllerException |
|
| 130 | + */ |
|
| 131 | + private static function getAction(QtController $controller): ?string |
|
| 132 | + { |
|
| 133 | + $action = current_action(); |
|
| 134 | + |
|
| 135 | + if ($action && !method_exists($controller, $action)) { |
|
| 136 | + throw ControllerException::actionNotDefined($action); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + return $action; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * Get arguments |
|
| 144 | + * @param callable $callable |
|
| 145 | + * @return array |
|
| 146 | + * @throws DiException |
|
| 147 | + * @throws ReflectionException |
|
| 148 | + */ |
|
| 149 | + private static function getArgs(callable $callable): array |
|
| 150 | + { |
|
| 151 | + return Di::autowire($callable, self::routeParams()); |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * Gets the route parameters |
|
| 156 | + * @return array |
|
| 157 | + */ |
|
| 158 | + private static function routeParams(): array |
|
| 159 | + { |
|
| 160 | + return array_map(function ($param) { |
|
| 161 | + return $param['value']; |
|
| 162 | + }, route_params()); |
|
| 163 | + } |
|
| 164 | 164 | |
| 165 | 165 | } |
@@ -66,8 +66,8 @@ discard block |
||
| 66 | 66 | if ($cacheSettings && |
| 67 | 67 | session()->getId() && |
| 68 | 68 | ViewCache::getInstance()->exists(route_uri(), session()->getId(), $cacheSettings['ttl']) |
| 69 | - ){ |
|
| 70 | - call_user_func_array(function () use (&$response, $cacheSettings) { |
|
| 69 | + ) { |
|
| 70 | + call_user_func_array(function() use (&$response, $cacheSettings) { |
|
| 71 | 71 | $content = ViewCache::getInstance()->get(route_uri(), session()->getId(), $cacheSettings['ttl']); |
| 72 | 72 | $response->html($content); |
| 73 | 73 | }, []); |
@@ -157,7 +157,7 @@ discard block |
||
| 157 | 157 | */ |
| 158 | 158 | private static function routeParams(): array |
| 159 | 159 | { |
| 160 | - return array_map(function ($param) { |
|
| 160 | + return array_map(function($param) { |
|
| 161 | 161 | return $param['value']; |
| 162 | 162 | }, route_params()); |
| 163 | 163 | } |
@@ -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 | } |
@@ -73,35 +73,35 @@ discard block |
||
| 73 | 73 | */ |
| 74 | 74 | private $virtualRoutes = []; |
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * @param array $module |
|
| 78 | - * @throws ConfigException |
|
| 79 | - * @throws DiException |
|
| 80 | - * @throws ReflectionException |
|
| 81 | - */ |
|
| 76 | + /** |
|
| 77 | + * @param array $module |
|
| 78 | + * @throws ConfigException |
|
| 79 | + * @throws DiException |
|
| 80 | + * @throws ReflectionException |
|
| 81 | + */ |
|
| 82 | 82 | public function __construct(array $module) |
| 83 | 83 | { |
| 84 | 84 | $this->virtualRoutes['*'] = []; |
| 85 | 85 | $this->moduleName = key($module); |
| 86 | 86 | $this->moduleOptions = $module[key($module)]; |
| 87 | 87 | |
| 88 | - if (config()->has('resource_cache') && !config()->has('view_cache')){ |
|
| 89 | - config()->import(new Setup('config', 'view_cache')); |
|
| 90 | - } |
|
| 88 | + if (config()->has('resource_cache') && !config()->has('view_cache')){ |
|
| 89 | + config()->import(new Setup('config', 'view_cache')); |
|
| 90 | + } |
|
| 91 | 91 | } |
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * @param string $route |
|
| 95 | - * @param string $method |
|
| 96 | - * @param ...$params |
|
| 97 | - * @return $this |
|
| 98 | - * @throws ConfigException |
|
| 99 | - * @throws DatabaseException |
|
| 100 | - * @throws DiException |
|
| 101 | - * @throws LangException |
|
| 102 | - * @throws ReflectionException |
|
| 103 | - * @throws SessionException |
|
| 104 | - */ |
|
| 93 | + /** |
|
| 94 | + * @param string $route |
|
| 95 | + * @param string $method |
|
| 96 | + * @param ...$params |
|
| 97 | + * @return $this |
|
| 98 | + * @throws ConfigException |
|
| 99 | + * @throws DatabaseException |
|
| 100 | + * @throws DiException |
|
| 101 | + * @throws LangException |
|
| 102 | + * @throws ReflectionException |
|
| 103 | + * @throws SessionException |
|
| 104 | + */ |
|
| 105 | 105 | public function add(string $route, string $method, ...$params): Route |
| 106 | 106 | { |
| 107 | 107 | $this->currentRoute = [ |
@@ -111,12 +111,12 @@ discard block |
||
| 111 | 111 | 'module' => $this->moduleName |
| 112 | 112 | ]; |
| 113 | 113 | |
| 114 | - if ($this->canSetCacheToCurrentRoute()){ |
|
| 115 | - $this->currentRoute['cache_settings'] = [ |
|
| 116 | - 'shouldCache' => true, |
|
| 117 | - 'ttl' => config()->get('view_cache.ttl', 300), |
|
| 118 | - ]; |
|
| 119 | - } |
|
| 114 | + if ($this->canSetCacheToCurrentRoute()){ |
|
| 115 | + $this->currentRoute['cache_settings'] = [ |
|
| 116 | + 'shouldCache' => true, |
|
| 117 | + 'ttl' => config()->get('view_cache.ttl', 300), |
|
| 118 | + ]; |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | 121 | if (is_callable($params[0])) { |
| 122 | 122 | $this->currentRoute['callback'] = $params[0]; |
@@ -137,33 +137,33 @@ discard block |
||
| 137 | 137 | return $this; |
| 138 | 138 | } |
| 139 | 139 | |
| 140 | - /** |
|
| 141 | - * @param string $route |
|
| 142 | - * @param ...$params |
|
| 143 | - * @return $this |
|
| 144 | - * @throws ConfigException |
|
| 145 | - * @throws DatabaseException |
|
| 146 | - * @throws DiException |
|
| 147 | - * @throws LangException |
|
| 148 | - * @throws ReflectionException |
|
| 149 | - * @throws SessionException |
|
| 150 | - */ |
|
| 140 | + /** |
|
| 141 | + * @param string $route |
|
| 142 | + * @param ...$params |
|
| 143 | + * @return $this |
|
| 144 | + * @throws ConfigException |
|
| 145 | + * @throws DatabaseException |
|
| 146 | + * @throws DiException |
|
| 147 | + * @throws LangException |
|
| 148 | + * @throws ReflectionException |
|
| 149 | + * @throws SessionException |
|
| 150 | + */ |
|
| 151 | 151 | public function get(string $route, ...$params): Route |
| 152 | 152 | { |
| 153 | 153 | return $this->add($route, 'GET', ...$params); |
| 154 | 154 | } |
| 155 | 155 | |
| 156 | - /** |
|
| 157 | - * @param string $route |
|
| 158 | - * @param ...$params |
|
| 159 | - * @return $this |
|
| 160 | - * @throws ConfigException |
|
| 161 | - * @throws DatabaseException |
|
| 162 | - * @throws DiException |
|
| 163 | - * @throws LangException |
|
| 164 | - * @throws ReflectionException |
|
| 165 | - * @throws SessionException |
|
| 166 | - */ |
|
| 156 | + /** |
|
| 157 | + * @param string $route |
|
| 158 | + * @param ...$params |
|
| 159 | + * @return $this |
|
| 160 | + * @throws ConfigException |
|
| 161 | + * @throws DatabaseException |
|
| 162 | + * @throws DiException |
|
| 163 | + * @throws LangException |
|
| 164 | + * @throws ReflectionException |
|
| 165 | + * @throws SessionException |
|
| 166 | + */ |
|
| 167 | 167 | public function post(string $route, ...$params): Route |
| 168 | 168 | { |
| 169 | 169 | return $this->add($route, 'POST', ...$params); |
@@ -219,53 +219,53 @@ discard block |
||
| 219 | 219 | return $this; |
| 220 | 220 | } |
| 221 | 221 | |
| 222 | - /** |
|
| 223 | - * @param bool $shouldCache |
|
| 224 | - * @param int|null $ttl |
|
| 225 | - * @return $this |
|
| 226 | - * @throws ConfigException |
|
| 227 | - * @throws DatabaseException |
|
| 228 | - * @throws DiException |
|
| 229 | - * @throws LangException |
|
| 230 | - * @throws ReflectionException |
|
| 231 | - * @throws SessionException |
|
| 232 | - */ |
|
| 233 | - public function cacheable(bool $shouldCache, int $ttl = null): Route |
|
| 234 | - { |
|
| 235 | - if (empty(session()->getId())){ |
|
| 236 | - return $this; |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - if (!$ttl) { |
|
| 240 | - $ttl = config()->get('view_cache.ttl', 300); |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - if (!$this->isGroup){ |
|
| 244 | - end($this->virtualRoutes['*']); |
|
| 245 | - $lastKey = key($this->virtualRoutes['*']); |
|
| 246 | - |
|
| 247 | - if (!$shouldCache && key_exists('cache_settings', $this->virtualRoutes['*'][$lastKey])) { |
|
| 248 | - unset($this->virtualRoutes['*'][$lastKey]['cache_settings']); |
|
| 249 | - }else{ |
|
| 250 | - $this->assignCacheToCurrentRoute($this->virtualRoutes['*'][$lastKey], $shouldCache, $ttl); |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - return $this; |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - end($this->virtualRoutes); |
|
| 257 | - $lastKeyOfFirstRound = key($this->virtualRoutes); |
|
| 258 | - |
|
| 259 | - foreach ($this->virtualRoutes[$lastKeyOfFirstRound] as &$route) { |
|
| 260 | - if (!$shouldCache && key_exists('cache_settings', $route)) { |
|
| 261 | - unset($route['cache_settings']); |
|
| 262 | - }else{ |
|
| 263 | - $this->assignCacheToCurrentRoute($route, $shouldCache, $ttl); |
|
| 264 | - } |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - return $this; |
|
| 268 | - } |
|
| 222 | + /** |
|
| 223 | + * @param bool $shouldCache |
|
| 224 | + * @param int|null $ttl |
|
| 225 | + * @return $this |
|
| 226 | + * @throws ConfigException |
|
| 227 | + * @throws DatabaseException |
|
| 228 | + * @throws DiException |
|
| 229 | + * @throws LangException |
|
| 230 | + * @throws ReflectionException |
|
| 231 | + * @throws SessionException |
|
| 232 | + */ |
|
| 233 | + public function cacheable(bool $shouldCache, int $ttl = null): Route |
|
| 234 | + { |
|
| 235 | + if (empty(session()->getId())){ |
|
| 236 | + return $this; |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + if (!$ttl) { |
|
| 240 | + $ttl = config()->get('view_cache.ttl', 300); |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + if (!$this->isGroup){ |
|
| 244 | + end($this->virtualRoutes['*']); |
|
| 245 | + $lastKey = key($this->virtualRoutes['*']); |
|
| 246 | + |
|
| 247 | + if (!$shouldCache && key_exists('cache_settings', $this->virtualRoutes['*'][$lastKey])) { |
|
| 248 | + unset($this->virtualRoutes['*'][$lastKey]['cache_settings']); |
|
| 249 | + }else{ |
|
| 250 | + $this->assignCacheToCurrentRoute($this->virtualRoutes['*'][$lastKey], $shouldCache, $ttl); |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + return $this; |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + end($this->virtualRoutes); |
|
| 257 | + $lastKeyOfFirstRound = key($this->virtualRoutes); |
|
| 258 | + |
|
| 259 | + foreach ($this->virtualRoutes[$lastKeyOfFirstRound] as &$route) { |
|
| 260 | + if (!$shouldCache && key_exists('cache_settings', $route)) { |
|
| 261 | + unset($route['cache_settings']); |
|
| 262 | + }else{ |
|
| 263 | + $this->assignCacheToCurrentRoute($route, $shouldCache, $ttl); |
|
| 264 | + } |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + return $this; |
|
| 268 | + } |
|
| 269 | 269 | |
| 270 | 270 | /** |
| 271 | 271 | * Sets a unique name for a route |
@@ -340,34 +340,34 @@ discard block |
||
| 340 | 340 | } |
| 341 | 341 | } |
| 342 | 342 | |
| 343 | - /** |
|
| 344 | - * @param array $route |
|
| 345 | - * @param bool $shouldCache |
|
| 346 | - * @param int $ttl |
|
| 347 | - * @return void |
|
| 348 | - */ |
|
| 349 | - private function assignCacheToCurrentRoute(array &$route, bool $shouldCache, int $ttl) |
|
| 350 | - { |
|
| 351 | - $route['cache_settings'] = [ |
|
| 352 | - 'shouldCache' => $shouldCache, |
|
| 353 | - 'ttl' => $ttl, |
|
| 354 | - ]; |
|
| 355 | - } |
|
| 356 | - |
|
| 357 | - /** |
|
| 358 | - * @return bool |
|
| 359 | - * @throws ConfigException |
|
| 360 | - * @throws DatabaseException |
|
| 361 | - * @throws DiException |
|
| 362 | - * @throws LangException |
|
| 363 | - * @throws SessionException |
|
| 364 | - * @throws ReflectionException |
|
| 365 | - */ |
|
| 366 | - private function canSetCacheToCurrentRoute(): bool |
|
| 367 | - { |
|
| 368 | - return config()->get('resource_cache') && |
|
| 369 | - !empty(session()->getId()) && |
|
| 370 | - !empty($this->moduleOptions) && |
|
| 371 | - !empty($this->moduleOptions['cacheable']); |
|
| 372 | - } |
|
| 343 | + /** |
|
| 344 | + * @param array $route |
|
| 345 | + * @param bool $shouldCache |
|
| 346 | + * @param int $ttl |
|
| 347 | + * @return void |
|
| 348 | + */ |
|
| 349 | + private function assignCacheToCurrentRoute(array &$route, bool $shouldCache, int $ttl) |
|
| 350 | + { |
|
| 351 | + $route['cache_settings'] = [ |
|
| 352 | + 'shouldCache' => $shouldCache, |
|
| 353 | + 'ttl' => $ttl, |
|
| 354 | + ]; |
|
| 355 | + } |
|
| 356 | + |
|
| 357 | + /** |
|
| 358 | + * @return bool |
|
| 359 | + * @throws ConfigException |
|
| 360 | + * @throws DatabaseException |
|
| 361 | + * @throws DiException |
|
| 362 | + * @throws LangException |
|
| 363 | + * @throws SessionException |
|
| 364 | + * @throws ReflectionException |
|
| 365 | + */ |
|
| 366 | + private function canSetCacheToCurrentRoute(): bool |
|
| 367 | + { |
|
| 368 | + return config()->get('resource_cache') && |
|
| 369 | + !empty(session()->getId()) && |
|
| 370 | + !empty($this->moduleOptions) && |
|
| 371 | + !empty($this->moduleOptions['cacheable']); |
|
| 372 | + } |
|
| 373 | 373 | } |
@@ -85,7 +85,7 @@ discard block |
||
| 85 | 85 | $this->moduleName = key($module); |
| 86 | 86 | $this->moduleOptions = $module[key($module)]; |
| 87 | 87 | |
| 88 | - if (config()->has('resource_cache') && !config()->has('view_cache')){ |
|
| 88 | + if (config()->has('resource_cache') && !config()->has('view_cache')) { |
|
| 89 | 89 | config()->import(new Setup('config', 'view_cache')); |
| 90 | 90 | } |
| 91 | 91 | } |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | 'module' => $this->moduleName |
| 112 | 112 | ]; |
| 113 | 113 | |
| 114 | - if ($this->canSetCacheToCurrentRoute()){ |
|
| 114 | + if ($this->canSetCacheToCurrentRoute()) { |
|
| 115 | 115 | $this->currentRoute['cache_settings'] = [ |
| 116 | 116 | 'shouldCache' => true, |
| 117 | 117 | 'ttl' => config()->get('view_cache.ttl', 300), |
@@ -232,7 +232,7 @@ discard block |
||
| 232 | 232 | */ |
| 233 | 233 | public function cacheable(bool $shouldCache, int $ttl = null): Route |
| 234 | 234 | { |
| 235 | - if (empty(session()->getId())){ |
|
| 235 | + if (empty(session()->getId())) { |
|
| 236 | 236 | return $this; |
| 237 | 237 | } |
| 238 | 238 | |
@@ -240,13 +240,13 @@ discard block |
||
| 240 | 240 | $ttl = config()->get('view_cache.ttl', 300); |
| 241 | 241 | } |
| 242 | 242 | |
| 243 | - if (!$this->isGroup){ |
|
| 243 | + if (!$this->isGroup) { |
|
| 244 | 244 | end($this->virtualRoutes['*']); |
| 245 | 245 | $lastKey = key($this->virtualRoutes['*']); |
| 246 | 246 | |
| 247 | 247 | if (!$shouldCache && key_exists('cache_settings', $this->virtualRoutes['*'][$lastKey])) { |
| 248 | 248 | unset($this->virtualRoutes['*'][$lastKey]['cache_settings']); |
| 249 | - }else{ |
|
| 249 | + } else { |
|
| 250 | 250 | $this->assignCacheToCurrentRoute($this->virtualRoutes['*'][$lastKey], $shouldCache, $ttl); |
| 251 | 251 | } |
| 252 | 252 | |
@@ -259,7 +259,7 @@ discard block |
||
| 259 | 259 | foreach ($this->virtualRoutes[$lastKeyOfFirstRound] as &$route) { |
| 260 | 260 | if (!$shouldCache && key_exists('cache_settings', $route)) { |
| 261 | 261 | unset($route['cache_settings']); |
| 262 | - }else{ |
|
| 262 | + } else { |
|
| 263 | 263 | $this->assignCacheToCurrentRoute($route, $shouldCache, $ttl); |
| 264 | 264 | } |
| 265 | 265 | } |
@@ -246,7 +246,7 @@ discard block |
||
| 246 | 246 | |
| 247 | 247 | if (!$shouldCache && key_exists('cache_settings', $this->virtualRoutes['*'][$lastKey])) { |
| 248 | 248 | unset($this->virtualRoutes['*'][$lastKey]['cache_settings']); |
| 249 | - }else{ |
|
| 249 | + } else{ |
|
| 250 | 250 | $this->assignCacheToCurrentRoute($this->virtualRoutes['*'][$lastKey], $shouldCache, $ttl); |
| 251 | 251 | } |
| 252 | 252 | |
@@ -259,7 +259,7 @@ discard block |
||
| 259 | 259 | foreach ($this->virtualRoutes[$lastKeyOfFirstRound] as &$route) { |
| 260 | 260 | if (!$shouldCache && key_exists('cache_settings', $route)) { |
| 261 | 261 | unset($route['cache_settings']); |
| 262 | - }else{ |
|
| 262 | + } else{ |
|
| 263 | 263 | $this->assignCacheToCurrentRoute($route, $shouldCache, $ttl); |
| 264 | 264 | } |
| 265 | 265 | } |
@@ -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 | /** |
@@ -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 (($cacheSettings = route_cache_settings()) && session()->getId()){ |
|
| 185 | - $content = ViewCache::getInstance() |
|
| 186 | - ->set(route_uri(), $content, session()->getId()) |
|
| 187 | - ->get(route_uri(), session()->getId(), $cacheSettings['ttl']); |
|
| 188 | - } |
|
| 184 | + if (($cacheSettings = route_cache_settings()) && session()->getId()){ |
|
| 185 | + $content = ViewCache::getInstance() |
|
| 186 | + ->set(route_uri(), $content, session()->getId()) |
|
| 187 | + ->get(route_uri(), session()->getId(), $cacheSettings['ttl']); |
|
| 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 (($cacheSettings = route_cache_settings()) && session()->getId()){ |
|
| 185 | - $content = ViewCache::getInstance() |
|
| 184 | + if (($cacheSettings = route_cache_settings()) && session()->getId()) { |
|
| 185 | + $content = ViewCache::getInstance() |
|
| 186 | 186 | ->set(route_uri(), $content, session()->getId()) |
| 187 | 187 | ->get(route_uri(), session()->getId(), $cacheSettings['ttl']); |
| 188 | 188 | } |
@@ -11,159 +11,159 @@ |
||
| 11 | 11 | |
| 12 | 12 | class ViewCache |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * @var string |
|
| 16 | - */ |
|
| 17 | - private $cacheDir; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * @var string |
|
| 21 | - */ |
|
| 22 | - private $mimeType = '.tmp'; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * @var object |
|
| 26 | - */ |
|
| 27 | - private $fs; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * @var ViewCache |
|
| 31 | - */ |
|
| 32 | - private static $instance = null; |
|
| 33 | - |
|
| 34 | - public static function getInstance(): ViewCache |
|
| 35 | - { |
|
| 36 | - if (self::$instance === null) { |
|
| 37 | - self::$instance = new self(); |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - return self::$instance; |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * @throws DiException |
|
| 45 | - * @throws ReflectionException |
|
| 46 | - * @throws Exception |
|
| 47 | - */ |
|
| 48 | - public function __construct() |
|
| 49 | - { |
|
| 50 | - $this->fs = Di::get(FileSystem::class); |
|
| 51 | - |
|
| 52 | - if (!config()->has('view_cache')) { |
|
| 53 | - throw new Exception('The config "view_cache" does not exists.'); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - $this->cacheDir = self::getCacheDir(); |
|
| 57 | - |
|
| 58 | - if (!$this->fs->isDirectory($this->cacheDir)) { |
|
| 59 | - mkdir($this->cacheDir, 0777, true); |
|
| 60 | - } |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * @param string $key |
|
| 65 | - * @param string $content |
|
| 66 | - * @param string $sessionId |
|
| 67 | - * @return ViewCache |
|
| 68 | - */ |
|
| 69 | - public function set(string $key, string $content, string $sessionId): ViewCache |
|
| 70 | - { |
|
| 71 | - if (config()->has('view_cache.minify')) { |
|
| 72 | - $content = $this->minify($content); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - $cacheFile = $this->getCacheFile($key, $sessionId); |
|
| 76 | - $this->fs->put($cacheFile, $content); |
|
| 77 | - |
|
| 78 | - return $this; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * @param string $key |
|
| 83 | - * @param string $sessionId |
|
| 84 | - * @param int $ttl |
|
| 85 | - * @return mixed|null |
|
| 86 | - */ |
|
| 87 | - public function get(string $key, string $sessionId, int $ttl): ?string |
|
| 88 | - { |
|
| 89 | - $cacheFile = $this->getCacheFile($key, $sessionId); |
|
| 90 | - if (!$this->fs->exists($cacheFile)) { |
|
| 91 | - return null; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - $data = $this->fs->get($cacheFile); |
|
| 95 | - if (time() > ($this->fs->lastModified($cacheFile) + $ttl)) { |
|
| 96 | - $this->fs->remove($cacheFile); |
|
| 97 | - return null; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - return $data; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * @param string $key |
|
| 105 | - * @param string $sessionId |
|
| 106 | - * @return void |
|
| 107 | - */ |
|
| 108 | - public function delete(string $key, string $sessionId): void |
|
| 109 | - { |
|
| 110 | - $cacheFile = $this->getCacheFile($key, $sessionId); |
|
| 111 | - if ($this->fs->exists($cacheFile)) { |
|
| 112 | - $this->fs->remove($cacheFile); |
|
| 113 | - } |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * @param string $key |
|
| 118 | - * @param string $sessionId |
|
| 119 | - * @param int $ttl |
|
| 120 | - * @return bool |
|
| 121 | - */ |
|
| 122 | - public function exists(string $key, string $sessionId, int $ttl): bool |
|
| 123 | - { |
|
| 124 | - $cacheFile = $this->getCacheFile($key, $sessionId); |
|
| 125 | - |
|
| 126 | - if (!$this->fs->exists($cacheFile)) { |
|
| 127 | - return false; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - if (time() > ($this->fs->lastModified($cacheFile) + $ttl)) { |
|
| 131 | - $this->fs->remove($cacheFile); |
|
| 132 | - return false; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - return true; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - private static function getCacheDir(): string |
|
| 139 | - { |
|
| 140 | - $configCacheDir = config()->get('view_cache.cache_dir', 'cache'); |
|
| 141 | - |
|
| 142 | - $cacheDir = base_dir() . DS . $configCacheDir . DS . 'views' . DS; |
|
| 143 | - |
|
| 144 | - if ($module = current_module()) { |
|
| 145 | - $cacheDir = base_dir() . DS . $configCacheDir . DS . 'views' . DS . strtolower($module) . DS; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - return $cacheDir; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * @param string $key |
|
| 153 | - * @param string $sessionId |
|
| 154 | - * @return string |
|
| 155 | - */ |
|
| 156 | - private function getCacheFile(string $key, string $sessionId): string |
|
| 157 | - { |
|
| 158 | - return $this->cacheDir . md5($key . $sessionId) . $this->mimeType; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * @param string $content |
|
| 163 | - * @return string |
|
| 164 | - */ |
|
| 165 | - private function minify(string $content): string |
|
| 166 | - { |
|
| 167 | - return (new HtmlMin())->minify($content); |
|
| 168 | - } |
|
| 14 | + /** |
|
| 15 | + * @var string |
|
| 16 | + */ |
|
| 17 | + private $cacheDir; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * @var string |
|
| 21 | + */ |
|
| 22 | + private $mimeType = '.tmp'; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * @var object |
|
| 26 | + */ |
|
| 27 | + private $fs; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * @var ViewCache |
|
| 31 | + */ |
|
| 32 | + private static $instance = null; |
|
| 33 | + |
|
| 34 | + public static function getInstance(): ViewCache |
|
| 35 | + { |
|
| 36 | + if (self::$instance === null) { |
|
| 37 | + self::$instance = new self(); |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + return self::$instance; |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * @throws DiException |
|
| 45 | + * @throws ReflectionException |
|
| 46 | + * @throws Exception |
|
| 47 | + */ |
|
| 48 | + public function __construct() |
|
| 49 | + { |
|
| 50 | + $this->fs = Di::get(FileSystem::class); |
|
| 51 | + |
|
| 52 | + if (!config()->has('view_cache')) { |
|
| 53 | + throw new Exception('The config "view_cache" does not exists.'); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + $this->cacheDir = self::getCacheDir(); |
|
| 57 | + |
|
| 58 | + if (!$this->fs->isDirectory($this->cacheDir)) { |
|
| 59 | + mkdir($this->cacheDir, 0777, true); |
|
| 60 | + } |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * @param string $key |
|
| 65 | + * @param string $content |
|
| 66 | + * @param string $sessionId |
|
| 67 | + * @return ViewCache |
|
| 68 | + */ |
|
| 69 | + public function set(string $key, string $content, string $sessionId): ViewCache |
|
| 70 | + { |
|
| 71 | + if (config()->has('view_cache.minify')) { |
|
| 72 | + $content = $this->minify($content); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + $cacheFile = $this->getCacheFile($key, $sessionId); |
|
| 76 | + $this->fs->put($cacheFile, $content); |
|
| 77 | + |
|
| 78 | + return $this; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * @param string $key |
|
| 83 | + * @param string $sessionId |
|
| 84 | + * @param int $ttl |
|
| 85 | + * @return mixed|null |
|
| 86 | + */ |
|
| 87 | + public function get(string $key, string $sessionId, int $ttl): ?string |
|
| 88 | + { |
|
| 89 | + $cacheFile = $this->getCacheFile($key, $sessionId); |
|
| 90 | + if (!$this->fs->exists($cacheFile)) { |
|
| 91 | + return null; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + $data = $this->fs->get($cacheFile); |
|
| 95 | + if (time() > ($this->fs->lastModified($cacheFile) + $ttl)) { |
|
| 96 | + $this->fs->remove($cacheFile); |
|
| 97 | + return null; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + return $data; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * @param string $key |
|
| 105 | + * @param string $sessionId |
|
| 106 | + * @return void |
|
| 107 | + */ |
|
| 108 | + public function delete(string $key, string $sessionId): void |
|
| 109 | + { |
|
| 110 | + $cacheFile = $this->getCacheFile($key, $sessionId); |
|
| 111 | + if ($this->fs->exists($cacheFile)) { |
|
| 112 | + $this->fs->remove($cacheFile); |
|
| 113 | + } |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * @param string $key |
|
| 118 | + * @param string $sessionId |
|
| 119 | + * @param int $ttl |
|
| 120 | + * @return bool |
|
| 121 | + */ |
|
| 122 | + public function exists(string $key, string $sessionId, int $ttl): bool |
|
| 123 | + { |
|
| 124 | + $cacheFile = $this->getCacheFile($key, $sessionId); |
|
| 125 | + |
|
| 126 | + if (!$this->fs->exists($cacheFile)) { |
|
| 127 | + return false; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + if (time() > ($this->fs->lastModified($cacheFile) + $ttl)) { |
|
| 131 | + $this->fs->remove($cacheFile); |
|
| 132 | + return false; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + return true; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + private static function getCacheDir(): string |
|
| 139 | + { |
|
| 140 | + $configCacheDir = config()->get('view_cache.cache_dir', 'cache'); |
|
| 141 | + |
|
| 142 | + $cacheDir = base_dir() . DS . $configCacheDir . DS . 'views' . DS; |
|
| 143 | + |
|
| 144 | + if ($module = current_module()) { |
|
| 145 | + $cacheDir = base_dir() . DS . $configCacheDir . DS . 'views' . DS . strtolower($module) . DS; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + return $cacheDir; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * @param string $key |
|
| 153 | + * @param string $sessionId |
|
| 154 | + * @return string |
|
| 155 | + */ |
|
| 156 | + private function getCacheFile(string $key, string $sessionId): string |
|
| 157 | + { |
|
| 158 | + return $this->cacheDir . md5($key . $sessionId) . $this->mimeType; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * @param string $content |
|
| 163 | + * @return string |
|
| 164 | + */ |
|
| 165 | + private function minify(string $content): string |
|
| 166 | + { |
|
| 167 | + return (new HtmlMin())->minify($content); |
|
| 168 | + } |
|
| 169 | 169 | } |
| 170 | 170 | \ No newline at end of file |