1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sludio\HelperBundle\Script\Twig; |
4
|
|
|
|
5
|
|
|
class BeautifyExtension extends \Twig_Extension |
6
|
|
|
{ |
7
|
|
|
use TwigTrait; |
8
|
|
|
|
9
|
|
|
protected $request; |
10
|
|
|
|
11
|
|
|
public function __construct($requestStack, $shortFunctions) |
12
|
|
|
{ |
13
|
|
|
$this->request = $requestStack->getCurrentRequest(); |
14
|
|
|
$this->shortFunctions = $shortFunctions; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public function getName() |
18
|
|
|
{ |
19
|
|
|
return 'sludio_helper.twig.beautify_extension'; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function getFilters() |
23
|
|
|
{ |
24
|
|
|
$input = [ |
25
|
|
|
'beautify' => 'beautify', |
26
|
|
|
'urldecode' => 'url_decode', |
27
|
|
|
'parse' => 'parse', |
28
|
|
|
'file_exists' => 'file_exists', |
29
|
|
|
'html_entity_decode' => 'html_entity_decode', |
30
|
|
|
'strip_descr' => 'strip_descr', |
31
|
|
|
'pretty_print' => 'prettyPrint', |
32
|
|
|
'status_code_class' => 'statusCodeClass', |
33
|
|
|
'format_duration' => 'formatDuration', |
34
|
|
|
'short_uri' => 'shorthenUri', |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
return $this->makeArray($input); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getFunctions() |
41
|
|
|
{ |
42
|
|
|
$input = [ |
43
|
|
|
'detect_lang' => 'detectLang', |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
return $this->makeArray($input, 'function'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function url_decode($string) |
50
|
|
|
{ |
51
|
|
|
return urldecode($string); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function parse($string) |
55
|
|
|
{ |
56
|
|
|
$str = parse_url($string); |
57
|
|
|
|
58
|
|
|
$argv = []; |
59
|
|
|
if (isset($str['query'])) { |
60
|
|
|
$args = explode('&', $str['query']); |
61
|
|
|
|
62
|
|
|
foreach ($args as $arg) { |
63
|
|
|
$tmp = explode('=', $arg, 2); |
64
|
|
|
$argv[$tmp[0]] = $tmp[1]; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $argv; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function file_exists($file) |
72
|
|
|
{ |
73
|
|
|
return file_exists(getcwd().$file); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function beautify($string) |
77
|
|
|
{ |
78
|
|
|
$explode = explode('/', strip_tags($string)); |
79
|
|
|
$string = implode(' / ', $explode); |
80
|
|
|
|
81
|
|
|
return $string; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function html_entity_decode($str) |
85
|
|
|
{ |
86
|
|
|
$str = html_entity_decode($str); |
87
|
|
|
$str = preg_replace('#\R+#', '', $str); |
88
|
|
|
|
89
|
|
|
return $str; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function strip_descr($body, $fallback = null, $type = null, $lengthIn = 300) |
93
|
|
|
{ |
94
|
|
|
if ($type && $fallback) { |
95
|
|
|
$length = isset($fallback[$type]) ? $fallback[$type] : null; |
96
|
|
|
} |
97
|
|
|
if (!isset($length)) { |
98
|
|
|
$length = $lengthIn; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (strlen($body) > $length) { |
102
|
|
|
$body = substr($body, 0, strpos($body, ' ', $length)).'...'; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $body; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function detectLang($body) |
109
|
|
|
{ |
110
|
|
|
switch (true) { |
111
|
|
|
case 0 === strpos($body, '<?xml'): |
112
|
|
|
return 'xml'; |
113
|
|
|
case 0 === strpos($body, '{'): |
114
|
|
|
case 0 === strpos($body, '['): |
115
|
|
|
return 'json'; |
116
|
|
|
default: |
117
|
|
|
return 'markup'; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function prettyPrint($code, $lang) |
122
|
|
|
{ |
123
|
|
|
switch ($lang) { |
124
|
|
|
case 'json': |
125
|
|
|
return json_encode(json_decode($code), JSON_PRETTY_PRINT); |
126
|
|
|
case 'xml': |
127
|
|
|
$xml = new \DomDocument('1.0'); |
128
|
|
|
$xml->preserveWhiteSpace = false; |
129
|
|
|
$xml->formatOutput = true; |
130
|
|
|
$xml->loadXml($code); |
131
|
|
|
|
132
|
|
|
return $xml->saveXml(); |
133
|
|
|
default: |
134
|
|
|
return $code; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function statusCodeClass($statusCode) |
139
|
|
|
{ |
140
|
|
|
$codes = [ |
141
|
|
|
5 => 'server-error', |
142
|
|
|
4 => 'client-error', |
143
|
|
|
3 => 'redirect', |
144
|
|
|
2 => 'success', |
145
|
|
|
1 => 'info', |
146
|
|
|
]; |
147
|
|
|
$code = (int)floor(intval($statusCode) / 100); |
148
|
|
|
|
149
|
|
|
return isset($codes[$code]) ? $codes[$code] : 'unknown'; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function formatDuration($seconds) |
153
|
|
|
{ |
154
|
|
|
$formats = [ |
155
|
|
|
'%.2f s', |
156
|
|
|
'%d ms', |
157
|
|
|
'%d µs', |
158
|
|
|
]; |
159
|
|
|
|
160
|
|
|
while ($format = array_shift($formats)) { |
161
|
|
|
if ($seconds > 1) { |
162
|
|
|
break; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$seconds *= 1000; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return sprintf($format, $seconds); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function shortenUri($uri) |
172
|
|
|
{ |
173
|
|
|
$parts = parse_url($uri); |
174
|
|
|
|
175
|
|
|
return sprintf('%s://%s%s', isset($parts['scheme']) ? $parts['scheme'] : 'http', $parts['host'], isset($parts['port']) ? (':'.$parts['port']) : ''); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
} |
179
|
|
|
|