Completed
Push — master ( 8de5f4...f65d48 )
by Vojta
15:28 queued 07:27
created

Plugin.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php namespace VojtaSvoboda\TwigExtensions;
2
3
use App;
4
use Backend;
5
use Carbon\Carbon;
6
use System\Classes\PluginBase;
7
use Twig_Extension_StringLoader;
8
use Twig_Extensions_Extension_Array;
9
use Twig_Extensions_Extension_Date;
10
use Twig_Extensions_Extension_Intl;
11
use Twig_Extensions_Extension_Text;
12
use VojtaSvoboda\TwigExtensions\Classes\TimeDiffTranslator;
13
14
/**
15
 * Twig Extensions Plugin.
16
 *
17
 * @see http://twig.sensiolabs.org/doc/extensions/index.html#extensions-install
18
 */
19
class Plugin extends PluginBase
20
{
21
    /**
22
     * Returns information about this plugin.
23
     *
24
     * @return array
25
     */
26
    public function pluginDetails()
27
    {
28
        return [
29
            'name'        => 'Twig Extensions',
30
            'description' => 'Add more Twig filters to your templates.',
31
            'author'      => 'Vojta Svoboda',
32
            'icon'        => 'icon-plus',
33
            'homepage'    => 'https://github.com/vojtasvoboda/oc-twigextensions-plugin',
34
        ];
35
    }
36
37 View Code Duplication
    public function boot()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
    {
39
        $this->app->singleton('time_diff_translator', function($app) {
40
            $loader = $app->make('translation.loader');
41
            $locale = $app->config->get('app.locale');
42
            $translator = $app->make(TimeDiffTranslator::class, [$loader, $locale]);
43
            $translator->setFallback($app->config->get('app.fallback_locale'));
44
45
            return $translator;
46
        });
47
    }
48
49
    /**
50
     * Add Twig extensions.
51
     *
52
     * @see Text extensions http://twig.sensiolabs.org/doc/extensions/text.html
53
     * @see Intl extensions http://twig.sensiolabs.org/doc/extensions/intl.html
54
     * @see Array extension http://twig.sensiolabs.org/doc/extensions/array.html
55
     * @see Time extension http://twig.sensiolabs.org/doc/extensions/date.html
56
     *
57
     * @return array
58
     */
59 1
    public function registerMarkupTags()
60
    {
61 1
        $filters = [];
62 1
        $functions = [];
63
64
        // init Twig
65 1
        $twig = $this->app->make('twig.environment');
66
67
        // add String Loader functions
68 1
        $functions += $this->getStringLoaderFunctions($twig);
69
70
        // add Config function
71 1
        $functions += $this->getConfigFunction();
72
73
        // add Session function
74 1
        $functions += $this->getSessionFunction();
75
76
        // add Trans function
77 1
        $functions += $this->getTransFunction();
78
79
        // add var_dump function
80 1
        $functions += $this->getVarDumpFunction();
81
82
        // add Text extensions
83 1
        $filters += $this->getTextFilters($twig);
84
85
        // add Intl extensions if php5-intl installed
86 1
        if (class_exists('IntlDateFormatter')) {
87 1
            $filters += $this->getLocalizedFilters($twig);
88 1
        }
89
90
        // add Array extensions
91 1
        $filters += $this->getArrayFilters();
92
93
        // add Time extensions
94 1
        $filters += $this->getTimeFilters($twig);
95
96
        // add Mail filters
97 1
        $filters += $this->getMailFilters();
98
99
        // add PHP functions
100 1
        $filters += $this->getPhpFunctions();
101
102
        // add File Version filter
103 1
        $filters += $this->getFileRevision();
104
105
        return [
106 1
            'filters'   => $filters,
107 1
            'functions' => $functions,
108 1
        ];
109
    }
110
111
    /**
112
     * Returns String Loader functions.
113
     *
114
     * @param \Twig_Environment $twig
115
     *
116
     * @return array
117
     */
118 1
    private function getStringLoaderFunctions($twig)
119
    {
120 1
        $stringLoader = new Twig_Extension_StringLoader();
121 1
        $stringLoaderFunc = $stringLoader->getFunctions();
122
123
        return [
124
            'template_from_string' => function($template) use ($twig, $stringLoaderFunc) {
125 1
                $callable = $stringLoaderFunc[0]->getCallable();
126 1
                return $callable($twig, $template);
127
            }
128 1
        ];
129
    }
130
131
    /**
132
     * Returns Text filters.
133
     *
134
     * @param \Twig_Environment $twig
135
     *
136
     * @return array
137
     */
138 3
    private function getTextFilters($twig)
139
    {
140 1
        $textExtension = new Twig_Extensions_Extension_Text();
141 1
        $textFilters = $textExtension->getFilters();
142
143
        return [
144
            'truncate' => function($value, $length = 30, $preserve = false, $separator = '...') use ($twig, $textFilters) {
145 3
                $callable = $textFilters[0]->getCallable();
146 3
                return $callable($twig, $value, $length, $preserve, $separator);
147 1
            },
148
            'wordwrap' => function($value, $length = 80, $separator = "\n", $preserve = false) use ($twig, $textFilters) {
149 1
                $callable = $textFilters[1]->getCallable();
150 1
                return $callable($twig, $value, $length, $separator, $preserve);
151
            }
152 1
        ];
153
    }
154
155
    /**
156
     * Returns Intl filters.
157
     *
158
     * @param \Twig_Environment $twig
159
     *
160
     * @return array
161
     */
162 1
    private function getLocalizedFilters($twig)
163
    {
164 1
        $intlExtension = new Twig_Extensions_Extension_Intl();
165 1
        $intlFilters = $intlExtension->getFilters();
166
167
        return [
168
            'localizeddate' => function($date, $dateFormat = 'medium', $timeFormat = 'medium', $locale = null, $timezone = null, $format = null) use ($twig, $intlFilters) {
169
                $callable = $intlFilters[0]->getCallable();
170
                return $callable($twig, $date, $dateFormat, $timeFormat, $locale, $timezone, $format);
171 1
            },
172
            'localizednumber' => function($number, $style = 'decimal', $type = 'default', $locale = null) use ($twig, $intlFilters) {
173
                $callable = $intlFilters[1]->getCallable();
174
                return $callable($number, $style, $type, $locale);
175 1
            },
176
            'localizedcurrency' => function($number, $currency = null, $locale = null) use ($twig, $intlFilters) {
177
                $callable = $intlFilters[2]->getCallable();
178
                return $callable($number, $currency, $locale);
179
            }
180 1
        ];
181
    }
182
183
    /**
184
     * Returns Array filters.
185
     *
186
     * @return array
187
     */
188 2
    private function getArrayFilters()
189
    {
190 1
        $arrayExtension = new Twig_Extensions_Extension_Array();
191 1
        $arrayFilters = $arrayExtension->getFilters();
192
193
        return [
194
            'shuffle' => function($array) use ($arrayFilters) {
195 2
                $callable = $arrayFilters[0]->getCallable();
196 2
                return $callable($array);
197
            }
198 1
        ];
199
    }
200
201
    /**
202
     * Returns Date filters.
203
     *
204
     * @param \Twig_Environment $twig
205
     *
206
     * @return array
207
     */
208 1
    private function getTimeFilters($twig)
209
    {
210 1
        $translator = $this->app->make('time_diff_translator');
211 1
        $timeExtension = new Twig_Extensions_Extension_Date($translator);
212 1
        $timeFilters = $timeExtension->getFilters();
213
214
        return [
215
            'time_diff' => function($date, $now = null) use ($twig, $timeFilters) {
216
                $callable = $timeFilters[0]->getCallable();
217
                return $callable($twig, $date, $now);
218
            }
219 1
        ];
220
    }
221
222
    /**
223
     * Returns mail filters.
224
     *
225
     * @return array
226
     */
227 1
    private function getMailFilters()
228
    {
229
        return [
230
            'mailto' => function($string, $link = true, $protected = true, $text = null) {
231 1
                return $this->hideEmail($string, $link, $protected, $text);
232
            }
233 1
        ];
234
    }
235
236
    /**
237
     * Returns plain PHP functions.
238
     *
239
     * @return array
240
     */
241 1
    private function getPhpFunctions()
242
    {
243
        return [
244
            'strftime' => function($time, $format = '%d.%m.%Y %H:%M:%S') {
245 1
                $timeObj = new Carbon($time);
246 1
                return strftime($format, $timeObj->getTimestamp());
247 1
            },
248
            'uppercase' => function($string) {
249 1
                return mb_convert_case($string, MB_CASE_UPPER, "UTF-8");
250 1
            },
251
            'lowercase' => function($string) {
252 1
                return mb_convert_case($string, MB_CASE_LOWER, "UTF-8");
253 1
            },
254
            'ucfirst' => function($string) {
255 1
                return mb_convert_case($string, MB_CASE_TITLE, "UTF-8");
256 1
            },
257
            'lcfirst' => function($string) {
258 1
                return lcfirst($string);
259 1
            },
260
            'ltrim' => function($string, $charlist = " \t\n\r\0\x0B") {
261 1
                return ltrim($string, $charlist);
262 1
            },
263
            'rtrim' => function($string, $charlist = " \t\n\r\0\x0B") {
264 1
                return rtrim($string, $charlist);
265 1
            },
266
            'str_repeat' => function($string, $multiplier = 1) {
267 1
                return str_repeat($string, $multiplier);
268 1
            },
269
            'plural' => function($string, $count = 2) {
270 1
                return str_plural($string, $count);
271 1
            },
272
            'strpad' => function($string, $pad_length, $pad_string = ' ') {
273 1
                return str_pad($string, $pad_length, $pad_string, $pad_type = STR_PAD_BOTH);
274 1
            },
275
            'leftpad' => function($string, $pad_length, $pad_string = ' ') {
276 1
                return str_pad($string, $pad_length, $pad_string, $pad_type = STR_PAD_LEFT);
277 1
            },
278
            'rightpad' => function($string, $pad_length, $pad_string = ' ') {
279 1
                return str_pad($string, $pad_length, $pad_string, $pad_type = STR_PAD_RIGHT);
280 1
            },
281
            'rtl' => function($string) {
282 1
                return strrev($string);
283 1
            },
284
            'strip_tags' => function($string, $allow = '') {
285 1
                return strip_tags($string, $allow);
286 1
            },
287 1
            'var_dump' => function($expression) {
288
                ob_start();
289 1
                var_dump($expression);
290 1
                $result = ob_get_clean();
291 1
292
                return $result;
293
            },
294
        ];
295
    }
296
297
    /**
298
     * Works like the config() helper function.
299 1
     *
300
     * @return array
301
     */
302
    private function getConfigFunction()
303 1
    {
304 1
        return [
305 1
            'config' => function($key = null, $default = null) {
306
                return config($key, $default);
307
            },
308
        ];
309
    }
310
311
    /**
312
     * Works like the session() helper function.
313 1
     *
314
     * @return array
315
     */
316
    private function getSessionFunction()
317 1
    {
318 1
        return [
319 1
            'session' => function($key = null) {
320
                return session($key);
321
            },
322
        ];
323
    }
324
325
    /**
326
     * Works like the trans() helper function.
327 1
     *
328
     * @return array
329
     */
330
    private function getTransFunction()
331 1
    {
332 1
        return [
333 1
            'trans' => function($key = null) {
334
                return trans($key);
335
            },
336
        ];
337
    }
338
339
    /**
340
     * Dumps information about a variable.
341 1
     *
342
     * @return array
343
     */
344
    private function getVarDumpFunction()
345 1
    {
346 1
        return [
347 1
            'var_dump' => function($expression) {
348
                ob_start();
349 1
                var_dump($expression);
350 1
                $result = ob_get_clean();
351 1
352
                return $result;
353
            },
354
        ];
355
    }
356
357
    /**
358
     * Create protected link with mailto:
359
     *
360
     * @param string $email Email to render.
361
     * @param bool $link If email should be rendered as link.
362
     * @param bool $protected If email should be protected.
363
     * @param string $text Link text. Render email by default.
364
     *
365
     * @see http://www.maurits.vdschee.nl/php_hide_email/
366 1
     *
367
     * @return string
368
     */
369 1
    private function hideEmail($email, $link = true, $protected = true, $text = null)
370 1
    {
371 1
        // email link text
372 1
        $linkText = $email;
373
        if ($text !== null) {
374
            $linkText = $text;
375 1
        }
376 1
377
        // if we want just unprotected link
378
        if (!$protected) {
379
            return $link ? '<a href="mailto:' . $email . '">' . $linkText . '</a>' : $linkText;
380 1
        }
381 1
382 1
        // turn on protection
383 1
        $character_set = '+-.0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz';
384 1
        $key = str_shuffle($character_set);
385 1
        $cipher_text = '';
386 1
        $id = 'e' . rand(1, 999999999);
387 1
        for ($i = 0; $i < strlen($email); $i += 1) $cipher_text .= $key[strpos($character_set, $email[$i])];
388 1
        $script = 'var a="' . $key . '";var b=a.split("").sort().join("");var c="' . $cipher_text . '";var d="";';
389 1
        $script .= 'for(var e=0;e<c.length;e++)d+=b.charAt(a.indexOf(c.charAt(e)));';
390 1
        $script .= 'var y = d;';
391 1
        if ($text !== null) {
392 1
            $script .= 'var y = "'.$text.'";';
393 1
        }
394 1
        if ($link) {
395
            $script .= 'document.getElementById("' . $id . '").innerHTML="<a href=\\"mailto:"+d+"\\">"+y+"</a>"';
396 1
        } else {
397 1
            $script .= 'document.getElementById("' . $id . '").innerHTML=y';
398
        }
399 1
        $script = "eval(\"" . str_replace(array("\\", '"'), array("\\\\", '\"'), $script) . "\")";
400
        $script = '<script type="text/javascript">/*<![CDATA[*/' . $script . '/*]]>*/</script>';
401
402
        return '<span id="' . $id . '">[javascript protected email address]</span>' . $script;
403
    }
404
405
    /**
406
     * Appends this pattern: ? . {last modified date}
407
     * to an assets filename to force browser to reload
408
     * cached modified file.
409
     *
410
     * See: https://github.com/vojtasvoboda/oc-twigextensions-plugin/issues/25
411
     *
412
     * @return array
413
     */
414 1
    private function getFileRevision()
415
    {
416
        return [
417
            'revision' => function ($filename, $format = null) {
418
                // Remove http/web address from the file name if there is one to load it locally
419
                $prefix = url('/');
420
                $filename_ = trim(preg_replace('/^' . preg_quote($prefix, '/') . '/', '', $filename), '/');
421
                if (file_exists($filename_)) {
422
                    $timestamp = filemtime($filename_);
423
                    $prepend = ($format) ? date($format, $timestamp) : $timestamp;
424
425
                    return $filename . "?" . $prepend;
426 1
                }
427 1
428
                return $filename;
429
            },
430
        ];
431
    }
432
}
433