Settings files are no longer required

all you need is a config/ directory under the appPath
If you do not have any settingsfiles to read, show will always report
settings not found
If you write, you can only write a new file with settings in the root
like `cfg write PREFIX
='{"key":"value","key2":{"some":"more","levels":"there"}}'

Which will give you a file `config/PREFIX.conf.php` with the contents
``` php
<?php return
[
	'key' => 'value',
	'key2' =>
	[
		'some' => 'more',
		'levels' => 'there'
	]
];
```

If you have a `default.conf.php` but now `conf.php` and modify a setting
a new conf.php file with the setting to override is written
This commit is contained in:
Norbert Wagner 2021-05-05 08:52:34 +02:00
parent ab736148c0
commit 9457987a31

31
bin/cfg
View file

@ -24,7 +24,7 @@ foreach ($autoloadFiles as $autoloadFile) {
$version = '0.1 beta';
$actions = [ 'show', 'write' ];
$actions = [ 'show', 'write', 'help' ];
$settings = ['key' => '', 'value' => ''];
/* Flags and Argument configuration {{{*/
@ -115,7 +115,6 @@ $collection = (new Input\InputCollection())
if (! (in_array($value, $specialValues) || is_numeric($value) || strpbrk($value, '[{":') !== false )) {
$value = "\"$value\"";
}
var_dump($value);
try {
$settings['value'] = json_decode($value, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $ex) {
@ -140,7 +139,7 @@ $usage = Cli\manpage( basename(__FILE__), $version,
'cfg show VeruA db:host'.PHP_EOL.
'cfg write VeruA \'db:host="newHost"\''.PHP_EOL
]
).PHP_EOL.PHP_EOL;
).PHP_EOL;
// Get the supplied input. Passing the collection will make the handler bind values
// and validate the input according to our collection
@ -148,11 +147,16 @@ try {
$argv = Input\InputHandlerFactory::build('Argv', $collection);
} catch (\Exception $ex) {
echo $usage;
if ($argv[1] == '-h' || $argv[1] == '--help' || $argv[1] == 'help') {
exit(0);
}
echo $ex->getMessage().PHP_EOL;
exit(1);
}
if ($argv->find( 'help' ) || $argc <= 2)
// show help
if ($argv->find( 'help' ) || $argv->find('action') == 'help')
{
echo $usage;
exit(0);
@ -183,7 +187,13 @@ foreach(new RecursiveIteratorIterator($it) as $file)
*/
$cfg = (new Settings())->appPath($appPath)->prefix($prefix);
if ($pkgPath = $argv->find('pkgPath')) $cfg->pkgPath($pkgPath);
if (is_readable($cfg->buildFileName('default'))) {
$cfg->load();
}
elseif (is_readable($cfgFile = $cfg->buildFileName())) {
$cfg->load(require($cfgFile));
}
//var_dump($cfg);
$result = $cfg;
@ -209,11 +219,12 @@ if ($result instanceof Settings) $result = $result->toArray();
switch ($argv->find('action'))
{
case 'show':
echo json_encode($result, JSON_PRETTY_PRINT);
echo json_encode($result, JSON_PRETTY_PRINT).PHP_EOL;
break;
case 'write':
$path = explode(':', $settings['key']);
$path = ($settings['key'] !== '') ? explode(':', $settings['key']) : [];
var_dump($path);
$setting2write = $settings['value'];
@ -224,11 +235,17 @@ case 'write':
if (is_readable($file = $cfg->buildFileName()))
{
$setting2write = array_replace_recursive(require($file), $setting2write);
copy($file, "$file.bak");
}
$writeCfg = $cfg->create($setting2write);
// var_dump($writeCfg->toArray());
copy($file, "$file.bak");
try {
(new SettingsWriter($writeCfg))->write();
echo "Written modified settings to: $file".PHP_EOL;
}
catch (\Exception $e) {
echo $e->getMessage().PHP_EOL;
}
break;
}