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

ResourceCacheClearCommand::initModule()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 13
rs 9.9666
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\Exceptions\ConfigException;
18
use Quantum\Exceptions\DiException;
19
use Quantum\Console\QtCommand;
20
use Quantum\Loader\Setup;
21
use ReflectionException;
22
23
/**
24
 * Class EnvCommand
25
 * @package Quantum\Console\Commands
26
 */
27
class ResourceCacheClearCommand extends QtCommand
28
{
29
30
	/**
31
	 * Command name
32
	 * @var string
33
	 */
34
	protected $name = 'resource_cache:clear';
35
36
	/**
37
	 * Command description
38
	 * @var string
39
	 */
40
	protected $description = 'Clearing resource caches';
41
42
	/**
43
	 * Command help text
44
	 * @var string
45
	 */
46
	protected $help = 'The command will clear the resource caches.';
47
48
	/**
49
	 * Command options
50
	 * @var array
51
	 */
52
	protected $options = [
53
		['all', 'all', 'none', ''],
54
		['type', 't', 'required', ''],
55
		['module', 'm', 'required', '']
56
	];
57
58
	/**
59
	 * @var array
60
	 */
61
	protected $types = ['views', 'asserts'];
62
63
	/**
64
	 * @var array
65
	 */
66
	protected $modules;
67
68
	/**
69
	 * @var string|null
70
	 */
71
	protected $type = null;
72
73
	/**
74
	 * @var string|null
75
	 */
76
	protected $module = null;
77
78
	/**
79
	 * @var string
80
	 */
81
	protected $cacheDir;
82
83
	/**
84
	 * @return void
85
	 */
86
	public function exec()
87
	{
88
		$this->importConfig();
89
		$this->initModule();
90
		$this->initType();
91
92
		if (is_dir($this->cacheDir)) {
93
			if ($this->module || $this->type) {
94
				$this->clearResourceFiles($this->cacheDir, $this->module, $this->type);
95
			} elseif (!empty($this->getOption('all'))) {
96
				$this->removeFilesInDirectory($this->cacheDir);
97
			}
98
		} else {
99
			$this->error('Cache directory does not exist or is not accessible.');
100
			exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
101
		}
102
103
		$this->info('Resource cache cleared successfully.');
104
	}
105
106
	/**
107
	 * @return void
108
	 */
109
	private function importModules()
110
	{
111
		try {
112
			if (!config()->has('modules')) {
113
				config()->import(new Setup('config', 'modules'));
114
			}
115
			$this->modules = array_keys(array_change_key_case(config()->get('modules')['modules']));
116
		} catch (ConfigException|DiException|ReflectionException $e) {
117
			$this->error($e->getMessage());
118
			exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
119
		}
120
	}
121
122
	/**
123
	 * @return void
124
	 */
125
	private function importConfig(): void
126
	{
127
		if (!config()->has('view_cache')) {
128
			try {
129
				config()->import(new Setup('config', 'view_cache'));
130
			} catch (\Exception $e) {
131
				$this->error('Error loading configuration for view_cache.');
132
			}
133
		}
134
135
		$this->cacheDir = base_dir() . DS . config()->get('view_cache.cache_dir', 'cache');
136
	}
137
138
	/**
139
	 * @return void
140
	 */
141
	private function initModule(): void
142
	{
143
		$moduleOption = $this->getOption('module');
144
145
		if (!empty($moduleOption)) {
146
			$this->importModules();
147
			$module = strtolower($moduleOption);
148
149
			if (in_array($module, $this->modules)) {
150
				$this->module = $module;
151
			} else {
152
				$this->error("Module '{$module}' does not exist.");
153
				exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
154
			}
155
		}
156
	}
157
158
	/**
159
	 * @return void
160
	 */
161
	private function initType(): void
162
	{
163
		$typeOption = $this->getOption('type');
164
165
		if (!empty($typeOption)) {
166
			$type = strtolower($typeOption);
167
168
			if (in_array($type, $this->types)) {
169
				$this->type = $type;
170
			} else {
171
				$this->error("Cache type '{$type}' is invalid.");
172
				exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
173
			}
174
		}
175
	}
176
177
	/**
178
	 * @param string $dir
179
	 * @param string|null $moduleName
180
	 * @param string|null $type
181
	 * @return void
182
	 */
183
	private function clearResourceFiles(string $dir, ?string $moduleName = null, ?string $type = null): void
184
	{
185
		if ($type) {
186
			$dir = $dir . DS . strtolower($type);
187
		}
188
189
		if ($moduleName) {
190
			if (!$type) {
191
				$dir = $dir . DS . '*';
192
			}
193
			$dir = $dir . DS . strtolower($moduleName);
194
		}
195
196
		$this->removeFilesInDirectory($dir);
197
	}
198
199
	/**
200
	 * @param string $dir
201
	 * @return void
202
	 */
203
	private function removeFilesInDirectory(string $dir): void
204
	{
205
		$folders = glob($dir);
206
		$files = glob($dir . '/*');
207
208
		foreach ($files as $file) {
209
			if (is_dir($file)) {
210
				$this->removeFilesInDirectory($file);
211
			} else {
212
				unlink($file);
213
			}
214
		}
215
216
		foreach ($folders as $folder) {
217
			if (count(glob($folder . '/*')) === 0 &&
218
				basename($dir) !== config()->get('view_cache.cache_dir', 'cache')
219
			) {
220
				rmdir($folder);
221
			}
222
		}
223
	}
224
}
225