Files
backupscript/src/Twig/AppExtension.php

33 lines
832 B
PHP
Raw Normal View History

2023-05-26 11:47:40 +00:00
<?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')),
);
}
/**
2023-05-26 12:14:21 +00:00
* Format a file size to be human readable
2023-06-01 09:16:19 +00:00
*
2023-05-26 12:14:21 +00:00
* @param int $bytes Number of bytes
2023-06-01 09:16:19 +00:00
* @param int $precision Precision
*
2023-05-26 12:14:21 +00:00
* @return string Formatted string
2023-05-26 11:47:40 +00:00
*/
public function formatBytes($bytes, $precision = 2)
{
$size = ['B','kB','MB','GB','TB','PB','EB','ZB','YB'];
2023-05-31 09:00:20 +00:00
$fact = (int)(floor((strlen((string)$bytes) - 1) / 3));
2023-05-26 12:14:21 +00:00
return sprintf("%.{$precision}f", $bytes / pow(1024, $fact)) . $size[$fact];
2023-05-26 11:47:40 +00:00
}
}