Files
backupscript/src/Twig/AppExtension.php
2023-06-01 09:16:19 +00:00

33 lines
832 B
PHP

<?php
namespace App\Twig;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
class AppExtension extends AbstractExtension
{
public function getFilters()
{
return array(
new TwigFilter('formatBytes', array($this, 'formatBytes')),
);
}
/**
* Format a file size to be human readable
*
* @param int $bytes Number of bytes
* @param int $precision Precision
*
* @return string Formatted string
*/
public function formatBytes($bytes, $precision = 2)
{
$size = ['B','kB','MB','GB','TB','PB','EB','ZB','YB'];
$fact = (int)(floor((strlen((string)$bytes) - 1) / 3));
return sprintf("%.{$precision}f", $bytes / pow(1024, $fact)) . $size[$fact];
}
}