Commit 9898e606 authored by Baudouin Feildel's avatar Baudouin Feildel

Add app:parameter command

parent f3401aa5
Pipeline #1152 passed with stage
in 1 minute and 38 seconds
<?php
namespace App\Command;
use App\Repository\SettingRepository;
use App\Service\Settings;
use Exception;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class AppParameter extends Command
{
protected static $defaultName = 'app:parameter';
/**
* @var SettingRepository
*/
private $settingRepository;
/**
* @var Settings
*/
private $settings;
public function __construct(SettingRepository $repository, Settings $settings)
{
parent::__construct(self::$defaultName);
$this->settingRepository = $repository;
$this->settings = $settings;
}
protected function configure()
{
$this
->setDescription('Manage application parameters')
->addArgument('name', InputArgument::REQUIRED, 'The parameter to manage')
->addOption('value', null, InputOption::VALUE_REQUIRED, 'Set the parameter to given value')
->addOption('reset', null, InputOption::VALUE_NONE, 'Reset the parameter to its default value')
->addOption('get', null, InputOption::VALUE_NONE, 'Show the current value');
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int|null
* @throws Exception
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$name = $input->getArgument('name');
$value = $input->getOption('value');
$reset = $input->getOption('reset');
$get = $input->getOption('get');
$io = new SymfonyStyle($input, $output);
if (!$get && !$reset && !$value) {
$io->error("You must provide at least --get, --reset or --value.");
return 1;
}
if (($get && $reset) || ($get && $value) || ($reset && $value)) {
$io->error("You can either specify --get, --reset or --value, not a combination of them");
return 1;
}
if ($get) {
$action = "get";
} else if ($reset) {
$action = "reset";
} else if ($value) {
$action = "set";
} else {
$io->error("No action selected.");
return 2;
}
switch ($action) {
case "get":
if ($this->settings->has($name)) {
$io->writeln($this->settings->get($name));
} else {
$io->error("Unknown parameter: $name");
return 3;
}
break;
case "reset":
$defaultValue = $this->settings->getDefault($name);
$this->settings->set($name, $defaultValue);
break;
case "set":
$this->settings->set($name, $value);
break;
}
return 0;
}
}
\ No newline at end of file
......@@ -3,86 +3,142 @@
namespace App\Service;
use App\Entity\Setting;
use App\Repository\SettingRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
use Exception;
use Psr\Log\LoggerInterface;
use RuntimeException;
class Settings
{
/**
* @var null|string Application name
*/
private $appName = null;
/**
* @var null|string Admin email
*/
private $adminEmail = null;
/**
* @var null|string Admin name
*/
private $adminName = null;
/**
* @var null|string User access control status: enabled/disabled
*/
private $uacStatus = null;
/**
* @var null|string Path to login logo relative to public folder (must be web accessible)
*/
private $loginLogo = null;
private $settingsRepo;
private $logger;
public function __construct(SettingRepository $settingRepository, LoggerInterface $logger) {
$this->settingsRepo = $settingRepository;
$this->logger = $logger;
}
/**
* @param null|string $value
* @param string $key
* @param string $defaultValue
*/
private function loadIfNull(?string &$value, string $key, string $defaultValue) {
if($value == null) {
$this->logger->info("Load setting value: $key");
$tmp = $this->settingsRepo->findOneBy(['key' => $key]);
if($tmp) {
$value = $tmp->getValue();
$this->logger->info("Loaded setting value: $key -> $value");
} else {
$value = $defaultValue;
$this->logger->info("Loaded setting default value: $key -> $value");
}
}
}
public function getAppName() : string {
$this->loadIfNull($this->appName, "app.name", "DDEC-Info");
return $this->appName;
}
public function getAdminEmail() : string {
$this->loadIfNull($this->adminEmail, "app.admin.email", "");
return $this->adminEmail;
}
public function getAdminName() : string {
$this->loadIfNull($this->adminName, "app.admin.name", "");
return $this->adminName;
}
public function uacEnabled(): bool {
$this->loadIfNull($this->uacStatus, "app.uac.enable", "true");
return $this->uacStatus;
}
public function getLoginLogo() {
$this->loadIfNull($this->loginLogo, "app.style.login_logo", "");
return $this->loginLogo;
}
/**
* @var SettingRepository
*/
private $settingsRepo;
private $logger;
private $doctrine;
private $cache = [
"app.name" => ['value' => null, "default" => "DDEC-Info", "type" => Setting::T_STRING],
"app.admin.email" => ['value' => null, "default" => "", "type" => Setting::T_STRING],
"app.admin.name" => ['value' => null, "default" => "", "type" => Setting::T_STRING],
"app.uac.enable" => ['value' => null, "default" => true, "type" => Setting::T_BOOL],
"app.style.login_logo" => ['value' => null, "default" => "", "type" => Setting::T_STRING],
];
public function __construct(SettingRepository $settingRepository, ManagerRegistry $doctrine, LoggerInterface $logger)
{
$this->settingsRepo = $settingRepository;
$this->doctrine = $doctrine;
$this->logger = $logger;
}
/**
* @param string $key
* @return Setting|object
*/
private function loadFromDb(string $key)
{
return $this->settingsRepo->findOneBy(['key' => $key]);
}
/**
* @param string $key
* @return mixed
*/
private function loadIfNull(string $key)
{
if (!isset($this->cache[$key])) {
$this->logger->warning("Creating temporary setting: $key");
$this->cache[$key] = ['value' => null, 'default' => '', "type" => Setting::T_MIXED];
}
if ($this->cache[$key]['value'] === null) {
$this->logger->info("Load setting value: $key");
$tmp = $this->loadFromDb($key);
if ($tmp) {
$this->cache[$key]['value'] = $tmp->getValue();
$this->logger->info("Loaded setting value: $key -> " . $this->cache[$key]['value']);
} else {
$this->cache[$key]['value'] = $this->cache[$key]['default'];
$this->logger->info("Loaded setting default value: $key -> " . $this->cache[$key]['value']);
}
}
return $this->cache[$key]['value'];
}
public function getAppName(): string
{
return $this->loadIfNull("app.name");
}
public function getAdminEmail(): string
{
return $this->loadIfNull("app.admin.email");
}
public function getAdminName(): string
{
return $this->loadIfNull("app.admin.name");
}
public function uacEnabled(): bool
{
return $this->loadIfNull("app.uac.enable");
}
public function getLoginLogo()
{
return $this->loadIfNull("app.style.login_logo");
}
public function get(string $key)
{
return $this->loadIfNull($key);
}
public function has(string $key)
{
// If we have the key in cache and it is not null, or if key is in database
return (isset($this->cache[$key]) && $this->cache[$key]['value'] !== null) ||
$this->settingsRepo->count(['key' => $key]) > 0;
}
public function getDefault(string $key)
{
if (isset($this->cache[$key])) {
return $this->cache[$key]['default'];
} else {
$tmp = $this->loadFromDb($key);
if ($tmp) {
return $tmp->getDefaultValue();
} else {
throw new RuntimeException("Parameter $key doesn't exists.");
}
}
}
/**
* @param string $key
* @param $value
* @throws Exception
*/
public function set(string $key, $value)
{
/** @var Setting $setting */
$setting = $this->loadFromDb($key);
if ($setting === null) {
$setting = new Setting();
$setting->setKey($key);
$setting->setType(Setting::T_MIXED);
$setting->setDefaultValue('');
}
$setting->setValue($value);
$em = $this->doctrine->getManager('ddec');
$em->persist($setting);
$em->flush();
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment