Passed
Pull Request — master (#178)
by
unknown
03:26
created

ResourceCacheClearCommand::initModule()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 12
rs 10
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.8.0
13
 */
14
15
namespace Quantum\Console\Commands;
16
17
use Quantum\Libraries\Storage\FileSystem;
18
use Quantum\Exceptions\ConfigException;
19
use Quantum\Exceptions\DiException;
20
use Quantum\Console\QtCommand;
21
use Quantum\Loader\Setup;
22
use ReflectionException;
23
use Quantum\Di\Di;
24
use Exception;
25
26
/**
27
 * Class EnvCommand
28
 * @package Quantum\Console\Commands
29
 */
30
class ResourceCacheClearCommand extends QtCommand
31
{
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 = 'Clearing resource caches';
44
45
	/**
46
	 * Command help text
47
	 * @var string
48
	 */
49
	protected $help = 'The command will clear the resource caches.';
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
		$this->fs = Di::get(FileSystem::class);
99
100
		try {
101
			$this->importConfig();
102
			$this->initModule();
103
			$this->initType();
104
		}catch (Exception $e){
105
			$this->error($e->getMessage());
106
			return;
107
		}
108
109
110
		if ($this->fs->isDirectory($this->cacheDir)) {
111
			if ($this->module || $this->type) {
112
				$this->clearResourceFiles($this->cacheDir, $this->module, $this->type);
113
			} elseif (!empty($this->getOption('all'))) {
114
				$this->removeFilesInDirectory($this->cacheDir);
115
			} else {
116
				$this->error('You must set at least one of these options {--all, --module=moduleName, --type=typeName}!');
117
				return;
118
			}
119
		} else {
120
			$this->error('Cache directory does not exist or is not accessible.');
121
			return;
122
		}
123
124
		$this->info('Resource cache cleared successfully.');
125
	}
126
127
	/**
128
	 * @return void
129
	 */
130
	private function importModules()
131
	{
132
		try {
133
			if (!config()->has('modules')) {
134
				config()->import(new Setup('config', 'modules'));
135
			}
136
			$this->modules = array_keys(array_change_key_case(config()->get('modules.modules')));
0 ignored issues
show
Bug introduced by
It seems like config()->get('modules.modules') can also be of type null; however, parameter $array of array_change_key_case() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

136
			$this->modules = array_keys(array_change_key_case(/** @scrutinizer ignore-type */ config()->get('modules.modules')));
Loading history...
137
		} catch (ConfigException|DiException|ReflectionException $e) {
138
			$this->error($e->getMessage());
139
			return;
140
		}
141
	}
142
143
	/**
144
	 * @return void
145
	 * @throws ConfigException
146
	 * @throws DiException
147
	 * @throws ReflectionException
148
	 */
149
	private function importConfig(): void
150
	{
151
		if (!config()->has('view_cache')) {
152
			config()->import(new Setup('config', 'view_cache'));
153
		}
154
155
		$this->cacheDir = base_dir() . DS . config()->get('view_cache.cache_dir', 'cache');
156
	}
157
158
	/**
159
	 * @return void
160
	 * @throws Exception
161
	 */
162
	private function initModule(): void
163
	{
164
		$moduleOption = $this->getOption('module');
165
166
		if (!empty($moduleOption)) {
167
			$this->importModules();
168
			$module = strtolower($moduleOption);
169
170
			if (in_array($module, $this->modules)) {
171
				$this->module = $module;
172
			} else {
173
				throw new Exception('Module {'. $module .'} does not exist.');
174
			}
175
		}
176
	}
177
178
	/**
179
	 * @return void
180
	 * @throws Exception
181
	 */
182
	private function initType(): void
183
	{
184
		$typeOption = $this->getOption('type');
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 $dir
199
	 * @param string|null $moduleName
200
	 * @param string|null $type
201
	 * @return void
202
	 */
203
	private function clearResourceFiles(string $dir, ?string $moduleName = null, ?string $type = null): void
204
	{
205
		if ($type) {
206
			$dir = $dir . DS . strtolower($type);
207
		}
208
209
		if ($moduleName) {
210
			if (!$type) {
211
				$dir = $dir . DS . '*';
212
			}
213
			$dir = $dir . DS . strtolower($moduleName);
214
		}
215
216
		$this->removeFilesInDirectory($dir);
217
	}
218
219
	/**
220
	 * @param string $dir
221
	 * @return void
222
	 */
223
	private function removeFilesInDirectory(string $dir): void
224
	{
225
		$folders = $this->fs->glob($dir);
226
		$files = $this->fs->glob($dir . DS . '*');
227
228
		foreach ($files as $file) {
229
			if ($this->fs->isDirectory($file)) {
230
				$this->removeFilesInDirectory($file);
231
			} else {
232
				$this->fs->remove($file);
233
			}
234
		}
235
236
		foreach ($folders as $folder) {
237
			if (count($this->fs->glob($folder . DS . '*')) === 0 &&
238
				basename($dir) !== config()->get('view_cache.cache_dir', 'cache')
239
			) {
240
				$this->fs->removeDirectory($folder);
241
			}
242
		}
243
	}
244
}
245