|
7 | 7 |
|
8 | 8 | class ClearSettings extends BaseCommand |
9 | 9 | { |
10 | | - protected $group = 'Housekeeping'; |
| 10 | + protected $group = 'Settings'; |
11 | 11 | protected $name = 'settings:clear'; |
12 | | - protected $description = 'Clears all settings from the database.'; |
| 12 | + protected $description = 'Clears all settings from persistent storage.'; |
13 | 13 |
|
14 | 14 | public function run(array $params) |
15 | 15 | { |
16 | | - if (CLI::prompt('This will delete all settings from the database. Are you sure you want to continue?', ['y', 'n'], 'required') !== 'y') { |
| 16 | + $config = config('Settings'); |
| 17 | + $handlers = $this->getHandlerNames($config); |
| 18 | + |
| 19 | + if ($handlers === null) { |
| 20 | + CLI::write('No handlers available to clear in the config file.'); |
| 21 | + |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + if (CLI::prompt('This will delete all settings from ' . $handlers . '. Are you sure you want to continue?', ['y', 'n'], 'required') !== 'y') { |
17 | 26 | return; |
18 | 27 | } |
19 | 28 |
|
20 | 29 | service('settings')->flush(); |
21 | 30 |
|
22 | | - CLI::write('Settings cleared from the database.', 'green'); |
| 31 | + CLI::write('Settings cleared from ' . $handlers . '.', 'green'); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Gets a human-readable list of handler names. |
| 36 | + * |
| 37 | + * @param mixed $config |
| 38 | + */ |
| 39 | + private function getHandlerNames($config): ?string |
| 40 | + { |
| 41 | + if ($config->handlers === []) { |
| 42 | + return null; |
| 43 | + } |
| 44 | + |
| 45 | + $handlerNames = []; |
| 46 | + |
| 47 | + foreach ($config->handlers as $handler) { |
| 48 | + // Get writeable handlers only (those that can be flushed) |
| 49 | + if (isset($config->{$handler}['writeable']) && $config->{$handler}['writeable'] === true) { |
| 50 | + $handlerNames[] = $handler; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + if ($handlerNames === []) { |
| 55 | + return null; |
| 56 | + } |
| 57 | + |
| 58 | + if (count($handlerNames) === 1) { |
| 59 | + return $handlerNames[0] . ' handler'; |
| 60 | + } |
| 61 | + |
| 62 | + // Multiple handlers: "database and file" |
| 63 | + $last = array_pop($handlerNames); |
| 64 | + |
| 65 | + return implode(', ', $handlerNames) . ' and ' . $last . ' handlers'; |
23 | 66 | } |
24 | 67 | } |
0 commit comments