Completed
Push — master ( 90ad64...ebc1c7 )
by Vojta
11:24
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
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
13
/**
14
 * Twig Extensions Plugin
15
 * - add more Twig filters to your template
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
    /**
38
     * Add Twig extensions
39
     *
40
     * @see Text extensions http://twig.sensiolabs.org/doc/extensions/text.html
41
     * @see Intl extensions http://twig.sensiolabs.org/doc/extensions/intl.html
42
     * @see Array extension http://twig.sensiolabs.org/doc/extensions/array.html
43
     * @see Time extension http://twig.sensiolabs.org/doc/extensions/date.html
44
     *
45
     * @return array
46
     */
47 1
    public function registerMarkupTags()
48
    {
49 1
        $filters = [];
50 1
        $functions = [];
51
52
        // init Twig
53 1
        $twig = App::make('twig.environment');
54
55
        // add String Loader functions
56 1
        $functions += $this->getStringLoaderFunctions($twig);
57
58
        // add Config function
59 1
        $functions += $this->getConfigFunction();
60
61
        // add Session function
62 1
        $functions += $this->getSessionFunction();
63
64
        // add Trans function
65 1
        $functions += $this->getTransFunction();
66
67
        // add Text extensions
68 1
        $filters += $this->getTextFilters($twig);
69
70
        // add Intl extensions if php5-intl installed
71 1
        if (class_exists('IntlDateFormatter')) {
72 1
            $filters += $this->getLocalizedFilters($twig);
73 1
        }
74
75
        // add Array extensions
76 1
        $filters += $this->getArrayFilters();
77
78
        // add Time extensions
79 1
        $filters += $this->getTimeFilters($twig);
80
81
        // add PHP functions
82 1
        $filters += $this->getPhpFunctions();
83
84
        return [
85 1
            'filters'   => $filters,
86
            'functions' => $functions
87 1
        ];
88
    }
89
90
    /**
91
     * Returns String Loader functions
92
     *
93
     * @param \Twig_Environment $twig
94
     *
95
     * @return array
96
     */
97 1
    private function getStringLoaderFunctions($twig)
98
    {
99 1
        $stringLoader = new Twig_Extension_StringLoader();
100 1
        $stringLoaderFunc = $stringLoader->getFunctions();
101
102
        return [
103
            'template_from_string' => function($template) use ($twig, $stringLoaderFunc) {
104 1
                $callable = $stringLoaderFunc[0]->getCallable();
105 1
                return $callable($twig, $template);
106
            }
107 1
        ];
108
    }
109
110
    /**
111
     * Returns Text filters
112
     *
113
     * @param \Twig_Environment $twig
114
     *
115
     * @return array
116
     */
117 3
    private function getTextFilters($twig)
118
    {
119 1
        $textExtension = new Twig_Extensions_Extension_Text();
120 1
        $textFilters = $textExtension->getFilters();
121
122
        return [
123 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...
124 3
                $callable = $textFilters[0]->getCallable();
125 3
                return $callable($twig, $value, $length, $preserve, $separator);
126 1
            },
127 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...
128 1
                $callable = $textFilters[1]->getCallable();
129 1
                return $callable($twig, $value, $length, $separator, $preserve);
130
            }
131 1
        ];
132
    }
133
134
    /**
135
     * Returns Intl filters
136
     *
137
     * @param \Twig_Environment $twig
138
     *
139
     * @return array
140
     */
141 1
    private function getLocalizedFilters($twig)
142
    {
143 1
        $intlExtension = new Twig_Extensions_Extension_Intl();
144 1
        $intlFilters = $intlExtension->getFilters();
145
146
        return [
147
            'localizeddate' => function($date, $dateFormat = 'medium', $timeFormat = 'medium', $locale = null, $timezone = null, $format = null) use ($twig, $intlFilters) {
148
                $callable = $intlFilters[0]->getCallable();
149
                return $callable($twig, $date, $dateFormat, $timeFormat, $locale, $timezone, $format);
150 1
            },
151
            'localizednumber' => function($number, $style = 'decimal', $type = 'default', $locale = null) use ($twig, $intlFilters) {
152
                $callable = $intlFilters[1]->getCallable();
153
                return $callable($number, $style, $type, $locale);
154 1
            },
155
            'localizedcurrency' => function($number, $currency = null, $locale = null) use ($twig, $intlFilters) {
156
                $callable = $intlFilters[2]->getCallable();
157
                return $callable($number, $currency, $locale);
158
            }
159 1
        ];
160
    }
