Implement YAML config loader

This commit is contained in:
Lexi / Zoe 2021-09-25 23:56:04 +02:00
parent 14c22e0e6c
commit 6c41f06105
Signed by: binaryDiv
GPG Key ID: F8D4956E224DA232
14 changed files with 169 additions and 9 deletions

View File

@ -10,6 +10,7 @@ MYSQL_USER=mailaccountadmin
MYSQL_PASSWORD=mailaccountadmin
# App settings
APP_TITLE="MailAccountAdmin [dev]"
APP_ENV=development
APP_DEBUG=true
APP_TIMEZONE=Europe/Berlin

4
.gitignore vendored
View File

@ -10,5 +10,9 @@
# PHP
/.composer
/.phpunit.cache
/.twig.cache
/coverage
/vendor
# Production settings
/config/app.yml

View File

@ -3,8 +3,10 @@ FROM php:7.4-apache AS base
WORKDIR /var/www
RUN apt-get update && \
apt-get install -y libzip-dev unzip git && \
docker-php-ext-install pdo pdo_mysql zip
apt-get install -y git unzip libyaml-dev libzip-dev && \
docker-php-ext-install pdo pdo_mysql zip && \
pecl install yaml && \
docker-php-ext-enable yaml
RUN a2enmod rewrite && \
sed -ri -e 's!/var/www/html!/var/www/public!g' /etc/apache2/sites-available/*.conf

View File

@ -4,7 +4,6 @@
## General
- Settings from a config file
- Database migrations
- Documentation
- App deployment
@ -12,6 +11,7 @@
- Refactor auth and session handling
## Admin user management
- Create/edit/delete admins
- Change own password

View File

@ -14,6 +14,7 @@
"require": {
"php": "^7.4",
"ext-pdo": "*",
"ext-yaml": "*",
"slim/slim": "^4.8",
"slim/psr7": "^1.3",
"php-di/php-di": "^6.3",

19
config/app.develop.yml Normal file
View File

@ -0,0 +1,19 @@
# Config file for local development (same settings as .env.develop)
# -- App settings
appTitle: "MailAccountAdmin [dev]"
environment: development
debug: true
timezone: Europe/Berlin
# -- Twig settings
twig:
cacheDir:
# -- Database settings
database:
host: db
port: 3306
name: mailusers
username: mailaccountadmin
password: mailaccountadmin

23
config/app.example.yml Normal file
View File

@ -0,0 +1,23 @@
# This is an example config file for MailAccountAdmin.
# Copy this file to /config/app.yml and change as needed.
# Settings that are commented out represent the default values.
# -- App settings
# appTitle: MailAccountAdmin
# environment: production
# debug: false
# timezone: UTC
# dateTimeFormat: r
# -- Twig settings
twig:
# Cache directory for Twig templates (default: unset, which means no cache is used)
cacheDir: .twig.cache
# -- Database settings
database:
host: localhost
port: 3306
name: mailusers
username: mailaccountadmin
password: very_secret_password

View File

@ -3,15 +3,21 @@ declare(strict_types=1);
require_once __DIR__ . '/../vendor/autoload.php';
use MailAccountAdmin\Config\Loaders\EnvConfigLoader;
use MailAccountAdmin\Config\Loaders\AutoConfigLoader;
use MailAccountAdmin\Dependencies;
use MailAccountAdmin\Middlewares;
use MailAccountAdmin\Routes;
use Slim\Factory\AppFactory;
const ROOT_DIR = __DIR__ . '/..';
session_start();
$config = EnvConfigLoader::loadFromEnv();
// Load application config (from config file or environment variables)
$configLoader = new AutoConfigLoader();
$config = $configLoader->loadConfig();
// Create application
$container = Dependencies::createContainer($config);
$app = AppFactory::createFromContainer($container);

View File

@ -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(),
];

View 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();
}
}

View File

@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace MailAccountAdmin\Config\Loaders;
use MailAccountAdmin\Config\AppConfig;
interface ConfigLoaderInterface
{
public function loadConfig(): AppConfig;
}

View File

@ -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

View 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,
]);
}
}

View File

@ -12,7 +12,11 @@
<header>
<h1>{{ app_info.getTitle() }}</h1>
<div class="header_appversion">
{% if logged_in | default() %}
<a href="{{ app_info.getRepositoryUrl() }}">{{ app_info.getVersion() }}</a>
{% else %}
{{ app_info.getVersion() }}
{% endif %}
</div>
<div class="header_spacer"></div>
<div class="header_userstatus">