added test support

This commit is contained in:
tracer 2022-10-06 16:18:25 +02:00
parent c166b5774c
commit 010914b7bd
1 changed files with 49 additions and 40 deletions

View File

@ -7,49 +7,58 @@ namespace App\Controller;
*/ */
class ConfigController class ConfigController
{ {
private array $config; private array $config;
public function __construct(bool $verbose = false, bool $quiet = false) {
$configFile = dirname(path: __DIR__, levels: 2) . "/config.json.local";
if (!file_exists(filename: $configFile)) {
$configFile = dirname(path: __DIR__, levels: 2) . "/config.json";
}
if (!file_exists(filename: $configFile)) {
echo 'Missing config file' . PHP_EOL;
if (confirm(message: 'Should I create a new config based on config.json.sample?')) {
copy(from: 'config.json.sample', to: 'config.json');
echo 'Config file has been generated. Adjust it to your needs, then proceed to database setup.' . PHP_EOL;
} else {
echo 'You first have to setup the bindAPI. Bye.' . PHP_EOL;
exit(0);
}
exit(1);
}
$configJSON = file_get_contents(filename: $configFile);
if (json_decode(json: $configJSON) === null) { /**
echo 'Config file is not valid JSON.' . PHP_EOL; * @param bool $verbose
echo $configJSON . PHP_EOL; * @param bool $quiet
exit(1); * @param bool $test
} */
public function __construct(bool $verbose = false, bool $quiet = false, bool $test = false)
$this->config = json_decode(json: $configJSON, associative: true); {
if ($verbose) { if ($test) {
$this->config['verbose'] = true; $configFile = dirname(path: __DIR__, levels: 2) . "/config.json.test";
} else { if (!file_exists(filename: $configFile)) {
$this->config['verbose'] = false; echo 'No testing (config.json.test) config has benn setup.' . PHP_EOL;
} die(1);
if ($quiet) { }
$this->config['quiet'] = true;
} else { } else {
$this->config['quiet'] = false; $configFile = dirname(path: __DIR__, levels: 2) . "/config.json.local";
if (!file_exists(filename: $configFile)) {
$configFile = dirname(path: __DIR__, levels: 2) . "/config.json";
}
if (!file_exists(filename: $configFile)) {
echo 'Missing config file' . PHP_EOL;
if (confirm(message: 'Should I create a new config based on config.json.sample?')) {
copy(from: 'config.json.sample', to: 'config.json');
echo 'Config file has been generated. Adjust it to your needs, then proceed to database setup.' . PHP_EOL;
} else {
echo 'You first have to setup the bindAPI. Bye.' . PHP_EOL;
exit(0);
}
exit(1);
}
} }
} $configJSON = file_get_contents(filename: $configFile);
public function getConfig(string $configKey): string { if (json_decode(json: $configJSON) === null) {
return $this->config[$configKey]; echo 'Config file is not valid JSON.' . PHP_EOL;
} echo $configJSON . PHP_EOL;
exit(1);
}
$this->config = json_decode(json: $configJSON, associative: true);
$this->config['verbose'] = (bool)$verbose;
$this->config['quiet'] = (bool)$quiet;
$this->config['test'] = (bool)$test;
}
public function getConfig(string $configKey): string
{
return $this->config[$configKey];
}
} }