161
162
    /**
163
     * Returns Array filters
164
     *
165
     * @return array
166
     */
167 2
    private function getArrayFilters()
168
    {
169 1
        $arrayExtension = new Twig_Extensions_Extension_Array();
170 1
        $arrayFilters = $arrayExtension->getFilters();
171
172
        return [
173
            'shuffle' => function($array) use ($arrayFilters) {
174 2
                $callable = $arrayFilters[0]->getCallable();
175 2
                return $callable($array);
176
            }
177 1
        ];
178
    }
179
180
    /**
181
     * Returns Date filters
182
     *
183
     * @param \Twig_Environment $twig
184
     *
185
     * @return array
186
     */
187 1
    private function getTimeFilters($twig)
188
    {
189 1
        $timeExtension = new Twig_Extensions_Extension_Date();
190 1
        $timeFilters = $timeExtension->getFilters();
191
192
        return [
193
            'time_diff' => function($date, $now = null) use ($twig, $timeFilters) {
194 1
                $callable = $timeFilters[0]->getCallable();
195 1
                return $callable($twig, $date, $now);
196
            }
197 1
        ];
198
    }
199
200
    /**
201
     * Returns plain PHP functions
202
     *
203
     * @return array
204
     */
205 1
    private function getPhpFunctions()
206
    {
207
        return [
208
            'strftime' => function($time, $format = '%d.%m.%Y %H:%M:%S') {
209 1
                $timeObj = new Carbon($time);
210 1
                return strftime($format, $timeObj->getTimestamp());
211 1
            },
212
            'uppercase' => function($string) {
213 1
                return mb_convert_case($string, MB_CASE_UPPER, "UTF-8");
214 1
            },
215
            'lowercase' => function($string) {
216 1
                return mb_convert_case($string, MB_CASE_LOWER, "UTF-8");
217 1
            },
218
            'ucfirst' => function($string) {
219 1
                return mb_convert_case($string, MB_CASE_TITLE, "UTF-8");
220 1
            },
221
            'lcfirst' => function($string) {
222 1
                return lcfirst($string);
223 1
            },
224
            'ltrim' => function($string, $charlist = " \t\n\r\0\x0B") {
225 1
                return ltrim($string, $charlist);
226 1
            },
227
            'rtrim' => function($string, $charlist = " \t\n\r\0\x0B") {
228 1
                return rtrim($string, $charlist);
229 1
            },
230
            'str_repeat' => function($string, $multiplier = 1) {
231 1
                return str_repeat($string, $multiplier);
232 1
            },
233
            'plural' => function($string, $count = 2) {
234 1
                return str_plural($string, $count);
235 1
            },
236
            'strpad' => function($string, $pad_length, $pad_string = ' ') {
237 1
                return str_pad($string, $pad_length, $pad_string, $pad_type = STR_PAD_BOTH);
238 1
            },
239
            'leftpad' => function($string, $pad_length, $pad_string = ' ') {
240 1
                return str_pad($string, $pad_length, $pad_string, $pad_type = STR_PAD_LEFT);
241 1
            },
242
            'rightpad' => function($string, $pad_length, $pad_string = ' ') {
243 1
                return str_pad($string, $pad_length, $pad_string, $pad_type = STR_PAD_RIGHT);
244 1
            },
245 1
        ];
246
    }
247
248
    /**
249
     * Works like the config() helper function
250
     *
251
     * @return array
252
     */
253 1
    private function getConfigFunction()
254
    {
255
        return [
256
            'config' => function($key = null, $default = null) {
257 1
                return config($key, $default);
258 1
            },
259 1
        ];
260
    }
261
262
    /**
263
     * Works like the session() helper function
264
     *
265
     * @return array
266
     */
267 1
    private function getSessionFunction()
268
    {
269
        return [
270
            'session' => function($key = null) {
271 1
                return session($key);
272 1
            },
273 1
        ];
274
    }
275
276
    /**
277
     * Works like the trans() helper function
278
     *
279
     * @return array
280
     */
281
    private function getTransFunction()
282
    {
283
        return [
284 1
            'trans' => function($key = null) {
285 1
                return trans($key);
286 1
            },
287 1
        ];
288
    }
289
}
290