WIP: Implement frontend
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace App\Twig;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Twig\Environment;
|
||||
use Twig\Extension\RuntimeExtensionInterface;
|
||||
|
||||
class AppRuntime implements RuntimeExtensionInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly RequestStack $requestStack,
|
||||
private readonly string $environment
|
||||
) {
|
||||
}
|
||||
|
||||
public function fileIconFilter(Environment $environment, string $mimeType, ?string $classes = 'w-4 h-4'): string
|
||||
{
|
||||
$icon = match ($mimeType) {
|
||||
'application/pdf' => 'pdf',
|
||||
'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'excel',
|
||||
'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'word',
|
||||
default => 'download',
|
||||
};
|
||||
|
||||
return $this->renderIcon($environment, $icon, $classes);
|
||||
}
|
||||
|
||||
public function formatBytes(int $bytes, ?int $precision = 2): string
|
||||
{
|
||||
$size = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
||||
$factor = floor((strlen($bytes) - 1) / 3);
|
||||
|
||||
return sprintf("%.{$precision}f", $bytes / (1024 ** $factor)).@$size[$factor];
|
||||
}
|
||||
|
||||
public function renderIcon(Environment $environment, string $icon, string $classes = 'w-6 h-6'): string
|
||||
{
|
||||
return $environment->render('_partials/_icon.html.twig', [
|
||||
'icon' => $icon,
|
||||
'class' => $classes,
|
||||
]);
|
||||
}
|
||||
|
||||
public function isCurrentRoute(string $route): bool
|
||||
{
|
||||
$requestedRoute = $this->requestStack->getMainRequest()->attributes->get('_route');
|
||||
|
||||
return $requestedRoute === $route;
|
||||
}
|
||||
|
||||
public function renderQaAttribute(string $label, string $value = null): string
|
||||
{
|
||||
if ('test' !== $this->environment) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (null === $value) {
|
||||
return sprintf(' data-qa-%s', strtolower($label));
|
||||
}
|
||||
|
||||
return sprintf(' data-qa-%s="%s"', strtolower($label), $value);
|
||||
}
|
||||
|
||||
public function dateDiffForHumans(\DateTimeInterface $dateTime, \DateTimeInterface $other = null): string
|
||||
{
|
||||
$oldLocale = Carbon::getLocale();
|
||||
$locale = $this->requestStack->getMainRequest()->getLocale();
|
||||
Carbon::setLocale($locale);
|
||||
|
||||
$result = Carbon::instance($dateTime)->diffForHumans($other);
|
||||
|
||||
Carbon::setLocale($oldLocale);
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user