Skip to content

Commit 68af8af

Browse files
committed
add support for FileHandler
1 parent 1969871 commit 68af8af

6 files changed

Lines changed: 983 additions & 4 deletions

File tree

src/Commands/ClearSettings.php

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,61 @@
77

88
class ClearSettings extends BaseCommand
99
{
10-
protected $group = 'Housekeeping';
10+
protected $group = 'Settings';
1111
protected $name = 'settings:clear';
12-
protected $description = 'Clears all settings from the database.';
12+
protected $description = 'Clears all settings from persistent storage.';
1313

1414
public function run(array $params)
1515
{
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') {
1726
return;
1827
}
1928

2029
service('settings')->flush();
2130

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';
2366
}
2467
}

src/Config/Settings.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use CodeIgniter\Config\BaseConfig;
66
use CodeIgniter\Settings\Handlers\ArrayHandler;
77
use CodeIgniter\Settings\Handlers\DatabaseHandler;
8+
use CodeIgniter\Settings\Handlers\FileHandler;
89

910
class Settings extends BaseConfig
1011
{
@@ -34,4 +35,13 @@ class Settings extends BaseConfig
3435
'group' => null,
3536
'writeable' => true,
3637
];
38+
39+
/**
40+
* File handler settings.
41+
*/
42+
public $file = [
43+
'class' => FileHandler::class,
44+
'path' => WRITEPATH . 'settings',
45+
'writeable' => true,
46+
];
3747
}

src/Handlers/ArrayHandler.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,18 @@ protected function forgetStored(string $class, string $property, ?string $contex
115115
unset($this->contexts[$context][$class][$property]);
116116
}
117117
}
118+
119+
/**
120+
* Retrieves all stored properties for a specific class and context.
121+
*
122+
* @return array<string,array> Format: ['property' => ['value', 'type']]
123+
*/
124+
protected function getAllStored(string $class, ?string $context): array
125+
{
126+
if ($context === null) {
127+
return $this->general[$class] ?? [];
128+
}
129+
130+
return $this->contexts[$context][$class] ?? [];
131+
}
118132
}

0 commit comments

Comments
 (0)