mail-account-admin/src/Config/AppConfig.php

106 lines
2.6 KiB
PHP

<?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 getTwigCacheDir(): ?string
{
if (empty($this->twigCacheDir)) {
return null;
} elseif (substr($this->twigCacheDir, 0, 1) === '/') {
// Absolute path
return $this->twigCacheDir;
} else {
// Relative path
return ROOT_DIR . '/' . $this->twigCacheDir;
}
}
public function getTwigSettings(): array
{
return [
'cache' => $this->getTwigCacheDir() ?: 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,
];
}
}