Refactor config management
This commit is contained in:
parent
f61ed20f32
commit
14c22e0e6c
9 changed files with 159 additions and 87 deletions
92
src/Config/AppConfig.php
Normal file
92
src/Config/AppConfig.php
Normal 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,
|
||||
];
|
||||
}
|
||||
}
|
||||
31
src/Config/Loaders/EnvConfigLoader.php
Normal file
31
src/Config/Loaders/EnvConfigLoader.php
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace MailAccountAdmin\Config\Loaders;
|
||||
|
||||
use MailAccountAdmin\Config\AppConfig;
|
||||
|
||||
class EnvConfigLoader
|
||||
{
|
||||
public static function loadFromEnv(): AppConfig
|
||||
{
|
||||
return AppConfig::createFromArray([
|
||||
// App settings
|
||||
'appTitle' => getenv('APP_TITLE') ?: null,
|
||||
'environment' => getenv('APP_ENV') ?: null,
|
||||
'debug' => getenv('APP_DEBUG') === 'true' ? true : null,
|
||||
'timezone' => getenv('APP_TIMEZONE') ?: null,
|
||||
'dateTimeFormat' => getenv('APP_DATE_TIME_FORMAT') ?: null,
|
||||
|
||||
// Twig settings
|
||||
'twigCacheDir' => getenv('TWIG_CACHE_DIR') ?: null,
|
||||
|
||||
// Database settings
|
||||
'databaseHost' => getenv('DB_HOST') ?: null,
|
||||
'databasePort' => (int)getenv('DB_PORT') ?: null,
|
||||
'databaseName' => getenv('DB_DATABASE') ?: null,
|
||||
'databaseUsername' => getenv('DB_USERNAME') ?: null,
|
||||
'databasePassword' => getenv('DB_PASSWORD') ?: null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue