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

This commit is contained in:
Alejandro Sosa 2026-03-25 16:39:28 +01:00
commit 832b37e613
4 changed files with 484 additions and 15 deletions

161
bin/cfg
View file

@ -22,7 +22,62 @@ foreach ($autoloadFiles as $autoloadFile) {
}
}
$version = '0.3';
// Pre-parse site options so they can be passed after positional args.
// The input helper library does not handle this case reliably.
$preparsedSiteOptions = [];
$rawArgv = $_SERVER['argv'] ?? $GLOBALS['argv'] ?? null;
if (is_array($rawArgv) && !empty($rawArgv)) {
$filteredArgv = [$rawArgv[0]];
for ($idx = 1; $idx < count($rawArgv); $idx++) {
$arg = $rawArgv[$idx];
if ($arg === '-s' || $arg === '--site') {
$preparsedSiteOptions[] = [
'name' => 'site',
'value' => $rawArgv[$idx + 1] ?? '',
];
if (isset($rawArgv[$idx + 1])) $idx++;
continue;
}
if (str_starts_with($arg, '--site=')) {
$preparsedSiteOptions[] = [
'name' => 'site',
'value' => substr($arg, strlen('--site=')),
];
continue;
}
if ($arg === '--directory-name' || $arg === '--directoryName') {
$preparsedSiteOptions[] = [
'name' => 'directoryName',
'value' => $rawArgv[$idx + 1] ?? '',
];
if (isset($rawArgv[$idx + 1])) $idx++;
continue;
}
if (str_starts_with($arg, '--directory-name=')) {
$preparsedSiteOptions[] = [
'name' => 'directoryName',
'value' => substr($arg, strlen('--directory-name=')),
];
continue;
}
if (str_starts_with($arg, '--directoryName=')) {
$preparsedSiteOptions[] = [
'name' => 'directoryName',
'value' => substr($arg, strlen('--directoryName=')),
];
continue;
}
$filteredArgv[] = $arg;
}
$_SERVER['argv'] = $filteredArgv;
$GLOBALS['argv'] = $filteredArgv;
}
$version = '0.4';
$actions = [ 'show', 'write', 'help' ];
$settings = ['key' => '', 'value' => ''];
@ -60,6 +115,16 @@ $collection = (new Input\InputCollection())
->description('Path where the config/ directory of the package conf files is located, defaults to the working dir')
) // }}}
->add( Input\InputTypeFactory::build('LongOption')->name('site')->short('s') // {{{
->flags(AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_VALUE_REQUIRED)
->description('Site/instance directory below config/ (target: config/<site>/<prefix>.conf.php), e.g. owner_xyz')
) // }}}
->add( Input\InputTypeFactory::build('LongOption')->name('directoryName') // {{{
->flags(AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_VALUE_REQUIRED)
->description('Alias for --site. Accepts owner_xyz or config/owner_xyz (also supports --directory-name)')
) // }}}
->add( Input\InputTypeFactory::build('Argument')->name('action') // {{{
->flags(AbstractInputType::FLAG_REQUIRED)
->description(
@ -141,8 +206,18 @@ $usage = Cli\manpage( basename(__FILE__), $version,
$collection, Colour::FG_GREEN, Colour::FG_WHITE,
[
'Examples' =>
'cfg show VeruA db:host'.PHP_EOL.
'cfg write VeruA \'db:host="newHost"\''.PHP_EOL
'cfg write myCEESV \'auth:projectId="218523"\''
.PHP_EOL
.'# writes local config: config/myCEESV.conf.php'
.PHP_EOL
.'cfg write myCEESV \'auth:projectId="218523"\' --directory-name=owner_xyz'
.PHP_EOL
.'# writes instance config: config/owner_xyz/myCEESV.conf.php'
.PHP_EOL
.'cfg show myCEESV auth:projectId --site=owner_xyz'
.PHP_EOL
.'# reads merged config including config/owner_xyz/myCEESV.conf.php'
.PHP_EOL
]
).PHP_EOL;
@ -181,6 +256,7 @@ echo $argv->find('setting')['value'].PHP_EOL;
// var_dump($cfg);
$appPath = $argv->find('appPath');
if (!$appPath) $appPath = getcwd().'/';
$appPath = rtrim($appPath, '/').'/';
/* $it = new RecursiveDirectoryIterator($appPath);
@ -194,7 +270,62 @@ foreach(new RecursiveIteratorIterator($it) as $file)
*/
$mode = ($argv->find('mode') == '') ? null : $argv->find('mode');
$cfg = (new Settings([], $mode))->appPath($appPath)->prefix($prefix);
if ($pkgPath = $argv->find('pkgPath')) $cfg->pkgPath($pkgPath);
// pkgPath points to package defaults (e.g. <prefix>.default.conf.php)
if ($pkgPath = $argv->find('pkgPath')) $cfg->pkgPath(rtrim($pkgPath, '/').'/');
$site = null;
$siteFlag = 0x01;
// Only one site selector may be provided to avoid ambiguous targets.
$providedSiteOptions = [];
foreach ($preparsedSiteOptions as $option) {
$providedSiteOptions[$option['name']][] = $option['value'] ?? '';
}
foreach (['site', 'directoryName'] as $optionName) {
$parsedValue = $argv->find($optionName);
if ($parsedValue !== null && $parsedValue !== '' && $parsedValue !== false) {
$providedSiteOptions[$optionName][] = $parsedValue;
}
}
if (count($providedSiteOptions) > 1) {
fwrite(STDERR, 'Please use only one of --site or --directoryName (alias: --directory-name).'.PHP_EOL);
exit(1);
}
if (!empty($providedSiteOptions)) {
$optionName = array_key_first($providedSiteOptions);
$siteInput = trim((string)end($providedSiteOptions[$optionName]));
if ($optionName === 'directoryName') {
// Accept both "owner_xyz" and "config/owner_xyz" and normalize to site key.
if (str_starts_with($siteInput, $appPath.'config/')) {
$siteInput = substr($siteInput, strlen($appPath.'config/'));
} elseif (str_starts_with($siteInput, 'config/')) {
$siteInput = substr($siteInput, strlen('config/'));
}
}
$siteInput = trim($siteInput, '/');
if ($siteInput === '') {
fwrite(STDERR, "Option --$optionName is empty.".PHP_EOL);
exit(1);
}
// Block directory traversal and hidden-dot segments.
if (str_contains($siteInput, '..') || preg_match('~(^|/)\.(?:/|$)~', $siteInput)) {
fwrite(STDERR, "Invalid directory in --$optionName: '$siteInput'.".PHP_EOL);
exit(1);
}
// Allow only predictable path segments for instance directories.
foreach (explode('/', $siteInput) as $part) {
if ($part === '' || !preg_match('/^[A-Za-z0-9._-]+$/', $part)) {
fwrite(STDERR, "Invalid directory name segment '$part' in --$optionName.".PHP_EOL);
exit(1);
}
}
$site = $siteInput;
// Reuse util-settings site resolution: config/<site>/<prefix>.conf.php
$cfg->site($site);
}
try {
if (is_readable($cfg->buildFileName('default'))) {
@ -238,27 +369,37 @@ case 'show':
case 'write':
$path = ($settings['key'] !== '') ? explode(':', $settings['key']) : [];
var_dump($path);
$setting2write = $settings['value'];
while ( ! empty($path))
{
$setting2write = [array_pop($path) => $setting2write];
}
if (is_readable($file = $cfg->buildFileName()))
$writeType = ($site !== null) ? $siteFlag : null;
$file = $cfg->buildFileName($writeType);
if (is_readable($file))
{
$setting2write = array_replace_recursive(require($file), $setting2write);
copy($file, "$file.bak");
}
$targetDir = dirname($file);
if (!is_dir($targetDir) && !mkdir($targetDir, 0775, true) && !is_dir($targetDir)) {
fwrite(STDERR, "Can not create directory: $targetDir".PHP_EOL);
exit(1);
}
$writeCfg = $cfg->create($setting2write);
// var_dump($writeCfg->toArray());
try {
(new SettingsWriter($writeCfg))->write();
(new SettingsWriter($writeCfg, '', $writeType))->write();
echo "Written modified settings to: $file".PHP_EOL;
}
catch (\Exception $e) {
echo $e->getMessage().PHP_EOL;
fwrite(STDERR, $e->getMessage().PHP_EOL);
exit(1);
}
break;