Browse Source

add format_ago filter, remove time service

main
ghost 1 year ago
parent
commit
df253ba1b0
  1. 1
      src/Controller/PageController.php
  2. 22
      src/Controller/UserController.php
  3. 61
      src/Service/TimeService.php
  4. 81
      src/Twig/AppExtension.php
  5. 2
      templates/default/user/info.html.twig
  6. 2
      templates/default/user/profile.html.twig

1
src/Controller/PageController.php

@ -12,7 +12,6 @@ use Symfony\Component\HttpFoundation\Request;
use App\Service\UserService; use App\Service\UserService;
use App\Service\PageService; use App\Service\PageService;
use App\Service\TorrentService; use App\Service\TorrentService;
use App\Service\TimeService;
class PageController extends AbstractController class PageController extends AbstractController
{ {

22
src/Controller/UserController.php

@ -9,7 +9,6 @@ use Symfony\Component\HttpFoundation\Request;
use App\Service\ActivityService; use App\Service\ActivityService;
use App\Service\UserService; use App\Service\UserService;
use App\Service\TimeService;
class UserController extends AbstractController class UserController extends AbstractController
{ {
@ -38,8 +37,7 @@ class UserController extends AbstractController
public function index( public function index(
Request $request, Request $request,
ActivityService $activityService, ActivityService $activityService,
UserService $userService, UserService $userService
TimeService $timeService
): Response ): Response
{ {
// Init user session // Init user session
@ -73,9 +71,7 @@ class UserController extends AbstractController
) )
], ],
'type' => 'join', 'type' => 'join',
'added' => $timeService->ago( 'added' => $activity->getAdded()
$activity->getAdded()
)
]; ];
} }
*/ */
@ -100,8 +96,7 @@ class UserController extends AbstractController
)] )]
public function profile( public function profile(
Request $request, Request $request,
UserService $userService, UserService $userService
TimeService $timeService
): Response ): Response
{ {
// Init user // Init user
@ -161,9 +156,7 @@ class UserController extends AbstractController
'status' => $user->isStatus(), 'status' => $user->isStatus(),
'locale' => $user->getLocale(), 'locale' => $user->getLocale(),
'locales' => $user->getLocales(), 'locales' => $user->getLocales(),
'added' => $timeService->ago( 'added' => $user->getAdded(),
$user->getAdded()
),
'identicon' => $userService->identicon( 'identicon' => $userService->identicon(
$user->getAddress(), $user->getAddress(),
48 48
@ -187,8 +180,7 @@ class UserController extends AbstractController
public function info( public function info(
int $id, int $id,
Request $request, Request $request,
UserService $userService, UserService $userService): Response
TimeService $timeService): Response
{ {
// Init user // Init user
if (!$user = $userService->get($id)) if (!$user = $userService->get($id))
@ -208,9 +200,7 @@ class UserController extends AbstractController
'status' => $user->isStatus(), 'status' => $user->isStatus(),
'locale' => $user->getLocale(), 'locale' => $user->getLocale(),
'locales' => $user->getLocales(), 'locales' => $user->getLocales(),
'added' => $timeService->ago( 'added' => $user->getAdded(),
$user->getAdded()
),
'identicon' => $userService->identicon( 'identicon' => $userService->identicon(
$user->getAddress(), $user->getAddress(),
48 48

61
src/Service/TimeService.php

@ -1,61 +0,0 @@
<?php
namespace App\Service;
use Symfony\Contracts\Translation\TranslatorInterface;
class TimeService
{
private TranslatorInterface $translator;
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
public function ago(int $time): string
{
$diff = time() - $time;
if ($diff < 1)
{
return $this->translator->trans('now');
}
$values =
[
365 * 24 * 60 * 60 => $this->translator->trans('year'),
30 * 24 * 60 * 60 => $this->translator->trans('month'),
24 * 60 * 60 => $this->translator->trans('day'),
60 * 60 => $this->translator->trans('hour'),
60 => $this->translator->trans('minute'),
1 => $this->translator->trans('second')
];
$plural = [
$this->translator->trans('year') => $this->translator->trans('years'),
$this->translator->trans('month') => $this->translator->trans('months'),
$this->translator->trans('day') => $this->translator->trans('days'),
$this->translator->trans('hour') => $this->translator->trans('hours'),
$this->translator->trans('minute') => $this->translator->trans('minutes'),
$this->translator->trans('second') => $this->translator->trans('seconds')
];
foreach ($values as $key => $value)
{
$result = $diff / $key;
if ($result >= 1)
{
$round = round($result);
return sprintf(
'%s %s %s',
$round,
$round > 1 ? $plural[$value] : $value,
$this->translator->trans('ago')
);
}
}
}
}

81
src/Twig/AppExtension.php

@ -3,18 +3,22 @@
namespace App\Twig; namespace App\Twig;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Extension\AbstractExtension; use Twig\Extension\AbstractExtension;
use Twig\TwigFilter; use Twig\TwigFilter;
class AppExtension extends AbstractExtension class AppExtension extends AbstractExtension
{ {
protected $container; protected ContainerInterface $container;
protected TranslatorInterface $translator;
public function __construct( public function __construct(
ContainerInterface $container ContainerInterface $container,
TranslatorInterface $translator
) )
{ {
$this->container = $container; $this->container = $container;
$this->translator = $translator;
} }
public function getFilters() public function getFilters()
@ -28,6 +32,13 @@ class AppExtension extends AbstractExtension
'formatBytes' 'formatBytes'
] ]
), ),
new TwigFilter(
'format_ago',
[
$this,
'formatAgo'
]
),
]; ];
} }
@ -37,19 +48,67 @@ class AppExtension extends AbstractExtension
) : string ) : string
{ {
$size = [ $size = [
'B', $this->translator->trans('B'),
'Kb', $this->translator->trans('Kb'),
'Mb', $this->translator->trans('Mb'),
'Gb', $this->translator->trans('Gb'),
'Tb', $this->translator->trans('Tb'),
'Pb', $this->translator->trans('Pb'),
'Eb', $this->translator->trans('Eb'),
'Zb', $this->translator->trans('Zb'),
'Yb' $this->translator->trans('Yb')
]; ];
$factor = floor((strlen($bytes) - 1) / 3); $factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$precision}f", $bytes / pow(1024, $factor)) . ' ' . @$size[$factor]; return sprintf("%.{$precision}f", $bytes / pow(1024, $factor)) . ' ' . @$size[$factor];
} }
public function formatAgo(
int $time,
) : string
{
$diff = time() - $time;
if ($diff < 1)
{
return $this->translator->trans('now');
}
$values =
[
365 * 24 * 60 * 60 => $this->translator->trans('year'),
30 * 24 * 60 * 60 => $this->translator->trans('month'),
24 * 60 * 60 => $this->translator->trans('day'),
60 * 60 => $this->translator->trans('hour'),
60 => $this->translator->trans('minute'),
1 => $this->translator->trans('second')
];
$plural = [
$this->translator->trans('year') => $this->translator->trans('years'),
$this->translator->trans('month') => $this->translator->trans('months'),
$this->translator->trans('day') => $this->translator->trans('days'),
$this->translator->trans('hour') => $this->translator->trans('hours'),
$this->translator->trans('minute') => $this->translator->trans('minutes'),
$this->translator->trans('second') => $this->translator->trans('seconds')
];
foreach ($values as $key => $value)
{
$result = $diff / $key;
if ($result >= 1)
{
$round = round($result);
return sprintf(
'%s %s %s',
$round,
$round > 1 ? $plural[$value] : $value,
$this->translator->trans('ago')
);
}
}
}
} }

