Refactor config management

This commit is contained in:
Lexi / Zoe 2021-09-24 20:24:07 +02:00
parent f61ed20f32
commit 14c22e0e6c
Signed by: binaryDiv
GPG key ID: F8D4956E224DA232
9 changed files with 159 additions and 87 deletions

92
src/Config/AppConfig.php Normal file
View file

@ -0,0 +1,92 @@
<?php
declare(strict_types=1);
namespace MailAccountAdmin\Config;
class AppConfig
{
// App settings
protected string $appTitle = 'MailAccountAdmin';
protected string $environment = 'production';
protected bool $debug = false;
protected string $timezone;
protected string $dateTimeFormat = 'r';
// Twig settings
protected ?string $twigCacheDir = null;
// Database settings
protected string $databaseHost = 'localhost';
protected int $databasePort = 3306;
protected string $databaseName = '';
protected string $databaseUsername = '';
protected string $databasePassword = '';
protected function __construct()
{
// Set default timezone from php.ini
$this->timezone = ini_get('date.timezone') ?: 'UTC';
}
public static function createFromArray(array $configArray): self
{
$config = new self();
foreach ($configArray as $key => $value) {
assert(property_exists($config, $key));
if ($value !== null) {
$config->$key = $value;
}
}
return $config;
}
public function getAppTitle(): string
{
return $this->appTitle;
}
public function getAppEnvironment(): string
{
return $this->environment;
}
public function isDebugMode(): bool
{
return $this->debug;
}
public function getTimezone(): string
{
return $this->timezone;
}
public function getDateTimeFormat(): string
{
// Specify datetime format (https://www.php.net/manual/en/datetime.format.php)
// Default value "r": RFC 2822 formatted date (Thu, 21 Dec 2000 16:01:07 +0200)
return $this->dateTimeFormat;
}
public function getTwigSettings(): array
{
return [
'cache' => $this->twigCacheDir ?: false,
'debug' => $this->isDebugMode(),
'strict_variables' => $this->isDebugMode(),
];
}
public function getDatabaseSettings(): array
{
return [
'host' => $this->databaseHost,
'port' => $this->databasePort,
'dbname' => $this->databaseName,
'username' => $this->databaseUsername,
'password' => $this->databasePassword,
];
}
}