1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace XHGui\Twig; |
4
|
|
|
|
5
|
|
|
use Slim\App; |
6
|
|
|
use Slim\Router; |
7
|
|
|
use Twig_Extension; |
8
|
|
|
use Twig_Filter_Method; |
9
|
|
|
use Twig_Function_Method; |
10
|
|
|
|
11
|
|
|
class XHGuiTwigExtension extends Twig_Extension |
12
|
|
|
{ |
13
|
|
|
/** @var App */ |
14
|
|
|
protected $_app; |
15
|
|
|
/** @var Router */ |
16
|
|
|
private $router; |
17
|
|
|
/** @var string */ |
18
|
|
|
private $pathPrefix; |
19
|
|
|
|
20
|
|
|
public function __construct(App $app) |
21
|
|
|
{ |
22
|
|
|
$this->_app = $app; |
23
|
|
|
$this->router = $app->router(); |
|
|
|
|
24
|
|
|
$this->pathPrefix = $app->config('path.prefix'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getName() |
28
|
|
|
{ |
29
|
|
|
return 'xhgui'; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function getFunctions() |
33
|
|
|
{ |
34
|
|
|
return [ |
|
|
|
|
35
|
|
|
'url' => new Twig_Function_Method($this, 'url'), |
|
|
|
|
36
|
|
|
'static' => new Twig_Function_Method($this, 'staticUrl'), |
|
|
|
|
37
|
|
|
'percent' => new Twig_Function_Method($this, 'makePercent', [ |
|
|
|
|
38
|
|
|
'is_safe' => ['html'], |
39
|
|
|
]), |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getFilters() |
44
|
|
|
{ |
45
|
|
|
return [ |
|
|
|
|
46
|
|
|
'as_bytes' => new Twig_Filter_Method($this, 'formatBytes', ['is_safe' => ['html']]), |
|
|
|
|
47
|
|
|
'as_time' => new Twig_Filter_Method($this, 'formatTime', ['is_safe' => ['html']]), |
|
|
|
|
48
|
|
|
'as_diff' => new Twig_Filter_Method($this, 'formatDiff', ['is_safe' => ['html']]), |
|
|
|
|
49
|
|
|
'as_percent' => new Twig_Filter_Method($this, 'formatPercent', ['is_safe' => ['html']]), |
|
|
|
|
50
|
|
|
'truncate' => new Twig_Filter_Method($this, 'truncate'), |
|
|
|
|
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function truncate($input, $length = 50) |
55
|
|
|
{ |
56
|
|
|
if (strlen($input) < $length) { |
57
|
|
|
return $input; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return substr($input, 0, $length) . "\xe2\x80\xa6"; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get a URL for xhgui. |
65
|
|
|
* |
66
|
|
|
* @param string $name The file/path you want a link to |
67
|
|
|
* @param array $queryargs additional querystring arguments |
68
|
|
|
* @return string url |
69
|
|
|
*/ |
70
|
|
|
public function url($name, $queryargs = []) |
71
|
|
|
{ |
72
|
|
|
$query = ''; |
73
|
|
|
if (!empty($queryargs)) { |
74
|
|
|
$query = '?' . http_build_query($queryargs); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// this is copy of \Slim\Slim::urlFor() to mix path prefix in |
78
|
|
|
// \Slim\Slim::urlFor |
79
|
|
|
|
80
|
|
|
return rtrim($this->pathPrefix(), '/') . $this->router->urlFor($name) . $query; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get the URL for static content relative to webroot |
85
|
|
|
* |
86
|
|
|
* @param string $path The file/path you want a link to |
87
|
|
|
* @return string url |
88
|
|
|
*/ |
89
|
|
|
public function staticUrl($path) |
90
|
|
|
{ |
91
|
|
|
$rootUri = $this->pathPrefix(); |
92
|
|
|
|
93
|
|
|
return rtrim($rootUri, '/') . '/' . $path; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function formatBytes($value) |
97
|
|
|
{ |
98
|
|
|
return number_format((float)$value) . ' <span class="units">bytes</span>'; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function formatTime($value) |
102
|
|
|
{ |
103
|
|
|
return number_format((float)$value) . ' <span class="units">µs</span>'; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function formatDiff($value) |
107
|
|
|
{ |
108
|
|
|
$class = $value > 0 ? 'diff-up' : 'diff-down'; |
109
|
|
|
if ($value == 0) { |
110
|
|
|
$class = 'diff-same'; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return sprintf( |
114
|
|
|
'<span class="%s">%s</span>', |
115
|
|
|
$class, |
116
|
|
|
number_format((float)$value) |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function makePercent($value, $total) |
121
|
|
|
{ |
122
|
|
|
$value = (false === empty($total)) ? $value / $total : 0; |
123
|
|
|
|
124
|
|
|
return $this->formatPercent($value); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function formatPercent($value) |
128
|
|
|
{ |
129
|
|
|
return number_format((float)$value * 100, 0) . ' <span class="units">%</span>'; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
private function pathPrefix() |
136
|
|
|
{ |
137
|
|
|
if ($this->pathPrefix !== null) { |
138
|
|
|
return $this->pathPrefix; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$request = $this->_app->request(); |
|
|
|
|
142
|
|
|
$rootUri = $request->getRootUri(); |
143
|
|
|
|
144
|
|
|
// Get URL part prepending index.php |
145
|
|
|
$indexPos = strpos($rootUri, 'index.php'); |
146
|
|
|
if ($indexPos > 0) { |
147
|
|
|
return substr($rootUri, 0, $indexPos); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $rootUri; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.