Refactor config management
This commit is contained in:
parent
f61ed20f32
commit
14c22e0e6c
11
.env.develop
11
.env.develop
|
|
@ -3,21 +3,22 @@
|
||||||
# composer: Set cache directory
|
# composer: Set cache directory
|
||||||
COMPOSER_CACHE_DIR=./.composer
|
COMPOSER_CACHE_DIR=./.composer
|
||||||
|
|
||||||
# MariaDB container
|
# Environment variables for MariaDB container
|
||||||
MYSQL_RANDOM_ROOT_PASSWORD=yes
|
MYSQL_RANDOM_ROOT_PASSWORD=yes
|
||||||
MYSQL_DATABASE=mailusers
|
MYSQL_DATABASE=mailusers
|
||||||
MYSQL_USER=mailaccountadmin
|
MYSQL_USER=mailaccountadmin
|
||||||
MYSQL_PASSWORD=mailaccountadmin
|
MYSQL_PASSWORD=mailaccountadmin
|
||||||
|
|
||||||
# App settings
|
# App settings
|
||||||
|
APP_ENV=development
|
||||||
APP_DEBUG=true
|
APP_DEBUG=true
|
||||||
|
APP_TIMEZONE=Europe/Berlin
|
||||||
|
|
||||||
# - Disable Twig cache
|
# Disable Twig cache
|
||||||
TWIG_CACHE_DIR=
|
TWIG_CACHE_DIR=
|
||||||
TWIG_STRICT=true
|
|
||||||
|
|
||||||
# - Database credentials
|
# Database credentials
|
||||||
DB_HOST=db
|
DB_HOST=db
|
||||||
DB_DATABASE=mailusers
|
DB_DATABASE=mailusers
|
||||||
DB_USER=mailaccountadmin
|
DB_USERNAME=mailaccountadmin
|
||||||
DB_PASSWORD=mailaccountadmin
|
DB_PASSWORD=mailaccountadmin
|
||||||
|
|
|
||||||
|
|
@ -18,8 +18,6 @@ RUN cp $PHP_INI_DIR/php.ini-development $PHP_INI_DIR/php.ini && \
|
||||||
pecl install xdebug && \
|
pecl install xdebug && \
|
||||||
docker-php-ext-enable xdebug
|
docker-php-ext-enable xdebug
|
||||||
|
|
||||||
ENV APP_ENV=development
|
|
||||||
|
|
||||||
# TODO: production image untested
|
# TODO: production image untested
|
||||||
#FROM base AS production
|
#FROM base AS production
|
||||||
#
|
#
|
||||||
|
|
|
||||||
|
|
@ -3,19 +3,19 @@ declare(strict_types=1);
|
||||||
|
|
||||||
require_once __DIR__ . '/../vendor/autoload.php';
|
require_once __DIR__ . '/../vendor/autoload.php';
|
||||||
|
|
||||||
|
use MailAccountAdmin\Config\Loaders\EnvConfigLoader;
|
||||||
use MailAccountAdmin\Dependencies;
|
use MailAccountAdmin\Dependencies;
|
||||||
use MailAccountAdmin\Middlewares;
|
use MailAccountAdmin\Middlewares;
|
||||||
use MailAccountAdmin\Routes;
|
use MailAccountAdmin\Routes;
|
||||||
use MailAccountAdmin\Settings;
|
|
||||||
use Slim\Factory\AppFactory;
|
use Slim\Factory\AppFactory;
|
||||||
|
|
||||||
session_start();
|
session_start();
|
||||||
|
|
||||||
$settings = new Settings();
|
$config = EnvConfigLoader::loadFromEnv();
|
||||||
$container = Dependencies::createContainer($settings);
|
$container = Dependencies::createContainer($config);
|
||||||
$app = AppFactory::createFromContainer($container);
|
$app = AppFactory::createFromContainer($container);
|
||||||
|
|
||||||
Middlewares::setMiddlewares($app, $settings);
|
Middlewares::setMiddlewares($app, $config);
|
||||||
Routes::setRoutes($app);
|
Routes::setRoutes($app);
|
||||||
|
|
||||||
$app->run();
|
$app->run();
|
||||||
|
|
|
||||||
|
|
@ -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,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -7,6 +7,7 @@ use DI\Container;
|
||||||
use MailAccountAdmin\Common\PasswordHelper;
|
use MailAccountAdmin\Common\PasswordHelper;
|
||||||
use MailAccountAdmin\Common\SessionHelper;
|
use MailAccountAdmin\Common\SessionHelper;
|
||||||
use MailAccountAdmin\Common\UserHelper;
|
use MailAccountAdmin\Common\UserHelper;
|
||||||
|
use MailAccountAdmin\Config\AppConfig;
|
||||||
use MailAccountAdmin\Frontend\Accounts\AccountController;
|
use MailAccountAdmin\Frontend\Accounts\AccountController;
|
||||||
use MailAccountAdmin\Frontend\Accounts\AccountHandler;
|
use MailAccountAdmin\Frontend\Accounts\AccountHandler;
|
||||||
use MailAccountAdmin\Frontend\Domains\DomainController;
|
use MailAccountAdmin\Frontend\Domains\DomainController;
|
||||||
|
|
@ -23,43 +24,43 @@ use Twig\Extension\CoreExtension as TwigCoreExtension;
|
||||||
|
|
||||||
class Dependencies
|
class Dependencies
|
||||||
{
|
{
|
||||||
public const SETTINGS = 'settings';
|
public const CONFIG = 'config';
|
||||||
public const TWIG = 'view';
|
public const TWIG = 'view';
|
||||||
private const TWIG_TEMPLATE_DIR = __DIR__ . '/../templates';
|
private const TWIG_TEMPLATE_DIR = __DIR__ . '/../templates';
|
||||||
public const DATABASE = 'database';
|
public const DATABASE = 'database';
|
||||||
|
|
||||||
public static function createContainer(Settings $settings): Container
|
public static function createContainer(AppConfig $config): Container
|
||||||
{
|
{
|
||||||
$container = new Container();
|
$container = new Container();
|
||||||
|
|
||||||
// App settings
|
// App configuration
|
||||||
$container->set(self::SETTINGS, $settings);
|
$container->set(self::CONFIG, $config);
|
||||||
|
|
||||||
// App information
|
// App information
|
||||||
$container->set(AppInfo::class, function (ContainerInterface $c) {
|
$container->set(AppInfo::class, function (ContainerInterface $c) {
|
||||||
/** @var Settings $settings */
|
/** @var AppConfig $config */
|
||||||
$settings = $c->get(self::SETTINGS);
|
$config = $c->get(self::CONFIG);
|
||||||
$versionHelper = new VersionHelper();
|
$versionHelper = new VersionHelper($config);
|
||||||
|
|
||||||
return new AppInfo(
|
return new AppInfo(
|
||||||
$settings->getAppTitle(),
|
$config->getAppTitle(),
|
||||||
$versionHelper->getAppVersion(),
|
$versionHelper->getAppVersion(),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Twig template engine
|
// Twig template engine
|
||||||
$container->set(self::TWIG, function (ContainerInterface $c) {
|
$container->set(self::TWIG, function (ContainerInterface $c) {
|
||||||
/** @var Settings $settings */
|
/** @var AppConfig $config */
|
||||||
$settings = $c->get(self::SETTINGS);
|
$config = $c->get(self::CONFIG);
|
||||||
|
|
||||||
// Create Twig view
|
// Create Twig view
|
||||||
$twig = Twig::create(self::TWIG_TEMPLATE_DIR, $settings->getTwigSettings());
|
$twig = Twig::create(self::TWIG_TEMPLATE_DIR, $config->getTwigSettings());
|
||||||
|
|
||||||
// Set default date format
|
// Set default date format
|
||||||
/** @var TwigCoreExtension $coreExtension */
|
/** @var TwigCoreExtension $coreExtension */
|
||||||
$coreExtension = $twig->getEnvironment()->getExtension(TwigCoreExtension::class);
|
$coreExtension = $twig->getEnvironment()->getExtension(TwigCoreExtension::class);
|
||||||
$coreExtension->setDateFormat($settings->getDateFormat());
|
$coreExtension->setDateFormat($config->getDateTimeFormat());
|
||||||
$coreExtension->setTimezone($settings->getTimezone());
|
$coreExtension->setTimezone($config->getTimezone());
|
||||||
|
|
||||||
// Add app information to globals
|
// Add app information to globals
|
||||||
$appInfo = $c->get(AppInfo::class);
|
$appInfo = $c->get(AppInfo::class);
|
||||||
|
|
@ -71,9 +72,9 @@ class Dependencies
|
||||||
|
|
||||||
// Database connection
|
// Database connection
|
||||||
$container->set(self::DATABASE, function (ContainerInterface $c) {
|
$container->set(self::DATABASE, function (ContainerInterface $c) {
|
||||||
/** @var Settings $settings */
|
/** @var AppConfig $config */
|
||||||
$settings = $c->get(self::SETTINGS);
|
$config = $c->get(self::CONFIG);
|
||||||
$dbSettings = $settings->getDatabaseSettings();
|
$dbSettings = $config->getDatabaseSettings();
|
||||||
|
|
||||||
return new PDO(
|
return new PDO(
|
||||||
"mysql:dbname={$dbSettings['dbname']};host={$dbSettings['host']};port={$dbSettings['port']}",
|
"mysql:dbname={$dbSettings['dbname']};host={$dbSettings['host']};port={$dbSettings['port']}",
|
||||||
|
|
@ -108,7 +109,7 @@ class Dependencies
|
||||||
});
|
});
|
||||||
|
|
||||||
// Helper classes
|
// Helper classes
|
||||||
$container->set(SessionHelper::class, function (ContainerInterface $c) {
|
$container->set(SessionHelper::class, function () {
|
||||||
return new SessionHelper();
|
return new SessionHelper();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -120,7 +121,7 @@ class Dependencies
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
$container->set(PasswordHelper::class, function (ContainerInterface $c) {
|
$container->set(PasswordHelper::class, function () {
|
||||||
return new PasswordHelper();
|
return new PasswordHelper();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,14 +4,15 @@ declare(strict_types=1);
|
||||||
namespace MailAccountAdmin;
|
namespace MailAccountAdmin;
|
||||||
|
|
||||||
use MailAccountAdmin\Auth\AuthMiddleware;
|
use MailAccountAdmin\Auth\AuthMiddleware;
|
||||||
|
use MailAccountAdmin\Config\AppConfig;
|
||||||
use Slim\App;
|
use Slim\App;
|
||||||
use Slim\Views\TwigMiddleware;
|
use Slim\Views\TwigMiddleware;
|
||||||
|
|
||||||
class Middlewares
|
class Middlewares
|
||||||
{
|
{
|
||||||
public static function setMiddlewares(App $app, Settings $settings): void
|
public static function setMiddlewares(App $app, AppConfig $config): void
|
||||||
{
|
{
|
||||||
$displayErrorDetails = $settings->isDebugMode();
|
$displayErrorDetails = $config->isDebugMode();
|
||||||
|
|
||||||
$app->addErrorMiddleware($displayErrorDetails, true, true);
|
$app->addErrorMiddleware($displayErrorDetails, true, true);
|
||||||
$app->add(new AuthMiddleware());
|
$app->add(new AuthMiddleware());
|
||||||
|
|
|
||||||
|
|
@ -1,49 +0,0 @@
|
||||||
<?php
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace MailAccountAdmin;
|
|
||||||
|
|
||||||
class Settings
|
|
||||||
{
|
|
||||||
public function getAppTitle(): string
|
|
||||||
{
|
|
||||||
return getenv('APP_TITLE') ?: 'MailAccountAdmin';
|
|
||||||
}
|
|
||||||
|
|
||||||
public function isDebugMode(): bool
|
|
||||||
{
|
|
||||||
return getenv('APP_DEBUG') === 'true';
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getTimezone(): string
|
|
||||||
{
|
|
||||||
return 'Europe/Berlin';
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getDateFormat(): string
|
|
||||||
{
|
|
||||||
// Default date format (https://www.php.net/manual/en/datetime.format.php)
|
|
||||||
// RFC 2822 formatted date (Thu, 21 Dec 2000 16:01:07 +0200)
|
|
||||||
return 'r';
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getTwigSettings(): array
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
'cache' => getenv('TWIG_CACHE_DIR') ?: false,
|
|
||||||
'debug' => $this->isDebugMode(),
|
|
||||||
'strict_variables' => getenv('TWIG_STRICT') === 'true',
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getDatabaseSettings(): array
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
'host' => getenv('DB_HOST') ?: 'localhost',
|
|
||||||
'port' => getenv('DB_PORT') ?: 3306,
|
|
||||||
'dbname' => getenv('DB_DATABASE') ?: '',
|
|
||||||
'username' => getenv('DB_USER') ?: '',
|
|
||||||
'password' => getenv('DB_PASSWORD') ?: '',
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -3,16 +3,18 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace MailAccountAdmin;
|
namespace MailAccountAdmin;
|
||||||
|
|
||||||
|
use MailAccountAdmin\Config\AppConfig;
|
||||||
|
|
||||||
class VersionHelper
|
class VersionHelper
|
||||||
{
|
{
|
||||||
private string $version;
|
private string $version;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct(AppConfig $config)
|
||||||
{
|
{
|
||||||
$version = $this->loadFromVersionFile();
|
$version = $this->loadFromVersionFile();
|
||||||
if (!empty($version)) {
|
if (!empty($version)) {
|
||||||
$this->version = $version;
|
$this->version = $version;
|
||||||
} elseif ($this->inDevelopmentMode()) {
|
} elseif ($config->getAppEnvironment() === 'development') {
|
||||||
$this->version = '[dev version]';
|
$this->version = '[dev version]';
|
||||||
} else {
|
} else {
|
||||||
$this->version = '[undefined version]';
|
$this->version = '[undefined version]';
|
||||||
|
|
@ -34,9 +36,4 @@ class VersionHelper
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function inDevelopmentMode(): bool
|
|
||||||
{
|
|
||||||
return getenv('APP_ENV') === 'development';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue