mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-08-08 20:25:13 +02:00
Improved organisation of Services
This commit is contained in:
parent
c3308aaf24
commit
a4eae19a1f
56 changed files with 100 additions and 97 deletions
94
src/Services/Misc/FAIconGenerator.php
Normal file
94
src/Services/Misc/FAIconGenerator.php
Normal file
|
@ -0,0 +1,94 @@
|
|||
<?php
|
||||
/**
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\Misc;
|
||||
|
||||
use App\Entity\Attachments\Attachment;
|
||||
use function in_array;
|
||||
use InvalidArgumentException;
|
||||
|
||||
class FAIconGenerator
|
||||
{
|
||||
protected const EXT_MAPPING = [
|
||||
'fa-file-pdf' => ['pdf'],
|
||||
'fa-file-image' => Attachment::PICTURE_EXTS,
|
||||
'fa-file-alt' => ['txt', 'md', 'rtf', 'log', 'rst', 'tex'],
|
||||
'fa-file-csv' => ['csv'],
|
||||
'fa-file-word' => ['doc', 'docx', 'odt'],
|
||||
'fa-file-archive' => ['zip', 'rar', 'bz2', 'tar', '7z', 'gz'],
|
||||
'fa-file-audio' => ['mp3', 'wav', 'aac', 'm4a', 'wma'],
|
||||
'fa-file-powerpoint' => ['ppt', 'pptx', 'odp', 'pps', 'key'],
|
||||
'fa-file-excel' => ['xls', 'xlr', 'xlsx', 'ods'],
|
||||
'fa-file-code' => ['php', 'xml', 'html', 'js', 'ts', 'htm', 'c', 'cpp'],
|
||||
'fa-file-video' => ['webm', 'avi', 'mp4', 'mkv', 'wmv'],
|
||||
];
|
||||
|
||||
/**
|
||||
* Gets the Font awesome icon class for a file with the specified extension.
|
||||
* For example 'pdf' gives you 'fa-file-pdf'.
|
||||
*
|
||||
* @param string $extension The file extension (without dot). Must be ASCII chars only!
|
||||
*
|
||||
* @return string The fontawesome class with leading 'fa-'
|
||||
*/
|
||||
public function fileExtensionToFAType(string $extension): string
|
||||
{
|
||||
if ('' === $extension) {
|
||||
throw new InvalidArgumentException('You must specify an extension!');
|
||||
}
|
||||
//Normalize file extension
|
||||
$extension = strtolower($extension);
|
||||
foreach (self::EXT_MAPPING as $fa => $exts) {
|
||||
if (in_array($extension, $exts, true)) {
|
||||
return $fa;
|
||||
}
|
||||
}
|
||||
|
||||
// When the extension is not found in the mapping array, we return the generic icon
|
||||
return 'fa-file';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns HTML code to show the given fontawesome icon.
|
||||
* E.g. <i class="fas fa-file-text"></i>.
|
||||
*
|
||||
* @param string $icon_class The icon which should be shown (e.g. fa-file-text)
|
||||
* @param string $style The style of the icon 'fas'
|
||||
* @param string $options any other css class attributes like size, etc
|
||||
*
|
||||
* @return string The final html
|
||||
*/
|
||||
public function generateIconHTML(string $icon_class, string $style = 'fa-solid', string $options = ''): string
|
||||
{
|
||||
//XSS protection
|
||||
$icon_class = htmlspecialchars($icon_class);
|
||||
$style = htmlspecialchars($style);
|
||||
$options = htmlspecialchars($options);
|
||||
|
||||
return sprintf(
|
||||
'<i class="%s %s %s"></i>',
|
||||
$style,
|
||||
$icon_class,
|
||||
$options
|
||||
);
|
||||
}
|
||||
}
|
83
src/Services/Misc/GitVersionInfo.php
Normal file
83
src/Services/Misc/GitVersionInfo.php
Normal file
|
@ -0,0 +1,83 @@
|
|||
<?php
|
||||
/**
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\Misc;
|
||||
|
||||
use Symfony\Component\HttpKernel\KernelInterface;
|
||||
|
||||
class GitVersionInfo
|
||||
{
|
||||
protected string $project_dir;
|
||||
|
||||
public function __construct(KernelInterface $kernel)
|
||||
{
|
||||
$this->project_dir = $kernel->getProjectDir();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Git branch name of the installed system.
|
||||
*
|
||||
* @return string|null The current git branch name. Null, if this is no Git installation
|
||||
*/
|
||||
public function getGitBranchName(): ?string
|
||||
{
|
||||
if (is_file($this->project_dir.'/.git/HEAD')) {
|
||||
$git = file($this->project_dir.'/.git/HEAD');
|
||||
$head = explode('/', $git[0], 3);
|
||||
|
||||
if (!isset($head[2])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return trim($head[2]);
|
||||
}
|
||||
|
||||
return null; // this is not a Git installation
|
||||
}
|
||||
|
||||
/**
|
||||
* Get hash of the last git commit (on remote "origin"!).
|
||||
*
|
||||
* If this method does not work, try to make a "git pull" first!
|
||||
*
|
||||
* @param int $length if this is smaller than 40, only the first $length characters will be returned
|
||||
*
|
||||
* @return string|null The hash of the last commit, null If this is no Git installation
|
||||
*/
|
||||
public function getGitCommitHash(int $length = 7): ?string
|
||||
{
|
||||
$filename = $this->project_dir.'/.git/refs/remotes/origin/'.$this->getGitBranchName();
|
||||
if (is_file($filename)) {
|
||||
$head = file($filename);
|
||||
|
||||
if (!isset($head[0])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$hash = $head[0];
|
||||
|
||||
return substr($hash, 0, $length);
|
||||
}
|
||||
|
||||
return null; // this is not a Git installation
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue