initial commit
This commit is contained in:
parent
fdd24d90da
commit
9b3a6b1f3a
40
src/Service/Config.php
Normal file
40
src/Service/Config.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class Config
|
||||
{
|
||||
private array $config;
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$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)) {
|
||||
throw new Exception(message: 'Missing config file');
|
||||
}
|
||||
$configJSON = file_get_contents(filename: $configFile);
|
||||
|
||||
if (json_decode(json: $configJSON) === null) {
|
||||
throw new Exception(message: 'Config file is not valid JSON.');
|
||||
}
|
||||
|
||||
$this->config = json_decode(json: $configJSON, associative: true);
|
||||
}
|
||||
|
||||
public function getConfig(string $configKey): string
|
||||
{
|
||||
return $this->config[$configKey];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user