|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class Xhgui_Twig_Extension extends Twig_Extension |
|
4
|
|
|
{ |
|
5
|
|
|
protected $_app; |
|
6
|
|
|
|
|
7
|
|
|
public function __construct($app) |
|
8
|
|
|
{ |
|
9
|
|
|
$this->_app = $app; |
|
10
|
|
|
} |
|
11
|
|
|
|
|
12
|
|
|
public function getName() |
|
13
|
|
|
{ |
|
14
|
|
|
return 'xhgui'; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function getFunctions() |
|
18
|
|
|
{ |
|
19
|
|
|
return array( |
|
|
|
|
|
|
20
|
|
|
'url' => new Twig_Function_Method($this, 'url'), |
|
|
|
|
|
|
21
|
|
|
'static' => new Twig_Function_Method($this, 'staticUrl'), |
|
|
|
|
|
|
22
|
|
|
'percent' => new Twig_Function_Method($this, 'makePercent', array( |
|
|
|
|
|
|
23
|
|
|
'is_safe' => array('html') |
|
24
|
|
|
)), |
|
25
|
|
|
); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function getFilters() |
|
29
|
|
|
{ |
|
30
|
|
|
return array( |
|
|
|
|
|
|
31
|
|
|
'simple_url' => new Twig_Filter_Function('Xhgui_Util::simpleUrl'), |
|
|
|
|
|
|
32
|
|
|
'as_bytes' => new Twig_Filter_Method($this, 'formatBytes', array('is_safe' => array('html'))), |
|
|
|
|
|
|
33
|
|
|
'as_time' => new Twig_Filter_Method($this, 'formatTime', array('is_safe' => array('html'))), |
|
|
|
|
|
|
34
|
|
|
'as_diff' => new Twig_Filter_Method($this, 'formatDiff', array('is_safe' => array('html'))), |
|
|
|
|
|
|
35
|
|
|
'as_percent' => new Twig_Filter_Method($this, 'formatPercent', array('is_safe' => array('html'))), |
|
|
|
|
|
|
36
|
|
|
'truncate' => new Twig_Filter_Method($this, 'truncate'), |
|
|
|
|
|
|
37
|
|
|
); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
protected function _getBase() |
|
41
|
|
|
{ |
|
42
|
|
|
$base = dirname($_SERVER['PHP_SELF']); |
|
43
|
|
|
if ($base == '/') { |
|
44
|
|
|
return ''; |
|
45
|
|
|
} |
|
46
|
|
|
return $base; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function truncate($input, $length = 50) |
|
50
|
|
|
{ |
|
51
|
|
|
if (strlen($input) < $length) { |
|
52
|
|
|
return $input; |
|
53
|
|
|
} |
|
54
|
|
|
return substr($input, 0, $length) . "\xe2\x80\xa6"; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get a URL for xhgui. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $path The file/path you want a link to |
|
|
|
|
|
|
61
|
|
|
* @param array $queryarg Additional querystring arguments. |
|
|
|
|
|
|
62
|
|
|
* @return string url. |
|
63
|
|
|
*/ |
|
64
|
|
|
public function url($name, $queryargs = array()) |
|
65
|
|
|
{ |
|
66
|
|
|
$query = ''; |
|
67
|
|
|
if (!empty($queryargs)) { |
|
68
|
|
|
$query = '?' . http_build_query($queryargs); |
|
69
|
|
|
} |
|
70
|
|
|
return $this->_app->urlFor($name) . $query; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get the URL for static content relative to webroot |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $path The file/path you want a link to |
|
|
|
|
|
|
77
|
|
|
* @return string url. |
|
78
|
|
|
*/ |
|
79
|
|
|
public function staticUrl($url) |
|
80
|
|
|
{ |
|
81
|
|
|
$rootUri = $this->_app->request()->getRootUri(); |
|
82
|
|
|
|
|
83
|
|
|
// Get URL part prepending index.php |
|
84
|
|
|
$indexPos = strpos($rootUri, 'index.php'); |
|
85
|
|
|
if ($indexPos > 0) { |
|
86
|
|
|
return substr($rootUri, 0, $indexPos) . $url; |
|
87
|
|
|
} |
|
88
|
|
|
return $rootUri . '/' . $url; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function formatBytes($value) |
|
92
|
|
|
{ |
|
93
|
|
|
return number_format((float)$value) . ' <span class="units">bytes</span>'; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function formatTime($value) |
|
97
|
|
|
{ |
|
98
|
|
|
return number_format((float)$value) . ' <span class="units">µs</span>'; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function formatDiff($value) |
|
102
|
|
|
{ |
|
103
|
|
|
$class = 'diff-same'; |
|
|
|
|
|
|
104
|
|
|
$class = $value > 0 ? 'diff-up' : 'diff-down'; |
|
105
|
|
|
if ($value == 0) { |
|
106
|
|
|
$class = 'diff-same'; |
|
107
|
|
|
} |
|
108
|
|
|
return sprintf( |
|
109
|
|
|
'<span class="%s">%s</span>', |
|
110
|
|
|
$class, |
|
111
|
|
|
number_format((float)$value) |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function makePercent($value, $total) |
|
116
|
|
|
{ |
|
117
|
|
|
$value = (false === empty($total)) ? $value / $total : 0; |
|
118
|
|
|
return $this->formatPercent($value); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function formatPercent($value) |
|
122
|
|
|
{ |
|
123
|
|
|
return number_format((float)$value * 100, 0) . ' <span class="units">%</span>'; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.