Task: #53687 Support instance-specific cfg write targets and add coverage V2 #4

Merged
norb merged 11 commits from 53687/task-support-instance-specific-cfg-write-target-v2 into master 2026-03-30 15:09:21 +00:00
Showing only changes of commit 480d97c804 - Show all commits

Task: #53687 Add minimal --verbose mode and replace legacy var_dump debug points

Alejandro Sosa 2026-03-27 08:55:53 +01:00

79
bin/cfg
View file

@ -56,6 +56,11 @@ function readJsonInputFile(string $path): array
return $data;
}
function verboseLog(bool $enabled, string $message): void
{
if ($enabled) fwrite(STDERR, "[verbose] $message".PHP_EOL);
}
$version = '0.4';
$actions = [ 'show', 'write', 'help' ];
@ -69,6 +74,11 @@ $collection = (new Input\InputCollection())
->description('Display help text')
) // }}}
->add( Input\InputTypeFactory::build('LongOption')->name('verbose')->short('v') // {{{
->flags(AbstractInputType::FLAG_OPTIONAL)
->description('Print debug details to STDERR')
) // }}}
->add( Input\InputTypeFactory::build('LongOption')->name('in')->short('i') // {{{
->flags(AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_VALUE_REQUIRED)
->description('Path to a JSON data file to read (for write)')
@ -179,26 +189,37 @@ $usage = Cli\manpage( basename(__FILE__), $version,
$collection, Colour::FG_GREEN, Colour::FG_WHITE,
[
'Examples' =>
'cfg write extension_name \'module:enabled=true\''
.PHP_EOL
.'# writes local config: config/extension_name.conf.php'
.PHP_EOL
.'cfg write extension_name -i /tmp/extension.json --siteDir=owner_xyz'
.PHP_EOL
.'# writes multiple settings from JSON to config/owner_xyz/extension_name.conf.php'
.PHP_EOL
.'cfg write extension_name \'feature_example:enabled=true\' --siteDir=owner_xyz'
.PHP_EOL
.'# writes instance config: config/owner_xyz/extension_name.conf.php'
.PHP_EOL
.'cfg write extension_name \'feature_example={"enabled":true,"timeout":30,"label":"example"}\' --siteDir=owner_xyz'
.PHP_EOL
.'# writes multiple keys from one JSON string'
.PHP_EOL
.'cfg show extension_name module:enabled --siteDir=owner_xyz'
.PHP_EOL
.'# reads merged config including config/owner_xyz/extension_name.conf.php'
.PHP_EOL
'Basic usage:'
.PHP_EOL
.'cfg show VeruA db:host'
.PHP_EOL
.'cfg write VeruA \'db:host="newHost"\''
.PHP_EOL
.PHP_EOL
.'Advance usage:'
.PHP_EOL
.'cfg write extension_name \'module:enabled=true\''
.PHP_EOL
.PHP_EOL
.'#Instance write:'
.PHP_EOL
.'cfg write extension_name \'module:enabled=true\' --siteDir=owner_xyz'
.PHP_EOL
.PHP_EOL
.'#Batch write from JSON file:'
.PHP_EOL
.'cfg write extension_name --siteDir=owner_xyz -i /tmp/extension.json'
.PHP_EOL
.PHP_EOL
.'#Batch write from JSON string:'
.PHP_EOL
.'cfg write extension_name \'module={"enabled":true,"timeout":30,"label":"example"}\' --siteDir=owner_xyz'
.PHP_EOL
.PHP_EOL
.'#Read merged value for an instance:'
.PHP_EOL
.'cfg show extension_name module:enabled --siteDir=owner_xyz'
.PHP_EOL
]
).PHP_EOL;
@ -231,13 +252,7 @@ if ($argv->find( 'help' ) || $argv->find('action') == 'help')
$prefix = $argv->find('prefix');
/*
echo $argv->find('action').PHP_EOL;
echo ($prefix).PHP_EOL;
echo $argv->find('setting')['key'].PHP_EOL;
echo $argv->find('setting')['value'].PHP_EOL;
*/
// var_dump($cfg);
$verbose = (bool)$argv->find('verbose');
$appPath = $argv->find('appPath');
norb marked this conversation as resolved Outdated

perhaps we could have a verbose mode?

perhaps we could have a verbose mode?

@norb wrote in #4 (comment):

perhaps we could have a verbose mode?

Hi Norber, for this round I kept the verbose implementation minimal on purpose. The current CLI helper library is not fully compatible with PHP 8 in the verbosity path (especially around incrementing flags), so going further right now would either require patching vendor code or introducing larger structural changes. I agree we should improve this longer term; a good replacement would be Symfony Console (https://symfony.com/doc/current/components/console.html), which I know well and that already provides robust argument parsing, verbosity levels, and many related features. For this version, I think it is safer to keep it simple and stable, and handle a full CLI migration in a dedicated follow-up.

@norb wrote in https://code.verua.online/rabe/Util-Settings/pulls/4#issuecomment-3735: > perhaps we could have a verbose mode? Hi Norber, for this round I kept the verbose implementation minimal on purpose. The current CLI helper library is not fully compatible with PHP 8 in the verbosity path (especially around incrementing flags), so going further right now would either require patching vendor code or introducing larger structural changes. I agree we should improve this longer term; a good replacement would be Symfony Console (https://symfony.com/doc/current/components/console.html), which I know well and that already provides robust argument parsing, verbosity levels, and many related features. For this version, I think it is safer to keep it simple and stable, and handle a full CLI migration in a dedicated follow-up.
if (!$appPath) $appPath = getcwd().'/';
$appPath = rtrim($appPath, '/').'/';
@ -299,6 +314,7 @@ if ($siteInput !== null && $siteInput !== false)
$site = $siteInput;
// Reuse util-settings site resolution: config/<site>/<prefix>.conf.php
$cfg->site($site);
verboseLog($verbose, "siteDir resolved to '$site'");
}
try
@ -315,7 +331,7 @@ catch (\Throwable $e)
fwrite(STDERR, "Error: ".$e->getMessage().PHP_EOL);
exit(1);
}
//var_dump($cfg);
verboseLog($verbose, 'config bootstrap loaded');
$result = $cfg;
$settings = $argv->find('setting') ?? $settings;
@ -340,6 +356,7 @@ if ($result instanceof Settings) $result = $result->toArray();
switch ($argv->find('action'))
{
case 'show':
verboseLog($verbose, "show action for prefix '$prefix'");
$out = (is_string($result)) ? $result : json_encode($result, JSON_PRETTY_PRINT);
echo $out.PHP_EOL;
break;
@ -361,6 +378,7 @@ case 'write':
}
$path = ($settings['key'] !== '') ? explode(':', $settings['key']) : [];
verboseLog($verbose, 'write source: '.($inputFile ? "--in ($inputFile)" : 'SETTING'));
if ($inputFile)
alejandro.sosa marked this conversation as resolved Outdated

probably this one too?

probably this one too?
{
try
@ -377,6 +395,7 @@ case 'write':
{
$setting2write = $settings['value'];
}
verboseLog($verbose, 'write path: '.json_encode($path));
while ( ! empty($path))
{
@ -385,10 +404,12 @@ case 'write':
$writeType = ($site !== null) ? $siteFlag : null;
$file = $cfg->buildFileName($writeType);
verboseLog($verbose, "write target: $file");
if (is_readable($file))
{
$setting2write = array_replace_recursive(require($file), $setting2write);
copy($file, "$file.bak");
verboseLog($verbose, "existing config merged from: $file");
}
$targetDir = dirname($file);
@ -399,7 +420,7 @@ case 'write':
}
$writeCfg = $cfg->create($setting2write);
// var_dump($writeCfg->toArray());
verboseLog($verbose, 'payload prepared for writer');
try
{
(new SettingsWriter($writeCfg, '', $writeType))->write();