Implement YAML config loader
This commit is contained in:
parent
14c22e0e6c
commit
6c41f06105
14 changed files with 169 additions and 9 deletions
|
|
@ -70,10 +70,23 @@ class AppConfig
|
|||
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->twigCacheDir ?: false,
|
||||
'cache' => $this->getTwigCacheDir() ?: false,
|
||||
'debug' => $this->isDebugMode(),
|
||||
'strict_variables' => $this->isDebugMode(),
|
||||
];
|
||||
|
|
|
|||
28
src/Config/Loaders/AutoConfigLoader.php
Normal file
28
src/Config/Loaders/AutoConfigLoader.php
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace MailAccountAdmin\Config\Loaders;
|
||||
|
||||
use MailAccountAdmin\Config\AppConfig;
|
||||
|
||||
class AutoConfigLoader implements ConfigLoaderInterface
|
||||
{
|
||||
private ConfigLoaderInterface $configLoader;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$yamlFilePath = ROOT_DIR . '/config/app.yml';
|
||||
|
||||
// Check if yml config file exists
|
||||
if (file_exists($yamlFilePath)) {
|
||||
$this->configLoader = new YamlConfigLoader($yamlFilePath);
|
||||
} else {
|
||||
$this->configLoader = new EnvConfigLoader();
|
||||
}
|
||||
}
|
||||
|
||||
public function loadConfig(): AppConfig
|
||||
{
|
||||
return $this->configLoader->loadConfig();
|
||||
}
|
||||
}
|
||||
11
src/Config/Loaders/ConfigLoaderInterface.php
Normal file
11
src/Config/Loaders/ConfigLoaderInterface.php
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace MailAccountAdmin\Config\Loaders;
|
||||
|
||||
use MailAccountAdmin\Config\AppConfig;
|
||||
|
||||
interface ConfigLoaderInterface
|
||||
{
|
||||
public function loadConfig(): AppConfig;
|
||||
}
|
||||
|
|
@ -5,9 +5,9 @@ namespace MailAccountAdmin\Config\Loaders;
|
|||
|
||||
use MailAccountAdmin\Config\AppConfig;
|
||||
|
||||
class EnvConfigLoader
|
||||
class EnvConfigLoader implements ConfigLoaderInterface
|
||||
{
|
||||
public static function loadFromEnv(): AppConfig
|
||||
public function loadConfig(): AppConfig
|
||||
{
|
||||
return AppConfig::createFromArray([
|
||||
// App settings
|
||||
|
|
|
|||
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