2
templates/default/user/info.html.twig

@ -13,7 +13,7 @@
</tr> </tr>
<tr> <tr>
<td class="padding-t-16-px">{{ 'Joined'|trans }}</td> <td class="padding-t-16-px">{{ 'Joined'|trans }}</td>
<td class="padding-t-16-px">{{ user.added }}</td> <td class="padding-t-16-px">{{ user.added | format_ago }}</td>
</tr> </tr>
<tr> <tr>
<td class="padding-b-8-px border-bottom-default text-right" colspan="2">{{ 'Access'|trans }}</td> <td class="padding-b-8-px border-bottom-default text-right" colspan="2">{{ 'Access'|trans }}</td>

2
templates/default/user/profile.html.twig

@ -28,7 +28,7 @@
</tr> </tr>
<tr> <tr>
<td>{{ 'Joined'|trans }}</td> <td>{{ 'Joined'|trans }}</td>
<td>{{ user.added }}</td> <td>{{ user.added | format_ago }}</td>
</tr> </tr>
<tr> <tr>
<td class="padding-b-8-px border-bottom-default text-right" colspan="2">{{ 'Access'|trans }}</td> <td class="padding-b-8-px border-bottom-default text-right" colspan="2">{{ 'Access'|trans }}</td>

Loading…
Cancel
Save