Completed
Push — master ( faecd2...265a8d )
by Vojta
7s
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
        $functions = [];
46
47
        // add StringLoader
48
        $stringLoaderExtension = new \Twig_Extension_StringLoader;
49
        $stringLoaderFunctions = $stringLoaderExtension->getFunctions();
50
51
        $functions += [
52
          'template_from_string' => function($template) use ($twig, $stringLoaderFunctions) {
53
            $callable = $stringLoaderFunctions['0']->getCallable();
54
            return $callable($twig, $template);
55
          }
56
        ];
57
58
        // add Text extensions
59
        $textExtension = new \Twig_Extensions_Extension_Text;
60
        $textFilters = $textExtension->getFilters();
61
        $filters += [
62 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...
63
                $callable = $textFilters['0']->getCallable();
64
                return $callable($twig, $value, $length, $preserve, $separator);
65
            },
66 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...
67
                $callable = $textFilters['1']->getCallable();
68
                return $callable($twig, $value, $length, $separator, $preserve);
69
            }
70
        ];
71
72
        // add Intl extensions if php5-intl installed
73
        if ( class_exists('IntlDateFormatter') ) {
74
            $intlExtension = new \Twig_Extensions_Extension_Intl;
75
            $intlFilters = $intlExtension->getFilters();
76
77
            $filters += [
78
                'localizeddate' => function($date, $dateFormat = 'medium', $timeFormat = 'medium', $locale = null, $timezone = null, $format = null) use ($twig, $intlFilters) {
79
                    $callable = $intlFilters['0']->getCallable();
80
                    return $callable($twig, $date, $dateFormat, $timeFormat, $locale, $timezone, $format);
81
                },
82
                'localizednumber' => function($number, $style = 'decimal', $type = 'default', $locale = null) use ($twig, $intlFilters) {
83
                    $callable = $intlFilters['1']->getCallable();
84
                    return $callable($twig, $number, $style, $type, $locale);
85
                },
86
                'localizedcurrency' => function($number, $currency = null, $locale = null) use ($twig, $intlFilters) {
87
                    $callable = $intlFilters['2']->getCallable();
88
                    return $callable($twig, $number, $currency, $locale);
89
                }
90
            ];
91
        }
92
93
        // add Array extensions
94
        $arrayExtension = new \Twig_Extensions_Extension_Array;
95
        $arrayFilters = $arrayExtension->getFilters();
96
        $filters += [
97
            'shuffle' => function($array) use ($twig, $arrayFilters) {
98
                $callable = $arrayFilters['0']->getCallable();
99
                return $callable($twig, $array);
100
            }
101
        ];
102
103
        // add Time extensions
104
        $timeExtension = new \Twig_Extensions_Extension_Date;
105
        $timeFilters = $timeExtension->getFilters();
106
        $filters += [
107
            'time_diff' => function($date, $now = null) use ($twig, $timeFilters) {
108
                $callable = $timeFilters['0']->getCallable();
109
                return $callable($twig, $date, $now);
110
            }
111
        ];
112
113
        // add PHP functions
114
        $filters += [
115
            'strftime' => function($time, $format = '%d.%m.%Y %H:%M:%S') {
116
                $timeObj = new Carbon($time);
117
                return strftime($format, $timeObj->getTimestamp());
118
            },
119
            'uppercase' => function($string) {
120
                return strtoupper($string);
121
            },
122
            'lowercase' => function($string) {
123
                return strtolower($string);
124
            },
125
            'ucfirst' => function($string) {
126
                return ucfirst($string);
127
            },
128
            'lcfirst' => function($string) {
129
                return lcfirst($string);
130
            },
131
            'ltrim' => function($string, $charlist = " \t\n\r\0\x0B") {
132
                return ltrim($string, $charlist);
133
            },
134
            'rtrim' => function($string, $charlist = " \t\n\r\0\x0B") {
135
                return rtrim($string, $charlist);
136
            },
137
            'str_repeat' => function($string, $multiplier = 1) {
138
                return str_repeat($string, $multiplier);
139
            },
140
            'plural' => function($string, $count = 2) {
141
                return str_plural($string, $count);
142
            }
143
        ];
144
145
        return [
146
            'filters' => $filters,
147
            'functions' => $functions
148
        ];
149
    }
150
}
151