-
Notifications
You must be signed in to change notification settings - Fork 108
/
twig.ext.php
83 lines (75 loc) · 3.07 KB
/
twig.ext.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/*
GOGGames
Copyright (C) 2018 GoodOldDownloads
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class AppExtension extends \Twig_Extension {
public function getFunctions(){
return array(
new \Twig_SimpleFunction('loadCSS', array($this, 'loadCSS'), array('is_safe' => array('html'))),
new \Twig_SimpleFunction('loadJS', array($this, 'loadJS'), array('is_safe' => array('html'))),
);
}
public function getFilters(){
return array(
new \Twig_SimpleFilter('long2ip', array($this, 'long2ip')),
new \Twig_SimpleFilter('convertBytes', array($this, 'convertBytes')),
);
}
public function long2ip($int){
return long2ip($int);
}
public function convertBytes($bytes, $decimals = 2){
// Jeffrey Sambells
// http://jeffreysambells.com/2012/10/25/human-readable-filesize-php
$size = array('B','KB','MB','GB','TB','PB','EB','ZB','YB');
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) .' '. @$size[$factor];
}
public function loadCSS($styles, $integrity = null){
$ret = array();
if (is_array($styles)) {
foreach ($styles as $key => $value) {
if ($integrity !== null) {
if (!empty($integrity[$key])) {
$integStr = ' integrity="'.$integrity[$key].'"';
} else {
$integStr = '';
}
}
if (file_exists(__DIR__.'/web/'.$value)) {
$ret[] = '<link rel="stylesheet" href="'.$value.'?'.filemtime(__DIR__.'/web/'.$value).'"'.$integStr.'>';
}
}
}
return join($ret, "\n");
}
public function loadJS($scripts, $integrity = null){
$ret = array();
if (is_array($scripts)) {
foreach ($scripts as $key => $value) {
if ($integrity !== null) {
if (!empty($integrity[$key])) {
$integStr = ' integrity="'.$integrity[$key].'"';
} else {
$integStr = '';
}
}
if (file_exists(__DIR__.'/web/'.$value)) {
$ret[] = '<script src="'.$value.'?'.filemtime(__DIR__.'/web/'.$value).'"'.$integStr.'></script>';
}
}
}
return join($ret, "\n");
}
}