Add version numbers; make app title configurable

This commit is contained in:
Lexi / Zoe 2021-09-23 00:55:05 +02:00
parent 186dcdc0cb
commit 8df2684e5c
Signed by: binaryDiv
GPG key ID: F8D4956E224DA232
11 changed files with 159 additions and 38 deletions

33
src/AppInfo.php Normal file
View file

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace MailAccountAdmin;
class AppInfo
{
private const REPOSITORY_URL = 'https://git.0xbd.space/0xbd/mail-account-admin';
private string $title;
private string $version;
public function __construct(string $title, string $version)
{
$this->title = $title;
$this->version = $version;
}
public function getTitle(): string
{
return $this->title;
}
public function getVersion(): string
{
return $this->version;
}
public function getRepositoryUrl(): string
{
return self::REPOSITORY_URL;
}
}

View file

@ -35,6 +35,18 @@ class Dependencies
// App settings
$container->set(self::SETTINGS, $settings);
// App information
$container->set(AppInfo::class, function (ContainerInterface $c) {
/** @var Settings $settings */
$settings = $c->get(self::SETTINGS);
$versionHelper = new VersionHelper();
return new AppInfo(
$settings->getAppTitle(),
$versionHelper->getAppVersion(),
);
});
// Twig template engine
$container->set(self::TWIG, function (ContainerInterface $c) {
/** @var Settings $settings */
@ -49,6 +61,10 @@ class Dependencies
$coreExtension->setDateFormat($settings->getDateFormat());
$coreExtension->setTimezone($settings->getTimezone());
// Add app information to globals
$appInfo = $c->get(AppInfo::class);
$twig->getEnvironment()->addGlobal('app_info', $appInfo);
// Return Twig view
return $twig;
});

View file

@ -21,6 +21,7 @@ class BaseController
// Register globals
$twigEnv = $view->getEnvironment();
$twigEnv->addGlobal('logged_in', $userHelper->isLoggedIn());
$twigEnv->addGlobal('current_user_name', $userHelper->isLoggedIn() ? $userHelper->getCurrentUser()->getUsername() : null);
}

View file

@ -5,6 +5,11 @@ namespace MailAccountAdmin;
class Settings
{
public function getAppTitle(): string
{
return getenv('APP_TITLE') ?: 'MailAccountAdmin';
}
public function isDebugMode(): bool
{
return getenv('APP_DEBUG') === 'true';

42
src/VersionHelper.php Normal file
View file

@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace MailAccountAdmin;
class VersionHelper
{
private string $version;
public function __construct()
{
$version = $this->loadFromVersionFile();
if (!empty($version)) {
$this->version = $version;
} elseif ($this->inDevelopmentMode()) {
$this->version = '[dev version]';
} else {
$this->version = '[undefined version]';
}
}
public function getAppVersion(): string
{
return $this->version;
}
private function loadFromVersionFile(): ?string
{
$versionFilePath = __DIR__ . '/../VERSION';
if (file_exists($versionFilePath)) {
$fileContent = file($versionFilePath);
return $fileContent[0] ?? null;
}
return null;
}
private function inDevelopmentMode(): bool
{
return getenv('APP_ENV') === 'development';
}
}