settings:set + settings:reset commands

This commit is contained in:
slawkens 2024-02-04 09:01:00 +01:00
parent 235e69b8da
commit e2487f97e3
3 changed files with 97 additions and 0 deletions

View File

@ -0,0 +1,36 @@
<?php
namespace MyAAC\Commands;
use MyAAC\Models\Settings as SettingsModel;
use MyAAC\Settings;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class SettingsResetCommand extends Command
{
protected function configure(): void
{
$this->setName('settings:reset')
->setDescription('Removes all settings in database');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
if (!$io->confirm('Are you sure you want to reset all settings in database?', false)) {
return Command::FAILURE;
}
SettingsModel::truncate();
$settings = Settings::getInstance();
$settings->clearCache();
$io->success('Setting cleared successfully');
return Command::SUCCESS;
}
}

View File

@ -0,0 +1,55 @@
<?php
namespace MyAAC\Commands;
use MyAAC\Models\Settings as SettingsModel;
use MyAAC\Settings;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class SettingsSetCommand extends Command
{
protected function configure(): void
{
$this->setName('settings:set')
->setDescription('Updates the setting specified by argument in database')
->addArgument('key',
InputArgument::REQUIRED,
'Setting name/key'
)
->addArgument('value',
InputArgument::REQUIRED,
'New value'
);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$key = $input->getArgument('key');
$value = $input->getArgument('value');
$settings = Settings::getInstance();
$settings->clearCache();
$settings->load();
$setting = $settings[$key];
if (!isset($setting['value'])) {
$io->warning('Settings with this key does not exists');
return Command::FAILURE;
}
// format plugin_name.key
// example: core.template
$explode = explode('.', $key);
$settings->updateInDatabase($explode[0], $explode[1], $value);
$settings->clearCache();
$io->success("Setting $key successfully updated");
return Command::SUCCESS;
}
}

View File

@ -464,9 +464,15 @@ class Settings implements \ArrayAccess
$ret['value'] = $value; $ret['value'] = $value;
} }
else { else {
if (!isset($this->settingsFile[$pluginKeyName]['settings'][$key])) {
return null;
}
$ret['value'] = $this->settingsFile[$pluginKeyName]['settings'][$key]['default']; $ret['value'] = $this->settingsFile[$pluginKeyName]['settings'][$key]['default'];
} }
$ret['key'] = $key;
if(isset($ret['type'])) { if(isset($ret['type'])) {
switch($ret['type']) { switch($ret['type']) {
case 'boolean': case 'boolean':