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