2021-05-04 17:55:29 +02:00
|
|
|
#!/usr/bin/env php
|
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
use rabe\Util\Settings;
|
|
|
|
|
use rabe\Util\SettingsWriter;
|
|
|
|
|
|
|
|
|
|
use pointybeard\Helpers\Cli\Input;
|
|
|
|
|
use pointybeard\Helpers\Cli\Input\AbstractInputType;
|
|
|
|
|
use pointybeard\Helpers\Cli\Input\AbstractInputHandler;
|
|
|
|
|
use pointybeard\Helpers\Cli\Colour\Colour;
|
|
|
|
|
use pointybeard\Helpers\Functions\Cli;
|
|
|
|
|
|
|
|
|
|
$autoloadFiles = [
|
|
|
|
|
__DIR__ . '/../vendor/autoload.php',
|
|
|
|
|
__DIR__ . '/../../../autoload.php'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
foreach ($autoloadFiles as $autoloadFile) {
|
|
|
|
|
if (file_exists($autoloadFile)) {
|
|
|
|
|
require_once $autoloadFile;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 11:28:58 +01:00
|
|
|
function readJsonInputFile(string $path): array
|
|
|
|
|
{
|
|
|
|
|
if (!is_readable($path)) {
|
|
|
|
|
throw new \RuntimeException("Input file is not readable: $path");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$content = file_get_contents($path);
|
|
|
|
|
if ($content === false) {
|
|
|
|
|
throw new \RuntimeException("Can not read input file: $path");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$data = json_decode($content, true, 512, JSON_THROW_ON_ERROR);
|
|
|
|
|
} catch (\JsonException $ex) {
|
|
|
|
|
throw new \RuntimeException(sprintf(
|
|
|
|
|
'Input JSON is invalid (%s): %s',
|
|
|
|
|
(string)$ex->getCode(),
|
|
|
|
|
$ex->getMessage()
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!is_array($data)) {
|
|
|
|
|
throw new \RuntimeException('Input JSON must decode to an object/array');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Pre-parse siteDir so it can be passed after positional args.
|
2026-03-25 16:39:28 +01:00
|
|
|
// The input helper library does not handle this case reliably.
|
2026-03-26 11:28:58 +01:00
|
|
|
$preparsedSiteDir = null;
|
2026-03-25 16:39:28 +01:00
|
|
|
$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];
|
|
|
|
|
|
2026-03-26 11:28:58 +01:00
|
|
|
if ($arg === '--siteDir') {
|
|
|
|
|
$preparsedSiteDir = $rawArgv[$idx + 1] ?? '';
|
2026-03-25 16:39:28 +01:00
|
|
|
if (isset($rawArgv[$idx + 1])) $idx++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2026-03-26 11:28:58 +01:00
|
|
|
if (str_starts_with($arg, '--siteDir=')) {
|
|
|
|
|
$preparsedSiteDir = substr($arg, strlen('--siteDir='));
|
2026-03-25 16:39:28 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$filteredArgv[] = $arg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$_SERVER['argv'] = $filteredArgv;
|
|
|
|
|
$GLOBALS['argv'] = $filteredArgv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$version = '0.4';
|
2021-05-04 17:55:29 +02:00
|
|
|
|
2021-05-05 08:52:34 +02:00
|
|
|
$actions = [ 'show', 'write', 'help' ];
|
2021-05-04 17:55:29 +02:00
|
|
|
$settings = ['key' => '', 'value' => ''];
|
|
|
|
|
|
|
|
|
|
/* Flags and Argument configuration {{{*/
|
|
|
|
|
$collection = (new Input\InputCollection())
|
|
|
|
|
|
|
|
|
|
->add( Input\InputTypeFactory::build('LongOption')->name('help')->short('h') // {{{
|
|
|
|
|
->flags(AbstractInputType::FLAG_OPTIONAL)
|
|
|
|
|
->description('Display help text')
|
|
|
|
|
) // }}}
|
|
|
|
|
|
|
|
|
|
->add( Input\InputTypeFactory::build('LongOption')->name('in')->short('i') // {{{
|
|
|
|
|
->flags(AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_VALUE_REQUIRED)
|
2026-03-26 11:28:58 +01:00
|
|
|
->description('Path to a JSON data file to read (for write)')
|
2021-05-04 17:55:29 +02:00
|
|
|
) // }}}
|
|
|
|
|
|
2022-11-23 08:10:40 +01:00
|
|
|
->add( Input\InputTypeFactory::build('LongOption')->name('mode')->short('m') // {{{
|
|
|
|
|
->flags(AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_VALUE_REQUIRED)
|
|
|
|
|
->description('Set a mode')
|
|
|
|
|
) // }}}
|
|
|
|
|
|
2021-05-04 17:55:29 +02:00
|
|
|
->add( Input\InputTypeFactory::build('LongOption')->name('appPath')->short('a') // {{{
|
|
|
|
|
->flags(AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_VALUE_REQUIRED)
|
|
|
|
|
->description('Path where the config/ directory for the conf files is located, defaults to the working dir')
|
|
|
|
|
) // }}}
|
|
|
|
|
|
|
|
|
|
->add( Input\InputTypeFactory::build('LongOption')->name('pkgPath')->short('p') // {{{
|
|
|
|
|
->flags(AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_VALUE_REQUIRED)
|
|
|
|
|
->description('Path where the config/ directory of the package conf files is located, defaults to the working dir')
|
|
|
|
|
) // }}}
|
|
|
|
|
|
2026-03-26 11:28:58 +01:00
|
|
|
->add( Input\InputTypeFactory::build('LongOption')->name('siteDir') // {{{
|
2026-03-25 16:39:28 +01:00
|
|
|
->flags(AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_VALUE_REQUIRED)
|
2026-03-26 11:28:58 +01:00
|
|
|
->description('Site/instance directory below config/. Accepts owner_xyz or config/owner_xyz')
|
2026-03-25 16:39:28 +01:00
|
|
|
) // }}}
|
|
|
|
|
|
2021-05-04 17:55:29 +02:00
|
|
|
->add( Input\InputTypeFactory::build('Argument')->name('action') // {{{
|
|
|
|
|
->flags(AbstractInputType::FLAG_REQUIRED)
|
|
|
|
|
->description(
|
|
|
|
|
'one of:'
|
|
|
|
|
."\n\t".'show reads configurations'
|
|
|
|
|
."\n\t".'write writes configurations'
|
|
|
|
|
)
|
|
|
|
|
->validator(new Input\Validator(
|
|
|
|
|
function (AbstractInputType $input, AbstractInputHandler $context) use ($actions)
|
|
|
|
|
{
|
|
|
|
|
$action = $context->find('action');
|
|
|
|
|
if ( !in_array($action, $actions) ) {
|
|
|
|
|
throw new \Exception("'$action' not found");
|
|
|
|
|
}
|
|
|
|
|
return $action;
|
|
|
|
|
}
|
|
|
|
|
))
|
|
|
|
|
) // }}}
|
|
|
|
|
|
|
|
|
|
->add( Input\InputTypeFactory::build('Argument')->name('prefix') // {{{
|
|
|
|
|
->flags(AbstractInputType::FLAG_REQUIRED)
|
|
|
|
|
->description(
|
|
|
|
|
'the settings prefix'
|
|
|
|
|
)
|
|
|
|
|
/* ->validator(new Input\Validator(
|
|
|
|
|
function (AbstractInputType $input, AbstractInputHandler $context) use ($actions)
|
|
|
|
|
{
|
|
|
|
|
$action = $context->find('action');
|
|
|
|
|
if ( !in_array($action, $actions) {
|
|
|
|
|
throw new \Exception("'$action' not found");
|
|
|
|
|
}
|
|
|
|
|
return $action;
|
|
|
|
|
}
|
|
|
|
|
))
|
|
|
|
|
*/
|
|
|
|
|
) // }}}
|
|
|
|
|
|
|
|
|
|
->add( Input\InputTypeFactory::build('Argument')->name('setting') // {{{
|
|
|
|
|
->flags(AbstractInputType::FLAG_OPTIONAL)
|
|
|
|
|
->description(
|
|
|
|
|
'the settings you want to work on. With action "write" you can pass the value to set as JSON after an equal sign.'
|
|
|
|
|
)
|
|
|
|
|
->validator(new Input\Validator(
|
|
|
|
|
function (AbstractInputType $input, AbstractInputHandler $context)
|
|
|
|
|
{
|
|
|
|
|
$setting = $context->find('setting');
|
|
|
|
|
$action = $context->find('action');
|
2026-03-26 11:28:58 +01:00
|
|
|
|
|
|
|
|
if ($setting === null || $setting === '') {
|
|
|
|
|
return ['key' => '', 'value' => null];
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-04 17:55:29 +02:00
|
|
|
$setting = explode('=', $setting);
|
|
|
|
|
$settings['key'] = $setting[0];
|
|
|
|
|
if ($action === 'write')
|
|
|
|
|
{
|
2026-03-26 11:28:58 +01:00
|
|
|
$value = $setting[1] ?? null;
|
|
|
|
|
if (! (isset($value) || $context->find('in'))) {
|
2021-05-04 17:55:29 +02:00
|
|
|
throw new \Exception('You need a value to write');
|
|
|
|
|
}
|
|
|
|
|
$specialValues = [ 'true', 'false', 'null' ];
|
|
|
|
|
if (! (in_array($value, $specialValues) || is_numeric($value) || strpbrk($value, '[{":') !== false )) {
|
|
|
|
|
$value = "\"$value\"";
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
$settings['value'] = json_decode($value, true, 512, JSON_THROW_ON_ERROR);
|
|
|
|
|
} catch (\JsonException $ex) {
|
|
|
|
|
throw new \Exception( sprintf(
|
|
|
|
|
'The value does not appear to be a valid JSON string. Returned: %s: %s',
|
|
|
|
|
$ex->getCode(), $ex->getMessage()
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $settings;
|
|
|
|
|
}
|
|
|
|
|
))
|
|
|
|
|
) // }}}
|
|
|
|
|
; // }}}
|
|
|
|
|
|
|
|
|
|
/* Parse Input, usage, unkown flags, Help {{{*/
|
|
|
|
|
$usage = Cli\manpage( basename(__FILE__), $version,
|
|
|
|
|
'read write settings',
|
|
|
|
|
$collection, Colour::FG_GREEN, Colour::FG_WHITE,
|
2026-03-26 11:28:58 +01:00
|
|
|
[
|
|
|
|
|
'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'
|
2026-03-25 16:39:28 +01:00
|
|
|
.PHP_EOL
|
2026-03-26 11:28:58 +01:00
|
|
|
.'cfg write extension_name \'feature_example:enabled=true\' --siteDir=owner_xyz'
|
2026-03-25 16:39:28 +01:00
|
|
|
.PHP_EOL
|
2026-03-26 11:28:58 +01:00
|
|
|
.'# writes instance config: config/owner_xyz/extension_name.conf.php'
|
2026-03-25 16:39:28 +01:00
|
|
|
.PHP_EOL
|
2026-03-26 11:28:58 +01:00
|
|
|
.'cfg write extension_name \'feature_example={"enabled":true,"timeout":30,"label":"example"}\' --siteDir=owner_xyz'
|
2026-03-25 16:39:28 +01:00
|
|
|
.PHP_EOL
|
2026-03-26 11:28:58 +01:00
|
|
|
.'# writes multiple keys from one JSON string'
|
2026-03-25 16:39:28 +01:00
|
|
|
.PHP_EOL
|
2026-03-26 11:28:58 +01:00
|
|
|
.'cfg show extension_name module:enabled --siteDir=owner_xyz'
|
2026-03-25 16:39:28 +01:00
|
|
|
.PHP_EOL
|
2026-03-26 11:28:58 +01:00
|
|
|
.'# reads merged config including config/owner_xyz/extension_name.conf.php'
|
|
|
|
|
.PHP_EOL
|
|
|
|
|
]
|
2021-05-05 08:52:34 +02:00
|
|
|
).PHP_EOL;
|
2021-05-04 17:55:29 +02:00
|
|
|
|
|
|
|
|
// Get the supplied input. Passing the collection will make the handler bind values
|
|
|
|
|
// and validate the input according to our collection
|
|
|
|
|
try {
|
|
|
|
|
$argv = Input\InputHandlerFactory::build('Argv', $collection);
|
|
|
|
|
} catch (\Exception $ex) {
|
|
|
|
|
echo $usage;
|
2021-05-05 08:52:34 +02:00
|
|
|
|
2022-11-23 11:18:42 +01:00
|
|
|
if (isset($argv[1])) {
|
|
|
|
|
if ($argv[1] == '-h' || $argv[1] == '--help' || $argv[1] == 'help') {
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
2021-05-05 08:52:34 +02:00
|
|
|
}
|
2023-09-21 09:17:26 +02:00
|
|
|
fwrite(STDERR, $ex->getMessage().PHP_EOL);
|
2021-05-04 17:55:29 +02:00
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-05 08:52:34 +02:00
|
|
|
// show help
|
|
|
|
|
if ($argv->find( 'help' ) || $argv->find('action') == 'help')
|
2021-05-04 17:55:29 +02:00
|
|
|
{
|
|
|
|
|
echo $usage;
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
|
|
|
|
//}}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$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);
|
|
|
|
|
$appPath = $argv->find('appPath');
|
|
|
|
|
if (!$appPath) $appPath = getcwd().'/';
|
2026-03-25 16:39:28 +01:00
|
|
|
$appPath = rtrim($appPath, '/').'/';
|
2021-05-04 17:55:29 +02:00
|
|
|
|
|
|
|
|
/* $it = new RecursiveDirectoryIterator($appPath);
|
|
|
|
|
|
|
|
|
|
foreach(new RecursiveIteratorIterator($it) as $file)
|
|
|
|
|
{
|
|
|
|
|
$configDir = $file->getPath();
|
|
|
|
|
if ($file->isDir() && $file->getFilename() == '.' && basename($configDir) == 'config') {
|
|
|
|
|
echo "found config dir: $configDir\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
*/
|
2022-11-23 08:10:40 +01:00
|
|
|
$mode = ($argv->find('mode') == '') ? null : $argv->find('mode');
|
|
|
|
|
$cfg = (new Settings([], $mode))->appPath($appPath)->prefix($prefix);
|
2026-03-25 16:39:28 +01:00
|
|
|
// pkgPath points to package defaults (e.g. <prefix>.default.conf.php)
|
|
|
|
|
if ($pkgPath = $argv->find('pkgPath')) $cfg->pkgPath(rtrim($pkgPath, '/').'/');
|
|
|
|
|
|
|
|
|
|
$site = null;
|
|
|
|
|
$siteFlag = 0x01;
|
2026-03-26 11:28:58 +01:00
|
|
|
$siteInput = ($preparsedSiteDir !== null) ? $preparsedSiteDir : $argv->find('siteDir');
|
|
|
|
|
if ($siteInput !== null && $siteInput !== false) {
|
|
|
|
|
$siteInput = trim((string)$siteInput);
|
|
|
|
|
|
|
|
|
|
// 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/'));
|
2026-03-25 16:39:28 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-26 11:28:58 +01:00
|
|
|
$siteInput = trim($siteInput, '/');
|
|
|
|
|
if ($siteInput === '') {
|
|
|
|
|
fwrite(STDERR, 'Option --siteDir 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 --siteDir: '$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 --siteDir.".PHP_EOL);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$site = $siteInput;
|
|
|
|
|
// Reuse util-settings site resolution: config/<site>/<prefix>.conf.php
|
|
|
|
|
$cfg->site($site);
|
2026-03-25 16:39:28 +01:00
|
|
|
}
|
2021-05-05 08:52:34 +02:00
|
|
|
|
2022-11-23 08:10:40 +01:00
|
|
|
try {
|
|
|
|
|
if (is_readable($cfg->buildFileName('default'))) {
|
|
|
|
|
$cfg->load();
|
|
|
|
|
}
|
|
|
|
|
elseif (is_readable($cfgFile = $cfg->buildFileName())) {
|
|
|
|
|
$cfg->load(require($cfgFile));
|
|
|
|
|
}
|
2026-03-26 09:31:07 +01:00
|
|
|
} catch (\Throwable $e) {
|
2023-09-21 09:17:26 +02:00
|
|
|
fwrite(STDERR, "Error: ".$e->getMessage().PHP_EOL);
|
2022-11-23 08:10:40 +01:00
|
|
|
exit(1);
|
2021-05-05 08:52:34 +02:00
|
|
|
}
|
2021-05-04 17:55:29 +02:00
|
|
|
//var_dump($cfg);
|
|
|
|
|
|
|
|
|
|
$result = $cfg;
|
|
|
|
|
$settings = $argv->find('setting') ?? $settings;
|
|
|
|
|
|
|
|
|
|
if ($settings['key'])
|
|
|
|
|
{
|
|
|
|
|
foreach (explode(':', $settings['key']) as $setting)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$result = $result->{$setting};
|
|
|
|
|
}
|
|
|
|
|
catch (\OutOfRangeException $e) {
|
2023-09-21 09:17:26 +02:00
|
|
|
fwrite(STDERR, $e->getMessage().PHP_EOL);
|
2021-05-04 17:55:29 +02:00
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else $result = $cfg;
|
|
|
|
|
|
|
|
|
|
if ($result instanceof Settings) $result = $result->toArray();
|
|
|
|
|
|
|
|
|
|
switch ($argv->find('action'))
|
|
|
|
|
{
|
|
|
|
|
case 'show':
|
2022-11-07 12:07:59 +01:00
|
|
|
$out = (is_string($result)) ? $result : json_encode($result, JSON_PRETTY_PRINT);
|
|
|
|
|
echo $out.PHP_EOL;
|
2021-05-04 17:55:29 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'write':
|
2026-03-26 11:28:58 +01:00
|
|
|
$inputFile = $argv->find('in');
|
|
|
|
|
// Write needs exactly one payload source:
|
|
|
|
|
// either SETTING (key=value) or --in (JSON file).
|
|
|
|
|
// This avoids ambiguous input precedence and empty writes.
|
|
|
|
|
if ($inputFile && $settings['key'] !== '') {
|
|
|
|
|
fwrite(STDERR, 'Please use either SETTING or --in, not both.'.PHP_EOL);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
if (!$inputFile && $settings['key'] === '') {
|
|
|
|
|
fwrite(STDERR, 'Nothing to write: provide SETTING or --in.'.PHP_EOL);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
2026-03-25 16:39:28 +01:00
|
|
|
|
2026-03-26 11:28:58 +01:00
|
|
|
$path = ($settings['key'] !== '') ? explode(':', $settings['key']) : [];
|
|
|
|
|
if ($inputFile) {
|
|
|
|
|
try {
|
|
|
|
|
$setting2write = readJsonInputFile($inputFile);
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
fwrite(STDERR, $e->getMessage().PHP_EOL);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$setting2write = $settings['value'];
|
|
|
|
|
}
|
2026-03-25 16:39:28 +01:00
|
|
|
|
2021-05-04 17:55:29 +02:00
|
|
|
while ( ! empty($path))
|
|
|
|
|
{
|
|
|
|
|
$setting2write = [array_pop($path) => $setting2write];
|
|
|
|
|
}
|
2026-03-25 16:39:28 +01:00
|
|
|
|
|
|
|
|
$writeType = ($site !== null) ? $siteFlag : null;
|
|
|
|
|
$file = $cfg->buildFileName($writeType);
|
|
|
|
|
if (is_readable($file))
|
2021-05-04 17:55:29 +02:00
|
|
|
{
|
|
|
|
|
$setting2write = array_replace_recursive(require($file), $setting2write);
|
2021-05-05 08:52:34 +02:00
|
|
|
copy($file, "$file.bak");
|
2021-05-04 17:55:29 +02:00
|
|
|
}
|
2026-03-25 16:39:28 +01:00
|
|
|
|
|
|
|
|
$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);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-04 17:55:29 +02:00
|
|
|
$writeCfg = $cfg->create($setting2write);
|
|
|
|
|
// var_dump($writeCfg->toArray());
|
2021-05-05 08:52:34 +02:00
|
|
|
try {
|
2026-03-25 16:39:28 +01:00
|
|
|
(new SettingsWriter($writeCfg, '', $writeType))->write();
|
2021-05-05 08:52:34 +02:00
|
|
|
echo "Written modified settings to: $file".PHP_EOL;
|
|
|
|
|
}
|
|
|
|
|
catch (\Exception $e) {
|
2026-03-25 16:39:28 +01:00
|
|
|
fwrite(STDERR, $e->getMessage().PHP_EOL);
|
|
|
|
|
exit(1);
|
2021-05-05 08:52:34 +02:00
|
|
|
}
|
2021-05-04 17:55:29 +02:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* jEdit buffer local properties {{{
|
|
|
|
|
* :folding=explicit:collapseFolds=1:
|
|
|
|
|
}}}*/
|