Skip to content

Commit

Permalink
Add the format_bytes filter for Contao 4.13 (#393)
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp authored Jan 17, 2025
1 parent 54878b4 commit 27e34ed
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
2 changes: 2 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Codefog\HasteBundle\StringParser;
use Contao\CoreBundle\Framework\ContaoFramework;
use Symfony\Contracts\Translation\TranslatorInterface;
use Terminal42\NotificationCenterBundle\Backend\AutoSuggester;
use Terminal42\NotificationCenterBundle\BulkyItem\BulkyItemStorage;
Expand Down Expand Up @@ -88,6 +89,7 @@
$services->set(NotificationCenterExtension::class);
$services->set(NotificationCenterRuntime::class)
->args([
service(ContaoFramework::class),
service(BulkyItemStorage::class),
])
;
Expand Down
18 changes: 18 additions & 0 deletions src/Twig/NotificationCenterExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Terminal42\NotificationCenterBundle\Twig;

use Contao\CoreBundle\Twig\Runtime\FormatterRuntime;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;

class NotificationCenterExtension extends AbstractExtension
Expand All @@ -15,4 +17,20 @@ public function getFunctions(): array
new TwigFunction('notification_center_file_url', [NotificationCenterRuntime::class, 'fileUrl']),
];
}

public function getFilters(): array
{
// This class only exists in Contao 5 and provides the format_bytes filter.
if (class_exists(FormatterRuntime::class)) {
return [];
}

return [
new TwigFilter(
'format_bytes',
[NotificationCenterRuntime::class, 'formatBytes'],
['is_safe' => ['html']],
),
];
}
}
18 changes: 16 additions & 2 deletions src/Twig/NotificationCenterRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,31 @@

namespace Terminal42\NotificationCenterBundle\Twig;

use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\System;
use Terminal42\NotificationCenterBundle\BulkyItem\BulkyItemStorage;
use Twig\Extension\RuntimeExtensionInterface;

final class NotificationCenterRuntime implements RuntimeExtensionInterface
{
public function __construct(private readonly BulkyItemStorage $bulkyItemStorage)
{
public function __construct(
private readonly ContaoFramework $framework,
private readonly BulkyItemStorage $bulkyItemStorage,
) {
}

public function fileUrl(string $voucher, int|null $ttl = null): string
{
return $this->bulkyItemStorage->generatePublicUri($voucher, $ttl);
}

/**
* Convert a byte value into a human-readable format.
*/
public function formatBytes(int $bytes, int $decimals = 1): string
{
$this->framework->initialize();

return System::getReadableSize($bytes, $decimals);
}
}

0 comments on commit 27e34ed

Please sign in to comment.