Implement YAML config loader
This commit is contained in:
parent
14c22e0e6c
commit
6c41f06105
14 changed files with 169 additions and 9 deletions
48
src/Config/Loaders/YamlConfigLoader.php
Normal file
48
src/Config/Loaders/YamlConfigLoader.php
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
<?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' => (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' => (int)$parsedConfig['database']['port'] ?? null,
|
||||
'databaseName' => $parsedConfig['database']['name'] ?? null,
|
||||
'databaseUsername' => $parsedConfig['database']['username'] ?? null,
|
||||
'databasePassword' => $parsedConfig['database']['password'] ?? null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue