Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save smichaelsen/68fdb677d7a6eb7fc293deafb44ea60a to your computer and use it in GitHub Desktop.
Save smichaelsen/68fdb677d7a6eb7fc293deafb44ea60a to your computer and use it in GitHub Desktop.
Show project version in TYPO3 backend
<?php
declare(strict_types=1);
namespace MyVendor\Sitepackage\EventListener;
use TYPO3\CMS\Backend\Backend\Event\SystemInformationToolbarCollectorEvent;
use TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Utility\CommandUtility;
/**
* Prints the current code version into the System Information Toolbar.
* The current version is read from
* * the PROJECT_VERSION env variable or
* * the currently checked out git branch.
*/
class AddProjectVersionToSystemInformationToolbar
{
public function __invoke(SystemInformationToolbarCollectorEvent $event): SystemInformationToolbarCollectorEvent
{
$this->addProjectVersion($event->getToolbarItem());
return $event;
}
protected function addProjectVersion(SystemInformationToolbarItem $systemInformationToolbarItem): void
{
$projectVersion = null;
if (!empty(getenv('PROJECT_VERSION'))) {
$projectVersion = getenv('PROJECT_VERSION');
} elseif (is_array($currentMessage = self::runGitcommand('git branch | grep \*'))) {
$projectVersion = $currentMessage[0];
if (strpos($projectVersion, '* ') === 0) {
$projectVersion = substr($projectVersion, 2);
}
}
if (empty($projectVersion)) {
return;
}
$systemInformationToolbarItem->addSystemInformation(
'Project Version',
$projectVersion,
'mimetypes-x-sys_category'
);
}
protected function runGitcommand(string $command): array
{
$path = Environment::getPublicPath() . '/';
CommandUtility::exec('cd ' . $path . ' &&' . $command, $output);
return $output;
}
}
services:
MyVendor\Sitepackage\EventListener\AddProjectVersionToSystemInformationToolbar:
tags:
- name: event.listener
identifier: 'sitepackage/addProjectVersionToSystemInformationToolbar'
event: TYPO3\CMS\Backend\Backend\Event\SystemInformationToolbarCollectorEvent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment