|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sludio\HelperBundle\Script\Twig; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
6
|
|
|
|
|
7
|
|
|
class SludioExtension extends \Twig_Extension |
|
8
|
|
|
{ |
|
9
|
|
|
use TwigTrait; |
|
10
|
|
|
|
|
11
|
|
|
const INFO = 'info'; |
|
12
|
|
|
const SUCCESS = 'success'; |
|
13
|
|
|
const REDIRECT = 'redirect'; |
|
14
|
|
|
const CLIENT = 'client_error'; |
|
15
|
|
|
const SERVER = 'server_error'; |
|
16
|
|
|
|
|
17
|
|
|
protected $appDir; |
|
18
|
|
|
private $paths = []; |
|
19
|
|
|
protected $param; |
|
20
|
|
|
protected $order; |
|
21
|
|
|
|
|
22
|
|
|
public $detector; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct($shortFunctions) |
|
25
|
|
|
{ |
|
26
|
|
|
global $kernel; |
|
27
|
|
|
|
|
28
|
|
|
$this->shortFunctions = $shortFunctions; |
|
29
|
|
|
$this->appDir = $kernel->getRootDir(); |
|
30
|
|
|
$this->detector = new \Mobile_Detect(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function getName() |
|
34
|
|
|
{ |
|
35
|
|
|
return 'sludio_helper.twig.extension'; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function getFilters() |
|
39
|
|
|
{ |
|
40
|
|
|
$input = [ |
|
41
|
|
|
'beautify' => 'beautify', |
|
42
|
|
|
'urldecode' => 'urlDecode', |
|
43
|
|
|
'parse' => 'parse', |
|
44
|
|
|
'file_exists' => 'fileExists', |
|
45
|
|
|
'html_entity_decode' => 'htmlEntityDecode', |
|
46
|
|
|
'strip_descr' => 'stripDescr', |
|
47
|
|
|
'pretty_print' => 'prettyPrint', |
|
48
|
|
|
'status_code_class' => 'statusCodeClass', |
|
49
|
|
|
'format_duration' => 'formatDuration', |
|
50
|
|
|
'short_uri' => 'shorthenUri', |
|
51
|
|
|
'is_ie' => 'isIE', |
|
52
|
|
|
'asset_version' => 'getAssetVersion', |
|
53
|
|
|
'usort' => 'usortFunction', |
|
54
|
|
|
]; |
|
55
|
|
|
|
|
56
|
|
|
return $this->makeArray($input); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function getFunctions() |
|
60
|
|
|
{ |
|
61
|
|
|
$input = [ |
|
62
|
|
|
'detect_lang' => 'detectLang', |
|
63
|
|
|
'is_mobile' => [ |
|
64
|
|
|
$this->detector, |
|
65
|
|
|
'isMobile', |
|
66
|
|
|
], |
|
67
|
|
|
'is_tablet' => [ |
|
68
|
|
|
$this->detector, |
|
69
|
|
|
'isTablet', |
|
70
|
|
|
], |
|
71
|
|
|
]; |
|
72
|
|
|
|
|
73
|
|
|
return $this->makeArray($input, 'function'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function urlDecode($string) |
|
77
|
|
|
{ |
|
78
|
|
|
return urldecode($string); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function parse($string) |
|
82
|
|
|
{ |
|
83
|
|
|
$str = parse_url($string); |
|
84
|
|
|
|
|
85
|
|
|
$arguments = []; |
|
86
|
|
|
if (isset($str['query'])) { |
|
87
|
|
|
$args = explode('&', $str['query']); |
|
88
|
|
|
|
|
89
|
|
|
foreach ($args as $arg) { |
|
90
|
|
|
$tmp = explode('=', $arg, 2); |
|
91
|
|
|
$arguments[$tmp[0]] = $tmp[1]; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $arguments; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function fileExists($file) |
|
99
|
|
|
{ |
|
100
|
|
|
return file_exists(getcwd().$file); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function beautify($string) |
|
104
|
|
|
{ |
|
105
|
|
|
$explode = explode('/', strip_tags($string)); |
|
106
|
|
|
$string = implode(' / ', $explode); |
|
107
|
|
|
|
|
108
|
|
|
return $string; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function htmlEntityDecode($str) |
|
112
|
|
|
{ |
|
113
|
|
|
$str = html_entity_decode($str); |
|
114
|
|
|
$str = preg_replace('#\R+#', '', $str); |
|
115
|
|
|
|
|
116
|
|
|
return $str; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function stripDescr($body, $fallback = null, $type = null, $lengthIn = 300) |
|
120
|
|
|
{ |
|
121
|
|
|
if ($type && $fallback) { |
|
122
|
|
|
$length = isset($fallback[$type]) ? $fallback[$type] : null; |
|
123
|
|
|
} |
|
124
|
|
|
if (!isset($length)) { |
|
125
|
|
|
$length = $lengthIn; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
if (strlen($body) > $length) { |
|
129
|
|
|
$body = substr($body, 0, strpos($body, ' ', $length)).'...'; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return $body; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function detectLang($body) |
|
136
|
|
|
{ |
|
137
|
|
|
switch (true) { |
|
138
|
|
|
case 0 === strpos($body, '<?xml'): |
|
139
|
|
|
return 'xml'; |
|
140
|
|
|
case 0 === strpos($body, '{'): |
|
141
|
|
|
case 0 === strpos($body, '['): |
|
142
|
|
|
return 'json'; |
|
143
|
|
|
default: |
|
144
|
|
|
return 'markup'; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
public function prettyPrint($code, $lang) |
|
149
|
|
|
{ |
|
150
|
|
|
switch ($lang) { |
|
151
|
|
|
case 'json': |
|
152
|
|
|
return json_encode(json_decode($code), JSON_PRETTY_PRINT); |
|
153
|
|
|
case 'xml': |
|
154
|
|
|
$xml = new \DomDocument('1.0'); |
|
155
|
|
|
$xml->preserveWhiteSpace = false; |
|
156
|
|
|
$xml->formatOutput = true; |
|
157
|
|
|
$xml->loadXml($code); |
|
158
|
|
|
|
|
159
|
|
|
return $xml->saveXml(); |
|
160
|
|
|
default: |
|
161
|
|
|
return $code; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public function statusCodeClass($statusCode) |
|
166
|
|
|
{ |
|
167
|
|
|
$codes = [ |
|
168
|
|
|
5 => self::SERVER, |
|
169
|
|
|
4 => self::CLIENT, |
|
170
|
|
|
3 => self::REDIRECT, |
|
171
|
|
|
2 => self::SUCCESS, |
|
172
|
|
|
1 => self::INFO, |
|
173
|
|
|
]; |
|
174
|
|
|
$code = (int)floor(intval($statusCode) / 100); |
|
175
|
|
|
|
|
176
|
|
|
return isset($codes[$code]) ? $codes[$code] : 'unknown'; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
public function formatDuration($seconds) |
|
180
|
|
|
{ |
|
181
|
|
|
$formats = [ |
|
182
|
|
|
'%.2f s', |
|
183
|
|
|
'%d ms', |
|
184
|
|
|
'%d µs', |
|
185
|
|
|
]; |
|
186
|
|
|
|
|
187
|
|
|
while ($format = array_shift($formats)) { |
|
188
|
|
|
if ($seconds > 1) { |
|
189
|
|
|
break; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
$seconds *= 1000; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
return sprintf($format, $seconds); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
public function shortenUri($uri) |
|
199
|
|
|
{ |
|
200
|
|
|
$parts = parse_url($uri); |
|
201
|
|
|
|
|
202
|
|
|
return sprintf('%s://%s%s', isset($parts['scheme']) ? $parts['scheme'] : 'http', $parts['host'], isset($parts['port']) ? (':'.$parts['port']) : ''); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
public function isIE() |
|
206
|
|
|
{ |
|
207
|
|
|
$request = Request::createFromGlobals(); |
|
208
|
|
|
$agent = $request->server->get('HTTP_USER_AGENT'); |
|
209
|
|
|
if (strpos($agent, 'MSIE') || strpos($agent, 'Edge') || strpos($agent, 'Trident/7')) { |
|
210
|
|
|
return 1; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
return 0; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
public function getAssetVersion($filename) |
|
217
|
|
|
{ |
|
218
|
|
|
if (count($this->paths) === 0) { |
|
219
|
|
|
$manifestPath = $this->appDir.'/Resources/assets/rev-manifest.json'; |
|
220
|
|
|
if (!file_exists($manifestPath)) { |
|
221
|
|
|
return $filename; |
|
222
|
|
|
} |
|
223
|
|
|
$this->paths = json_decode(file_get_contents($manifestPath), true); |
|
224
|
|
|
if (!isset($this->paths[$filename])) { |
|
225
|
|
|
return $filename; |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
return $this->paths[$filename]; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
public function cmpOrderBy($aVar, $bVar) |
|
233
|
|
|
{ |
|
234
|
|
|
$aValue = $aVar->{'get'.ucfirst($this->param)}(); |
|
235
|
|
|
$bValue = $bVar->{'get'.ucfirst($this->param)}(); |
|
236
|
|
|
switch ($this->order) { |
|
237
|
|
|
case 'asc': |
|
238
|
|
|
return $aValue > $bValue; |
|
239
|
|
|
case 'desc': |
|
240
|
|
|
return $aValue < $bValue; |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
public function usortFunction($objects, $parameter, $order = 'asc') |
|
245
|
|
|
{ |
|
246
|
|
|
$this->param = $parameter; |
|
247
|
|
|
$this->order = strtolower($order); |
|
248
|
|
|
|
|
249
|
|
|
if (is_object($objects)) { |
|
250
|
|
|
$objects = $objects->toArray(); |
|
251
|
|
|
} |
|
252
|
|
|
usort($objects, [ |
|
253
|
|
|
__CLASS__, |
|
254
|
|
|
'cmpOrderBy', |
|
255
|
|
|
]); |
|
256
|
|
|
|
|
257
|
|
|
return $objects; |
|
258
|
|
|
} |
|
259
|
|
|
} |
|
260
|
|
|
|