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