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

1
.gitignore vendored
View File

@ -5,6 +5,7 @@
# General
/tmp
/_tmp
/VERSION
# PHP
/.composer

View File

@ -18,6 +18,7 @@ RUN cp $PHP_INI_DIR/php.ini-development $PHP_INI_DIR/php.ini && \
pecl install xdebug && \
docker-php-ext-enable xdebug
ENV APP_ENV=development
# TODO: production image untested
#FROM base AS production

View File

@ -9,7 +9,7 @@ DOCKER_RUN = $(DOCKER_COMPOSE) run --rm app
COMPOSER = $(DOCKER_RUN) composer
PHPUNIT = $(DOCKER_RUN) vendor/bin/phpunit
.PHONY: all clean \
.PHONY: all clean VERSION \
docker-up docker-up-detached docker-down docker-restart docker-build docker-rebuild docker-purge docker-logs docker-run \
composer-install composer-install-no-dev composer-update composer-cmd \
test phpunit open-coverage
@ -94,9 +94,17 @@ open-coverage:
$(or $(BROWSER),firefox) coverage/index.html
# Version management
# ------------------
# Create VERSION file from current git tag
version:
git describe | tee VERSION
# Clean up
# --------
# Remove vendor directory, phpunit caches and other files that have been generated
clean: docker-down
rm -rf .composer vendor .phpunit.cache coverage
rm -rf .composer vendor .phpunit.cache coverage VERSION

View File

@ -57,15 +57,32 @@ header {
margin: 1rem;
padding: 0 1rem;
display: flex;
flex-flow: row wrap;
justify-content: space-between;
align-content: center;
align-items: center;
align-items: baseline;
}
header h1 {
header > * {
margin: 1rem;
}
header > .header_appversion {
font-size: 0.75rem;
color: #767676;
}
header > .header_appversion > a {
color: inherit;
}
header > .header_spacer {
width: 0;
margin: 0;
padding: 0;
flex: 1 0 0;
}
/* -- Navigation bar -- */
nav {
}

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';
}
}

View File

@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>{% block title %}Untitled page{% endblock %} - MailAccountAdmin</title>
<title>{% block title %}Untitled page{% endblock %} - {{ app_info.getTitle() }}</title>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="stylesheet" href="/static/style.css"/>
</head>
@ -10,27 +10,37 @@
<body>
<header>
<h1>MailAccountAdmin</h1>
<h1>{{ app_info.getTitle() }}</h1>
<div class="header_appversion">
<a href="{{ app_info.getRepositoryUrl() }}">{{ app_info.getVersion() }}</a>
</div>
<div class="header_spacer"></div>
<div class="header_userstatus">
Hello, <b>{{ current_user_name }}</b>. | <a href="/logout">Logout</a>
{% if logged_in | default() %}
Hello, <b>{{ current_user_name }}</b>. | <a href="/logout">Logout</a>
{% else %}
<a href="/login">Login</a>
{% endif %}
</div>
</header>
<nav>
<ul>
{% macro navbar_item(path, text) -%}
<li{% if current_url() == path or current_url() starts with path ~ '/' %} class="nav_current_page"{% endif %}>
<a href="{{ path }}">{{ text }}</a>
</li>
{%- endmacro -%}
{% if logged_in | default() %}
<nav>
<ul>
{% macro navbar_item(path, text) -%}
<li{% if current_url() == path or current_url() starts with path ~ '/' %} class="nav_current_page"{% endif %}>
<a href="{{ path }}">{{ text }}</a>
</li>
{%- endmacro -%}
{{ _self.navbar_item('/', 'Dashboard') }}
{{ _self.navbar_item('/domains', 'Domains') }}
{{ _self.navbar_item('/accounts', 'Accounts') }}
</ul>
</nav>
{{ _self.navbar_item('/', 'Dashboard') }}
{{ _self.navbar_item('/domains', 'Domains') }}
{{ _self.navbar_item('/accounts', 'Accounts') }}
</ul>
</nav>
{% endif %}
<main>
<main{% if main_classes is defined %} class="{{ main_classes }}"{% endif %}>
{% block content %}
Nothing to see here...
{% endblock %}

View File

@ -1,19 +1,9 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Login - MailAccountAdmin</title>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="stylesheet" href="/static/style.css"/>
</head>
{% extends "base.html.twig" %}
<body>
{% block title %}Login{% endblock %}
{% set main_classes = 'login_page' %}
<header>
<h1>MailAccountAdmin</h1>
</header>
<main class="login_page">
{% block content %}
<h2>Login</h2>
<form action="/login" method="POST">
@ -38,7 +28,4 @@
</tr>
</table>
</form>
</main>
</body>
</html>
{% endblock %}