1
0
mirror of https://github.com/ToxicCrack/PrintABrick.git synced 2025-05-29 10:10:25 -07:00

Add Twig extensions

This commit is contained in:
David Hübner 2017-04-10 16:57:47 +02:00
parent 9929956786
commit cf341d03aa
2 changed files with 46 additions and 0 deletions

View File

@ -0,0 +1,21 @@
{#http://stackoverflow.com/a/15303004#}
{% macro bytesToSize(bytes) %}
{% spaceless %}
{% set kilobyte = 1024 %}
{% set megabyte = kilobyte * 1024 %}
{% set gigabyte = megabyte * 1024 %}
{% set terabyte = gigabyte * 1024 %}
{% if bytes < kilobyte %}
{{ bytes ~ ' B' }}
{% elseif bytes < megabyte %}
{{ (bytes / kilobyte)|number_format(2, '.') ~ ' KB' }}
{% elseif bytes < gigabyte %}
{{ (bytes / megabyte)|number_format(2, '.') ~ ' MB' }}
{% elseif bytes < terabyte %}
{{ (bytes / gigabyte)|number_format(2, '.') ~ ' GB' }}
{% else %}
{{ (bytes / terabyte)|number_format(2, '.') ~ ' TB' }}
{% endif %}
{% endspaceless %}
{% endmacro %}

View File

@ -30,6 +30,13 @@ class AppExtension extends \Twig_Extension
];
}
public function getFunctions() {
return [
new \Twig_SimpleFunction('remoteSize', [$this, 'remoteSize']),
new \Twig_SimpleFunction('remoteFilename', [$this, 'remoteFilename']),
];
}
public function partImage(Part $part, Color $color = null)
{
return '/parts/ldraw/'.($color ? $color->getId():'-1').'/'.$part->getNumber().'.png';
@ -39,4 +46,22 @@ class AppExtension extends \Twig_Extension
{
return '/sets/'.strtolower($set->getNumber()).'.jpg';
}
public function remoteSize($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
$data = curl_exec($ch);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
curl_close($ch);
return $size;
}
public function remoteFilename($url) {
return basename($url);
}
}