49 lines
1.7 KiB
PHP
49 lines
1.7 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace MailAccountAdmin\Config\Loaders;
|
|
|
|
use MailAccountAdmin\Config\AppConfig;
|
|
|
|
class YamlConfigLoader implements ConfigLoaderInterface
|
|
{
|
|
private string $filePath;
|
|
|
|
public function __construct(string $filePath)
|
|
{
|
|
assert(file_exists($filePath));
|
|
|
|
$this->filePath = $filePath;
|
|
}
|
|
|
|
public function loadConfig(): AppConfig
|
|
{
|
|
// Parse yml config file
|
|
$parsedConfig = yaml_parse_file($this->filePath);
|
|
|
|
// Check datatypes
|
|
assert(is_array($parsedConfig));
|
|
assert(!isset($parsedConfig['twig']) || is_array($parsedConfig['twig']));
|
|
assert(!isset($parsedConfig['database']) || is_array($parsedConfig['database']));
|
|
|
|
return AppConfig::createFromArray([
|
|
// App settings
|
|
'appTitle' => $parsedConfig['appTitle'] ?? null,
|
|
'environment' => $parsedConfig['environment'] ?? null,
|
|
'debug' => isset($parsedConfig['debug'] ? (bool)$parsedConfig['debug'] : null,
|
|
'timezone' => $parsedConfig['timezone'] ?? null,
|
|
'dateTimeFormat' => $parsedConfig['dateTimeFormat'] ?? null,
|
|
|
|
// Twig settings
|
|
'twigCacheDir' => $parsedConfig['twig']['cacheDir'] ?? null,
|
|
|
|
// Database settings
|
|
'databaseHost' => $parsedConfig['database']['host'] ?? null,
|
|
'databasePort' => isset($parsedConfig['database']['port'] ? (int)$parsedConfig['database']['port'] : null,
|
|
'databaseName' => $parsedConfig['database']['name'] ?? null,
|
|
'databaseUsername' => $parsedConfig['database']['username'] ?? null,
|
|
'databasePassword' => $parsedConfig['database']['password'] ?? null,
|
|
]);
|
|
}
|
|
}
|