Created
December 2, 2021 10:57
-
-
Save smichaelsen/68fdb677d7a6eb7fc293deafb44ea60a to your computer and use it in GitHub Desktop.
Show project version in TYPO3 backend
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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