forked from codeigniter4/settings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClearSettings.php
More file actions
66 lines (50 loc) · 1.79 KB
/
ClearSettings.php
File metadata and controls
66 lines (50 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
namespace CodeIgniter\Settings\Commands;
use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
use CodeIgniter\Settings\Config\Settings;
class ClearSettings extends BaseCommand
{
protected $group = 'Settings';
protected $name = 'settings:clear';
protected $description = 'Clears all settings from persistent storage.';
public function run(array $params)
{
$config = config('Settings');
$handlers = $this->getHandlerNames($config);
if ($handlers === null) {
CLI::write('No handlers available to clear in the config file.');
return;
}
if (CLI::prompt('This will delete all settings from ' . $handlers . '. Are you sure you want to continue?', ['y', 'n'], 'required') !== 'y') {
return;
}
service('settings')->flush();
CLI::write('Settings cleared from ' . $handlers . '.', 'green');
}
/**
* Gets a human-readable list of handler names.
*/
private function getHandlerNames(Settings $config): ?string
{
if ($config->handlers === []) {
return null;
}
$handlerNames = [];
foreach ($config->handlers as $handler) {
// Get writeable handlers only (those that can be flushed)
if (isset($config->{$handler}['writeable']) && $config->{$handler}['writeable'] === true) {
$handlerNames[] = $handler;
}
}
if ($handlerNames === []) {
return null;
}
if (count($handlerNames) === 1) {
return $handlerNames[0] . ' handler';
}
// Multiple handlers: "database and file"
$last = array_pop($handlerNames);
return implode(', ', $handlerNames) . ' and ' . $last . ' handlers';
}
}