Completed
Push — master ( e2b77b...faecd2 )
by Vojta
02:17
created

Plugin.php (2 issues)

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
8
/**
9
 * Twig Extensions Plugin
10
 * - add more Twig filters to your template
11
 *
12
 * @see http://twig.sensiolabs.org/doc/extensions/index.html#extensions-install
13
 */
14
class Plugin extends PluginBase
15
{
16
    /**
17
     * Returns information about this plugin.
18
     *
19
     * @return array
20
     */
21
    public function pluginDetails()
22
    {
23
        return [
24
            'name'        => 'Twig Extensions',
25
            'description' => 'Add more Twig filters to your templates.',
26
            'author'      => 'Vojta Svoboda',
27
            'icon'        => 'icon-plus'
28
        ];
29
    }
30
31
    /**
32
     * Add Twig extensions
33
     *
34
     * @see Text extensions http://twig.sensiolabs.org/doc/extensions/text.html
35
     * @see Intl extensions http://twig.sensiolabs.org/doc/extensions/intl.html
36
     * @see Array extension http://twig.sensiolabs.org/doc/extensions/array.html
37
     * @see Time extension http://twig.sensiolabs.org/doc/extensions/date.html
38
     *
39
     * @return array
40
     */
41
    public function registerMarkupTags()
42
    {
43
        $twig = App::make('twig.environment');
44
        $filters = [];
45
46
        // add Text extensions
47
        $textExtension = new \Twig_Extensions_Extension_Text;
48
        $textFilters = $textExtension->getFilters();
49
        $filters += [
50 View Code Duplication
            'truncate' => function($value, $length = 30, $preserve = false, $separator = '...') use ($twig, $textFilters) {
1 ignored issue
show
This code seems to be duplicated across 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...
51
                $callable = $textFilters['0']->getCallable();
52
                return $callable($twig, $value, $length, $preserve, $separator);
53
            },
54 View Code Duplication
            'wordwrap' => function($value, $length = 80, $separator = "\n", $preserve = false) use ($twig, $textFilters) {
1 ignored issue
show
This code seems to be duplicated across 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...
55
                $callable = $textFilters['1']->getCallable();
56
                return $callable($twig, $value, $length, $separator, $preserve);
57
            }
58
        ];
59
60
        // add Intl extensions if php5-intl installed
61
        if ( class_exists('IntlDateFormatter') ) {
62
            $intlExtension = new \Twig_Extensions_Extension_Intl;
63
            $intlFilters = $intlExtension->getFilters();
64
65
            $filters += [
66
                'localizeddate' => function($date, $dateFormat = 'medium', $timeFormat = 'medium', $locale = null, $timezone = null, $format = null) use ($twig, $intlFilters) {
67
                    $callable = $intlFilters['0']->getCallable();
68
                    return $callable($twig, $date, $dateFormat, $timeFormat, $locale, $timezone, $format);
69
                },
70
                'localizednumber' => function($number, $style = 'decimal', $type = 'default', $locale = null) use ($twig, $intlFilters) {
71
                    $callable = $intlFilters['1']->getCallable();
72
                    return $callable($twig, $number, $style, $type, $locale);
73
                },
74
                'localizedcurrency' => function($number, $currency = null, $locale = null) use ($twig, $intlFilters) {
75
                    $callable = $intlFilters['2']->getCallable();
76
                    return $callable($twig, $number, $currency, $locale);
77
                }
78
            ];
79
        }
80
81
        // add Array extensions
82
        $arrayExtension = new \Twig_Extensions_Extension_Array;
83
        $arrayFilters = $arrayExtension->getFilters();
84
        $filters += [
85
            'shuffle' => function($array) use ($twig, $arrayFilters) {
86
                $callable = $arrayFilters['0']->getCallable();
87
                return $callable($twig, $array);
88
            }
89
        ];
90
91
        // add Time extensions
92
        $timeExtension = new \Twig_Extensions_Extension_Date;
93
        $timeFilters = $timeExtension->getFilters();
94
        $filters += [
95
            'time_diff' => function($date, $now = null) use ($twig, $timeFilters) {
96
                $callable = $timeFilters['0']->getCallable();
97
                return $callable($twig, $date, $now);
98
            }
99
        ];
100
101
        // add PHP functions
102
        $filters += [
103
            'strftime' => function($time, $format = '%d.%m.%Y %H:%M:%S') {
104
                $timeObj = new Carbon($time);
105
                return strftime($format, $timeObj->getTimestamp());
106
            },
107
            'uppercase' => function($string) {
108
                return strtoupper($string);
109
            },
110
            'lowercase' => function($string) {
111
                return strtolower($string);
112
            },
113
            'ucfirst' => function($string) {
114
                return ucfirst($string);
115
            },
116
            'lcfirst' => function($string) {
117
                return lcfirst($string);
118
            },
119
            'ltrim' => function($string, $charlist = " \t\n\r\0\x0B") {
120
                return ltrim($string, $charlist);
121
            },
122
            'rtrim' => function($string, $charlist = " \t\n\r\0\x0B") {
123
                return rtrim($string, $charlist);
124
            },
125
            'str_repeat' => function($string, $multiplier = 1) {
126
                return str_repeat($string, $multiplier);
127
            },
128
            'plural' => function($string, $count = 2) {
129
                return str_plural($string, $count);
130
            }
131
        ];
132
133
        return [
134
            'filters' => $filters
135
        ];
136
    }
137
}
138