first commit

This commit is contained in:
tracer 2022-09-17 15:48:26 +02:00
parent 46fdadf8e5
commit 09c3f5c1dd
1 changed files with 50 additions and 0 deletions

View File

@ -0,0 +1,50 @@
<?php
namespace App\Controller;
/**
*
*/
class ConfigController
{
private array $config;
public function __construct(bool $verbose = 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;
echo $configJSON . PHP_EOL;
exit(1);
}
$this->config = json_decode(json: $configJSON, associative: true);
if ($verbose) {
$this->config['verbose'] = true;
} else {
$this->config['verbose'] = false;
}
}
public function getConfig(string $configKey): string {
return $this->config[$configKey];
}
}