Passed
Pull Request — master (#178)
by
unknown
03:01
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/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/Libraries/ResourceCache/ViewCache.php 2 patches
Indentation   +212 added lines, -212 removed lines patch added patch discarded remove patch
@@ -16,216 +16,216 @@
 block discarded – undo
16 16
 
17 17
 class ViewCache
18 18
 {
19
-	/**
20
-	 * @var string
21
-	 */
22
-	private $cacheDir;
23
-
24
-	/**
25
-	 * @var string
26
-	 */
27
-	private $sessionId;
28
-
29
-	/**
30
-	 * @var string
31
-	 */
32
-	private $mimeType = '.tmp';
33
-
34
-	/**
35
-	 * @var int
36
-	 */
37
-	private $ttl = 300;
38
-
39
-	/**
40
-	 * @var bool
41
-	 */
42
-	private $isEnabled;
43
-
44
-	/**
45
-	 * @var object
46
-	 */
47
-	private $fs;
48
-
49
-	/**
50
-	 * @var ViewCache
51
-	 */
52
-	private static $instance = null;
53
-
54
-	public static function getInstance(): ViewCache
55
-	{
56
-		if (self::$instance === null) {
57
-			self::$instance = new self();
58
-		}
59
-
60
-		return self::$instance;
61
-	}
62
-
63
-	/**
64
-	 * @throws DiException
65
-	 * @throws ReflectionException
66
-	 * @throws Exception
67
-	 */
68
-	public function __construct()
69
-	{
70
-		if (config()->get('resource_cache')){
71
-			config()->import(new Setup('config','view_cache'));
72
-		}
73
-
74
-		$this->fs = Di::get(FileSystem::class);
75
-
76
-		$this->cacheDir = $this->getCacheDir();
77
-
78
-		$configTtl  = config()->get('view_cache.ttl');
79
-		if (is_int($configTtl)){
80
-			$this->ttl = $configTtl;
81
-		}
82
-
83
-		$this->sessionId = session()->getId();
84
-
85
-		if (!$this->fs->isDirectory($this->cacheDir)) {
86
-			mkdir($this->cacheDir, 0777, true);
87
-		}
88
-	}
89
-
90
-	/**
91
-	 * @param string $key
92
-	 * @param string $content
93
-	 * @return ViewCache
94
-	 */
95
-	public function set(string $key, string $content): ViewCache
96
-	{
97
-		if (config()->has('view_cache.minify')) {
98
-			$content = $this->minify($content);
99
-		}
100
-
101
-		$this->fs->put($this->getCacheFile($key), $content);
102
-
103
-		return $this;
104
-	}
105
-
106
-	/**
107
-	 * @param string $key
108
-	 * @return mixed|null
109
-	 */
110
-	public function get(string $key): ?string
111
-	{
112
-		if (!$this->exists($key)) {
113
-			return null;
114
-		}
115
-
116
-		return $this->fs->get($this->getCacheFile($key));
117
-	}
118
-
119
-	/**
120
-	 * @param string $key
121
-	 * @return void
122
-	 */
123
-	public function delete(string $key): void
124
-	{
125
-		$cacheFile = $this->getCacheFile($key);
126
-		if ($this->fs->exists($cacheFile)) {
127
-			$this->fs->remove($cacheFile);
128
-		}
129
-	}
130
-
131
-	/**
132
-	 * @param string $key
133
-	 * @return bool
134
-	 */
135
-	public function exists(string $key): bool
136
-	{
137
-		$cacheFile = $this->getCacheFile($key);
138
-
139
-		if (!$this->fs->exists($cacheFile) || $this->isExpired($cacheFile)) {
140
-			return false;
141
-		}
142
-
143
-		return true;
144
-	}
145
-
146
-	/**
147
-	 * @param $cacheFile
148
-	 * @return bool
149
-	 */
150
-	public function isExpired($cacheFile): bool
151
-	{
152
-		if (time() > ($this->fs->lastModified($cacheFile) + $this->ttl)) {
153
-			$this->fs->remove($cacheFile);
154
-			return true;
155
-		}
156
-
157
-		return false;
158
-	}
159
-
160
-	/**
161
-	 * @return bool
162
-	 * @throws DiException
163
-	 * @throws ReflectionException
164
-	 * @throws ConfigException
165
-	 * @throws DatabaseException
166
-	 * @throws LangException
167
-	 * @throws SessionException
168
-	 */
169
-	public function isEnabled(): bool
170
-	{
171
-		if (!is_null($this->isEnabled)){
172
-			return $this->isEnabled;
173
-		}
174
-
175
-		$resourceCache = config()->get('resource_cache');
176
-
177
-		if (is_bool($resourceCache) && $resourceCache && !empty(session()->getId())){
178
-			return true;
179
-		}
180
-
181
-		return false;
182
-	}
183
-
184
-	/**
185
-	 * @param int $ttl
186
-	 * @return void
187
-	 */
188
-	public function setTtl(int $ttl): void
189
-	{
190
-		$this->ttl = $ttl;
191
-	}
192
-
193
-	public function enableCaching(bool $state): void
194
-	{
195
-		$this->isEnabled = $state;
196
-	}
197
-
198
-	/**
199
-	 * @return string
200
-	 */
201
-	private function getCacheDir(): string
202
-	{
203
-		$configCacheDir = config()->get('view_cache.cache_dir', 'cache');
204
-
205
-		$cacheDir = base_dir() . DS . $configCacheDir . DS . 'views' . DS;
206
-
207
-		if ($module = current_module()) {
208
-			$cacheDir = base_dir() . DS . $configCacheDir . DS . 'views' . DS . strtolower($module) . DS;
209
-		}
210
-
211
-		return $cacheDir;
212
-	}
213
-
214
-	/**
215
-	 * @param string $key
216
-	 * @return string
217
-	 */
218
-	private function getCacheFile(string $key): string
219
-	{
220
-		return $this->cacheDir . md5($key . $this->sessionId) . $this->mimeType;
221
-	}
222
-
223
-	/**
224
-	 * @param string $content
225
-	 * @return string
226
-	 */
227
-	private function minify(string $content): string
228
-	{
229
-		return (new HtmlMin())->minify($content);
230
-	}
19
+    /**
20
+     * @var string
21
+     */
22
+    private $cacheDir;
23
+
24
+    /**
25
+     * @var string
26
+     */
27
+    private $sessionId;
28
+
29
+    /**
30
+     * @var string
31
+     */
32
+    private $mimeType = '.tmp';
33
+
34
+    /**
35
+     * @var int
36
+     */
37
+    private $ttl = 300;
38
+
39
+    /**
40
+     * @var bool
41
+     */
42
+    private $isEnabled;
43
+
44
+    /**
45
+     * @var object
46
+     */
47
+    private $fs;
48
+
49
+    /**
50
+     * @var ViewCache
51
+     */
52
+    private static $instance = null;
53
+
54
+    public static function getInstance(): ViewCache
55
+    {
56
+        if (self::$instance === null) {
57
+            self::$instance = new self();
58
+        }
59
+
60
+        return self::$instance;
61
+    }
62
+
63
+    /**
64
+     * @throws DiException
65
+     * @throws ReflectionException
66
+     * @throws Exception
67
+     */
68
+    public function __construct()
69
+    {
70
+        if (config()->get('resource_cache')){
71
+            config()->import(new Setup('config','view_cache'));
72
+        }
73
+
74
+        $this->fs = Di::get(FileSystem::class);
75
+
76
+        $this->cacheDir = $this->getCacheDir();
77
+
78
+        $configTtl  = config()->get('view_cache.ttl');
79
+        if (is_int($configTtl)){
80
+            $this->ttl = $configTtl;
81
+        }
82
+
83
+        $this->sessionId = session()->getId();
84
+
85
+        if (!$this->fs->isDirectory($this->cacheDir)) {
86
+            mkdir($this->cacheDir, 0777, true);
87
+        }
88
+    }
89
+
90
+    /**
91
+     * @param string $key
92
+     * @param string $content
93
+     * @return ViewCache
94
+     */
95
+    public function set(string $key, string $content): ViewCache
96
+    {
97
+        if (config()->has('view_cache.minify')) {
98
+            $content = $this->minify($content);
99
+        }
100
+
101
+        $this->fs->put($this->getCacheFile($key), $content);
102
+
103
+        return $this;
104
+    }
105
+
106
+    /**
107
+     * @param string $key
108
+     * @return mixed|null
109
+     */
110
+    public function get(string $key): ?string
111
+    {
112
+        if (!$this->exists($key)) {
113
+            return null;
114
+        }
115
+
116
+        return $this->fs->get($this->getCacheFile($key));
117
+    }
118
+
119
+    /**
120
+     * @param string $key
121
+     * @return void
122
+     */
123
+    public function delete(string $key): void
124
+    {
125
+        $cacheFile = $this->getCacheFile($key);
126
+        if ($this->fs->exists($cacheFile)) {
127
+            $this->fs->remove($cacheFile);
128
+        }
129
+    }
130
+
131
+    /**
132
+     * @param string $key
133
+     * @return bool
134
+     */
135
+    public function exists(string $key): bool
136
+    {
137
+        $cacheFile = $this->getCacheFile($key);
138
+
139
+        if (!$this->fs->exists($cacheFile) || $this->isExpired($cacheFile)) {
140
+            return false;
141
+        }
142
+
143
+        return true;
144
+    }
145
+
146
+    /**
147
+     * @param $cacheFile
148
+     * @return bool
149
+     */
150
+    public function isExpired($cacheFile): bool
151
+    {
152
+        if (time() > ($this->fs->lastModified($cacheFile) + $this->ttl)) {
153
+            $this->fs->remove($cacheFile);
154
+            return true;
155
+        }
156
+
157
+        return false;
158
+    }
159
+
160
+    /**
161
+     * @return bool
162
+     * @throws DiException
163
+     * @throws ReflectionException
164
+     * @throws ConfigException
165
+     * @throws DatabaseException
166
+     * @throws LangException
167
+     * @throws SessionException
168
+     */
169
+    public function isEnabled(): bool
170
+    {
171
+        if (!is_null($this->isEnabled)){
172
+            return $this->isEnabled;
173
+        }
174
+
175
+        $resourceCache = config()->get('resource_cache');
176
+
177
+        if (is_bool($resourceCache) && $resourceCache && !empty(session()->getId())){
178
+            return true;
179
+        }
180
+
181
+        return false;
182
+    }
183
+
184
+    /**
185
+     * @param int $ttl
186
+     * @return void
187
+     */
188
+    public function setTtl(int $ttl): void
189
+    {
190
+        $this->ttl = $ttl;
191
+    }
192
+
193
+    public function enableCaching(bool $state): void
194
+    {
195
+        $this->isEnabled = $state;
196
+    }
197
+
198
+    /**
199
+     * @return string
200
+     */
201
+    private function getCacheDir(): string
202
+    {
203
+        $configCacheDir = config()->get('view_cache.cache_dir', 'cache');
204
+
205
+        $cacheDir = base_dir() . DS . $configCacheDir . DS . 'views' . DS;
206
+
207
+        if ($module = current_module()) {
208
+            $cacheDir = base_dir() . DS . $configCacheDir . DS . 'views' . DS . strtolower($module) . DS;
209
+        }
210
+
211
+        return $cacheDir;
212
+    }
213
+
214
+    /**
215
+     * @param string $key
216
+     * @return string
217
+     */
218
+    private function getCacheFile(string $key): string
219
+    {
220
+        return $this->cacheDir . md5($key . $this->sessionId) . $this->mimeType;
221
+    }
222
+
223
+    /**
224
+     * @param string $content
225
+     * @return string
226
+     */
227
+    private function minify(string $content): string
228
+    {
229
+        return (new HtmlMin())->minify($content);
230
+    }
231 231
 }
232 232
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -67,8 +67,8 @@  discard block
 block discarded – undo
67 67
 	 */
68 68
 	public function __construct()
69 69
 	{
70
-		if (config()->get('resource_cache')){
71
-			config()->import(new Setup('config','view_cache'));
70
+		if (config()->get('resource_cache')) {
71
+			config()->import(new Setup('config', 'view_cache'));
72 72
 		}
73 73
 
74 74
 		$this->fs = Di::get(FileSystem::class);
@@ -76,7 +76,7 @@  discard block
 block discarded – undo
76 76
 		$this->cacheDir = $this->getCacheDir();
77 77
 
78 78
 		$configTtl  = config()->get('view_cache.ttl');
79
-		if (is_int($configTtl)){
79
+		if (is_int($configTtl)) {
80 80
 			$this->ttl = $configTtl;
81 81
 		}
82 82
 
@@ -168,13 +168,13 @@  discard block
 block discarded – undo
168 168
 	 */
169 169
 	public function isEnabled(): bool
170 170
 	{
171
-		if (!is_null($this->isEnabled)){
171
+		if (!is_null($this->isEnabled)) {
172 172
 			return $this->isEnabled;
173 173
 		}
174 174
 
175 175
 		$resourceCache = config()->get('resource_cache');
176 176
 
177
-		if (is_bool($resourceCache) && $resourceCache && !empty(session()->getId())){
177
+		if (is_bool($resourceCache) && $resourceCache && !empty(session()->getId())) {
178 178
 			return true;
179 179
 		}
180 180
 
Please login to merge, or discard this patch.
src/Mvc/QtView.php 2 patches
Indentation   +23 added lines, -23 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,15 +179,15 @@  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
-	    $viewCacheInstance = ViewCache::getInstance();
184
+        $viewCacheInstance = ViewCache::getInstance();
185 185
 
186
-	    if ($viewCacheInstance->isEnabled()){
187
-		    $content =  $viewCacheInstance
188
-			    ->set(route_uri(), $content)
189
-			    ->get(route_uri());
190
-	    }
186
+        if ($viewCacheInstance->isEnabled()){
187
+            $content =  $viewCacheInstance
188
+                ->set(route_uri(), $content)
189
+                ->get(route_uri());
190
+        }
191 191
 
192 192
         return $content;
193 193
     }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -175,7 +175,7 @@  discard block
 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
 
@@ -183,8 +183,8 @@  discard block
 block discarded – undo
183 183
 
184 184
 	    $viewCacheInstance = ViewCache::getInstance();
185 185
 
186
-	    if ($viewCacheInstance->isEnabled()){
187
-		    $content =  $viewCacheInstance
186
+	    if ($viewCacheInstance->isEnabled()) {
187
+		    $content = $viewCacheInstance
188 188
 			    ->set(route_uri(), $content)
189 189
 			    ->get(route_uri());
190 190
 	    }
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
-
65
-		$viewCacheInstance = ViewCache::getInstance();
66
-
67
-		if ($viewCacheInstance->isEnabled() &&
68
-			$viewCacheInstance->exists(route_uri())
69
-		){
70
-			$content = $viewCacheInstance->get(route_uri());
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
+
65
+        $viewCacheInstance = ViewCache::getInstance();
66
+
67
+        if ($viewCacheInstance->isEnabled() &&
68
+            $viewCacheInstance->exists(route_uri())
69
+        ){
70
+            $content = $viewCacheInstance->get(route_uri());
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
 
67 67
 		if ($viewCacheInstance->isEnabled() &&
68 68
 			$viewCacheInstance->exists(route_uri())
69
-		){
69
+		) {
70 70
 			$content = $viewCacheInstance->get(route_uri());
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 = $viewCacheInstance->get(route_uri());
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.
src/Router/Router.php 2 patches
Indentation   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -118,15 +118,15 @@
 block discarded – undo
118 118
             throw RouteException::incorrectMethod($this->request->getMethod());
119 119
         }
120 120
 
121
-	    if (!empty($currentRoute['cache_settings']) && key_exists('shouldCache', $currentRoute)){
122
-		    $viewCacheInstance = ViewCache::getInstance();
121
+        if (!empty($currentRoute['cache_settings']) && key_exists('shouldCache', $currentRoute)){
122
+            $viewCacheInstance = ViewCache::getInstance();
123 123
 
124
-			$viewCacheInstance->enableCaching($currentRoute['cache_settings']['shouldCache']);
124
+            $viewCacheInstance->enableCaching($currentRoute['cache_settings']['shouldCache']);
125 125
 
126
-			if (!empty($currentRoute['cache_settings']['ttl'])){
127
-				$viewCacheInstance->setTtl($currentRoute['cache_settings']['ttl']);
128
-			}
129
-	    }
126
+            if (!empty($currentRoute['cache_settings']['ttl'])){
127
+                $viewCacheInstance->setTtl($currentRoute['cache_settings']['ttl']);
128
+            }
129
+        }
130 130
 
131 131
         self::setCurrentRoute($currentRoute);
132 132
 
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -103,7 +103,7 @@  discard block
 block discarded – undo
103 103
         $this->matchedRoutes = $this->findMatches($uri);
104 104
 
105 105
         if (empty($this->matchedRoutes)) {
106
-            stop(function () {
106
+            stop(function() {
107 107
                 $this->handleNotFound();
108 108
             });
109 109
         }
@@ -118,12 +118,12 @@  discard block
 block discarded – undo
118 118
             throw RouteException::incorrectMethod($this->request->getMethod());
119 119
         }
120 120
 
121
-	    if (!empty($currentRoute['cache_settings']) && key_exists('shouldCache', $currentRoute)){
121
+	    if (!empty($currentRoute['cache_settings']) && key_exists('shouldCache', $currentRoute)) {
122 122
 		    $viewCacheInstance = ViewCache::getInstance();
123 123
 
124 124
 			$viewCacheInstance->enableCaching($currentRoute['cache_settings']['shouldCache']);
125 125
 
126
-			if (!empty($currentRoute['cache_settings']['ttl'])){
126
+			if (!empty($currentRoute['cache_settings']['ttl'])) {
127 127
 				$viewCacheInstance->setTtl($currentRoute['cache_settings']['ttl']);
128 128
 			}
129 129
 	    }
@@ -214,7 +214,7 @@  discard block
 block discarded – undo
214 214
         $routePattern = '(\/)?';
215 215
         $routeParams = [];
216 216
 
217
-        $lastIndex = (int)array_key_last($routeSegments);
217
+        $lastIndex = (int) array_key_last($routeSegments);
218 218
 
219 219
         foreach ($routeSegments as $index => $segment) {
220 220
             $segmentParam = $this->getSegmentParam($segment, $index, $lastIndex);
Please login to merge, or discard this patch.
src/Router/Route.php 2 patches
Indentation   +280 added lines, -280 removed lines patch added patch discarded remove patch
@@ -30,284 +30,284 @@
 block discarded – undo
30 30
 class Route
31 31
 {
32 32
 
33
-	/**
34
-	 * Current module name
35
-	 * @var string
36
-	 */
37
-	private $moduleName;
38
-
39
-	/**
40
-	 * Module options
41
-	 * @var array
42
-	 */
43
-	private $moduleOptions = [];
44
-
45
-	/**
46
-	 * Identifies the group middleware
47
-	 * @var bool
48
-	 */
49
-	private $isGroupMiddlewares;
50
-
51
-	/**
52
-	 * Identifies the group
53
-	 * @var boolean
54
-	 */
55
-	private $isGroup = false;
56
-
57
-	/**
58
-	 * Current group name
59
-	 * @var string
60
-	 */
61
-	private $currentGroupName = null;
62
-
63
-	/**
64
-	 * Current route
65
-	 * @var array
66
-	 */
67
-	private $currentRoute = [];
68
-
69
-	/**
70
-	 * Virtual routes
71
-	 * @var array
72
-	 */
73
-	private $virtualRoutes = [];
74
-
75
-	/**
76
-	 * @param array $module
77
-	 */
78
-	public function __construct(array $module)
79
-	{
80
-		$this->virtualRoutes['*'] = [];
81
-		$this->moduleName = key($module);
82
-		$this->moduleOptions = $module[key($module)];
83
-	}
84
-
85
-	/**
86
-	 * @param string $route
87
-	 * @param string $method
88
-	 * @param ...$params
89
-	 * @return $this
90
-	 */
91
-	public function add(string $route, string $method, ...$params): Route
92
-	{
93
-		$this->currentRoute = [
94
-			'route' => !empty($this->moduleOptions['prefix']) ? $this->moduleOptions['prefix'] . '/' . $route : $route,
95
-			'prefix' => $this->moduleOptions['prefix'],
96
-			'method' => $method,
97
-			'module' => $this->moduleName
98
-		];
99
-
100
-		if (key_exists('cacheable', $this->moduleOptions)){
101
-			$enabled = true;
102
-			if (!$this->moduleOptions['cacheable']){
103
-				$enabled = false;
104
-			}
105
-
106
-			$this->currentRoute['cache_settings']['shouldCache'] = $enabled;
107
-		}
108
-
109
-		if (is_callable($params[0])) {
110
-			$this->currentRoute['callback'] = $params[0];
111
-		} else {
112
-			$this->currentRoute['controller'] = $params[0];
113
-			$this->currentRoute['action'] = $params[1];
114
-		}
115
-
116
-		if ($this->currentGroupName) {
117
-			$this->currentRoute['group'] = $this->currentGroupName;
118
-			$this->virtualRoutes[$this->currentGroupName][] = $this->currentRoute;
119
-		} else {
120
-			$this->isGroup = false;
121
-			$this->isGroupMiddlewares = false;
122
-			$this->virtualRoutes['*'][] = $this->currentRoute;
123
-		}
124
-
125
-		return $this;
126
-	}
127
-
128
-	/**
129
-	 * @param string $route
130
-	 * @param ...$params
131
-	 * @return $this
132
-	 */
133
-	public function get(string $route, ...$params): Route
134
-	{
135
-		return $this->add($route, 'GET', ...$params);
136
-	}
137
-
138
-	/**
139
-	 * @param string $route
140
-	 * @param ...$params
141
-	 * @return $this
142
-	 */
143
-	public function post(string $route, ...$params): Route
144
-	{
145
-		return $this->add($route, 'POST', ...$params);
146
-	}
147
-
148
-	/**
149
-	 * Starts a named group of routes
150
-	 * @param string $groupName
151
-	 * @param Closure $callback
152
-	 * @return Route
153
-	 */
154
-	public function group(string $groupName, Closure $callback): Route
155
-	{
156
-		$this->currentGroupName = $groupName;
157
-
158
-		$this->isGroup = true;
159
-		$this->isGroupMiddlewares = false;
160
-		$callback($this);
161
-		$this->isGroupMiddlewares = true;
162
-		$this->currentGroupName = null;
163
-
164
-		return $this;
165
-	}
166
-
167
-	/**
168
-	 * Adds middlewares to routes and route groups
169
-	 * @param array $middlewares
170
-	 * @return Route
171
-	 */
172
-	public function middlewares(array $middlewares = []): Route
173
-	{
174
-		if (!$this->isGroup) {
175
-			end($this->virtualRoutes['*']);
176
-			$lastKey = key($this->virtualRoutes['*']);
177
-			$this->assignMiddlewaresToRoute($this->virtualRoutes['*'][$lastKey], $middlewares);
178
-			return $this;
179
-		}
180
-
181
-		end($this->virtualRoutes);
182
-		$lastKeyOfFirstRound = key($this->virtualRoutes);
183
-
184
-		if (!$this->isGroupMiddlewares) {
185
-			end($this->virtualRoutes[$lastKeyOfFirstRound]);
186
-			$lastKeyOfSecondRound = key($this->virtualRoutes[$lastKeyOfFirstRound]);
187
-			$this->assignMiddlewaresToRoute($this->virtualRoutes[$lastKeyOfFirstRound][$lastKeyOfSecondRound], $middlewares);
188
-			return $this;
189
-		}
190
-
191
-		foreach ($this->virtualRoutes[$lastKeyOfFirstRound] as &$route) {
192
-			$this->assignMiddlewaresToRoute($route, $middlewares);
193
-		}
194
-
195
-		return $this;
196
-	}
197
-
198
-	/**
199
-	 * @param bool $shouldCache
200
-	 * @param int|null $ttl
201
-	 * @return $this
202
-	 * @throws ConfigException
203
-	 * @throws DatabaseException
204
-	 * @throws DiException
205
-	 * @throws LangException
206
-	 * @throws ReflectionException
207
-	 * @throws SessionException
208
-	 */
209
-	public function cacheable(bool $shouldCache, int $ttl = null): Route
210
-	{
211
-		if (empty(session()->getId())){
212
-			return $this;
213
-		}
214
-
215
-		if (!$this->isGroup){
216
-			end($this->virtualRoutes['*']);
217
-			$lastKey = key($this->virtualRoutes['*']);
218
-
219
-			$this->virtualRoutes['*'][$lastKey]['cache_settings']['shouldCache'] = $shouldCache;
220
-
221
-			if ($ttl) {
222
-				$this->virtualRoutes['*'][$lastKey]['cache_settings']['ttl'] = $ttl;
223
-			}
224
-
225
-			return $this;
226
-		}
227
-
228
-		end($this->virtualRoutes);
229
-		$lastKeyOfFirstRound = key($this->virtualRoutes);
230
-
231
-		foreach ($this->virtualRoutes[$lastKeyOfFirstRound] as &$route) {
232
-			$route['cache_settings']['shouldCache'] = $shouldCache;
233
-			if ($ttl) {
234
-				$route['cache_settings']['ttl'] = $ttl;
235
-			}
236
-		}
237
-
238
-		return $this;
239
-	}
240
-
241
-	/**
242
-	 * Sets a unique name for a route
243
-	 * @param string $name
244
-	 * @return Route
245
-	 * @throws RouteException
246
-	 */
247
-	public function name(string $name): Route
248
-	{
249
-		if (empty($this->currentRoute)) {
250
-			throw RouteException::nameBeforeDefinition();
251
-		}
252
-
253
-		if ($this->isGroupMiddlewares) {
254
-			throw RouteException::nameOnGroup();
255
-		}
256
-
257
-		foreach ($this->virtualRoutes as &$virtualRoute) {
258
-			foreach ($virtualRoute as &$route) {
259
-				if (isset($route['name']) && $route['name'] == $name) {
260
-					throw RouteException::nonUniqueName();
261
-				}
262
-
263
-				if ($route['route'] == $this->currentRoute['route']) {
264
-					$route['name'] = $name;
265
-				}
266
-			}
267
-		}
268
-
269
-		return $this;
270
-	}
271
-
272
-	/**
273
-	 * Gets the run-time routes
274
-	 * @return array
275
-	 */
276
-	public function getRuntimeRoutes(): array
277
-	{
278
-		$runtimeRoutes = [];
279
-		foreach ($this->virtualRoutes as $virtualRoute) {
280
-			foreach ($virtualRoute as $route) {
281
-				$runtimeRoutes[] = $route;
282
-			}
283
-		}
284
-		return $runtimeRoutes;
285
-	}
286
-
287
-	/**
288
-	 * Gets the virtual routes
289
-	 * @return array
290
-	 */
291
-	public function getVirtualRoutes(): array
292
-	{
293
-		return $this->virtualRoutes;
294
-	}
295
-
296
-	/**
297
-	 * Assigns middlewares to the route
298
-	 * @param array $route
299
-	 * @param array $middlewares
300
-	 */
301
-	private function assignMiddlewaresToRoute(array &$route, array $middlewares)
302
-	{
303
-		if (!key_exists('middlewares', $route)) {
304
-			$route['middlewares'] = $middlewares;
305
-		} else {
306
-			$middlewares = array_reverse($middlewares);
307
-
308
-			foreach ($middlewares as $middleware) {
309
-				array_unshift($route['middlewares'], $middleware);
310
-			}
311
-		}
312
-	}
33
+    /**
34
+     * Current module name
35
+     * @var string
36
+     */
37
+    private $moduleName;
38
+
39
+    /**
40
+     * Module options
41
+     * @var array
42
+     */
43
+    private $moduleOptions = [];
44
+
45
+    /**
46
+     * Identifies the group middleware
47
+     * @var bool
48
+     */
49
+    private $isGroupMiddlewares;
50
+
51
+    /**
52
+     * Identifies the group
53
+     * @var boolean
54
+     */
55
+    private $isGroup = false;
56
+
57
+    /**
58
+     * Current group name
59
+     * @var string
60
+     */
61
+    private $currentGroupName = null;
62
+
63
+    /**
64
+     * Current route
65
+     * @var array
66
+     */
67
+    private $currentRoute = [];
68
+
69
+    /**
70
+     * Virtual routes
71
+     * @var array
72
+     */
73
+    private $virtualRoutes = [];
74
+
75
+    /**
76
+     * @param array $module
77
+     */
78
+    public function __construct(array $module)
79
+    {
80
+        $this->virtualRoutes['*'] = [];
81
+        $this->moduleName = key($module);
82
+        $this->moduleOptions = $module[key($module)];
83
+    }
84
+
85
+    /**
86
+     * @param string $route
87
+     * @param string $method
88
+     * @param ...$params
89
+     * @return $this
90
+     */
91
+    public function add(string $route, string $method, ...$params): Route
92
+    {
93
+        $this->currentRoute = [
94
+            'route' => !empty($this->moduleOptions['prefix']) ? $this->moduleOptions['prefix'] . '/' . $route : $route,
95
+            'prefix' => $this->moduleOptions['prefix'],
96
+            'method' => $method,
97
+            'module' => $this->moduleName
98
+        ];
99
+
100
+        if (key_exists('cacheable', $this->moduleOptions)){
101
+            $enabled = true;
102
+            if (!$this->moduleOptions['cacheable']){
103
+                $enabled = false;
104
+            }
105
+
106
+            $this->currentRoute['cache_settings']['shouldCache'] = $enabled;
107
+        }
108
+
109
+        if (is_callable($params[0])) {
110
+            $this->currentRoute['callback'] = $params[0];
111
+        } else {
112
+            $this->currentRoute['controller'] = $params[0];
113
+            $this->currentRoute['action'] = $params[1];
114
+        }
115
+
116
+        if ($this->currentGroupName) {
117
+            $this->currentRoute['group'] = $this->currentGroupName;
118
+            $this->virtualRoutes[$this->currentGroupName][] = $this->currentRoute;
119
+        } else {
120
+            $this->isGroup = false;
121
+            $this->isGroupMiddlewares = false;
122
+            $this->virtualRoutes['*'][] = $this->currentRoute;
123
+        }
124
+
125
+        return $this;
126
+    }
127
+
128
+    /**
129
+     * @param string $route
130
+     * @param ...$params
131
+     * @return $this
132
+     */
133
+    public function get(string $route, ...$params): Route
134
+    {
135
+        return $this->add($route, 'GET', ...$params);
136
+    }
137
+
138
+    /**
139
+     * @param string $route
140
+     * @param ...$params
141
+     * @return $this
142
+     */
143
+    public function post(string $route, ...$params): Route
144
+    {
145
+        return $this->add($route, 'POST', ...$params);
146
+    }
147
+
148
+    /**
149
+     * Starts a named group of routes
150
+     * @param string $groupName
151
+     * @param Closure $callback
152
+     * @return Route
153
+     */
154
+    public function group(string $groupName, Closure $callback): Route
155
+    {
156
+        $this->currentGroupName = $groupName;
157
+
158
+        $this->isGroup = true;
159
+        $this->isGroupMiddlewares = false;
160
+        $callback($this);
161
+        $this->isGroupMiddlewares = true;
162
+        $this->currentGroupName = null;
163
+
164
+        return $this;
165
+    }
166
+
167
+    /**
168
+     * Adds middlewares to routes and route groups
169
+     * @param array $middlewares
170
+     * @return Route
171
+     */
172
+    public function middlewares(array $middlewares = []): Route
173
+    {
174
+        if (!$this->isGroup) {
175
+            end($this->virtualRoutes['*']);
176
+            $lastKey = key($this->virtualRoutes['*']);
177
+            $this->assignMiddlewaresToRoute($this->virtualRoutes['*'][$lastKey], $middlewares);
178
+            return $this;
179
+        }
180
+
181
+        end($this->virtualRoutes);
182
+        $lastKeyOfFirstRound = key($this->virtualRoutes);
183
+
184
+        if (!$this->isGroupMiddlewares) {
185
+            end($this->virtualRoutes[$lastKeyOfFirstRound]);
186
+            $lastKeyOfSecondRound = key($this->virtualRoutes[$lastKeyOfFirstRound]);
187
+            $this->assignMiddlewaresToRoute($this->virtualRoutes[$lastKeyOfFirstRound][$lastKeyOfSecondRound], $middlewares);
188
+            return $this;
189
+        }
190
+
191
+        foreach ($this->virtualRoutes[$lastKeyOfFirstRound] as &$route) {
192
+            $this->assignMiddlewaresToRoute($route, $middlewares);
193
+        }
194
+
195
+        return $this;
196
+    }
197
+
198
+    /**
199
+     * @param bool $shouldCache
200
+     * @param int|null $ttl
201
+     * @return $this
202
+     * @throws ConfigException
203
+     * @throws DatabaseException
204
+     * @throws DiException
205
+     * @throws LangException
206
+     * @throws ReflectionException
207
+     * @throws SessionException
208
+     */
209
+    public function cacheable(bool $shouldCache, int $ttl = null): Route
210
+    {
211
+        if (empty(session()->getId())){
212
+            return $this;
213
+        }
214
+
215
+        if (!$this->isGroup){
216
+            end($this->virtualRoutes['*']);
217
+            $lastKey = key($this->virtualRoutes['*']);
218
+
219
+            $this->virtualRoutes['*'][$lastKey]['cache_settings']['shouldCache'] = $shouldCache;
220
+
221
+            if ($ttl) {
222
+                $this->virtualRoutes['*'][$lastKey]['cache_settings']['ttl'] = $ttl;
223
+            }
224
+
225
+            return $this;
226
+        }
227
+
228
+        end($this->virtualRoutes);
229
+        $lastKeyOfFirstRound = key($this->virtualRoutes);
230
+
231
+        foreach ($this->virtualRoutes[$lastKeyOfFirstRound] as &$route) {
232
+            $route['cache_settings']['shouldCache'] = $shouldCache;
233
+            if ($ttl) {
234
+                $route['cache_settings']['ttl'] = $ttl;
235
+            }
236
+        }
237
+
238
+        return $this;
239
+    }
240
+
241
+    /**
242
+     * Sets a unique name for a route
243
+     * @param string $name
244
+     * @return Route
245
+     * @throws RouteException
246
+     */
247
+    public function name(string $name): Route
248
+    {
249
+        if (empty($this->currentRoute)) {
250
+            throw RouteException::nameBeforeDefinition();
251
+        }
252
+
253
+        if ($this->isGroupMiddlewares) {
254
+            throw RouteException::nameOnGroup();
255
+        }
256
+
257
+        foreach ($this->virtualRoutes as &$virtualRoute) {
258
+            foreach ($virtualRoute as &$route) {
259
+                if (isset($route['name']) && $route['name'] == $name) {
260
+                    throw RouteException::nonUniqueName();
261
+                }
262
+
263
+                if ($route['route'] == $this->currentRoute['route']) {
264
+                    $route['name'] = $name;
265
+                }
266
+            }
267
+        }
268
+
269
+        return $this;
270
+    }
271
+
272
+    /**
273
+     * Gets the run-time routes
274
+     * @return array
275
+     */
276
+    public function getRuntimeRoutes(): array
277
+    {
278
+        $runtimeRoutes = [];
279
+        foreach ($this->virtualRoutes as $virtualRoute) {
280
+            foreach ($virtualRoute as $route) {
281
+                $runtimeRoutes[] = $route;
282
+            }
283
+        }
284
+        return $runtimeRoutes;
285
+    }
286
+
287
+    /**
288
+     * Gets the virtual routes
289
+     * @return array
290
+     */
291
+    public function getVirtualRoutes(): array
292
+    {
293
+        return $this->virtualRoutes;
294
+    }
295
+
296
+    /**
297
+     * Assigns middlewares to the route
298
+     * @param array $route
299
+     * @param array $middlewares
300
+     */
301
+    private function assignMiddlewaresToRoute(array &$route, array $middlewares)
302
+    {
303
+        if (!key_exists('middlewares', $route)) {
304
+            $route['middlewares'] = $middlewares;
305
+        } else {
306
+            $middlewares = array_reverse($middlewares);
307
+
308
+            foreach ($middlewares as $middleware) {
309
+                array_unshift($route['middlewares'], $middleware);
310
+            }
311
+        }
312
+    }
313 313
 }
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -97,9 +97,9 @@  discard block
 block discarded – undo
97 97
 			'module' => $this->moduleName
98 98
 		];
99 99
 
100
-		if (key_exists('cacheable', $this->moduleOptions)){
100
+		if (key_exists('cacheable', $this->moduleOptions)) {
101 101
 			$enabled = true;
102
-			if (!$this->moduleOptions['cacheable']){
102
+			if (!$this->moduleOptions['cacheable']) {
103 103
 				$enabled = false;
104 104
 			}
105 105
 
@@ -208,11 +208,11 @@  discard block
 block discarded – undo
208 208
 	 */
209 209
 	public function cacheable(bool $shouldCache, int $ttl = null): Route
210 210
 	{
211
-		if (empty(session()->getId())){
211
+		if (empty(session()->getId())) {
212 212
 			return $this;
213 213
 		}
214 214
 
215
-		if (!$this->isGroup){
215
+		if (!$this->isGroup) {
216 216
 			end($this->virtualRoutes['*']);
217 217
 			$lastKey = key($this->virtualRoutes['*']);
218 218
 
Please login to merge, or discard this patch.