Completed
Push — master ( 601cd8...485f34 )
by Vojta
08:12
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
 *
16
 * @see http://twig.sensiolabs.org/doc/extensions/index.html#extensions-install
17
 */
18
class Plugin extends PluginBase
19
{
20
    /**
21
     * Returns information about this plugin.
22
     *
23
     * @return array
24
     */
25
    public function pluginDetails()
26
    {
27
        return [
28
            'name'        => 'Twig Extensions',
29
            'description' => 'Add more Twig filters to your templates.',
30
            'author'      => 'Vojta Svoboda',
31
            'icon'        => 'icon-plus',
32
            'homepage'    => 'https://github.com/vojtasvoboda/oc-twigextensions-plugin',
33
        ];
34
    }
35
36
    /**
37
     * Add Twig extensions.
38
     *
39
     * @see Text extensions http://twig.sensiolabs.org/doc/extensions/text.html
40
     * @see Intl extensions http://twig.sensiolabs.org/doc/extensions/intl.html
41
     * @see Array extension http://twig.sensiolabs.org/doc/extensions/array.html
42
     * @see Time extension http://twig.sensiolabs.org/doc/extensions/date.html
43
     *
44
     * @return array
45
     */
46 1
    public function registerMarkupTags()
47
    {
48 1
        $filters = [];
49 1
        $functions = [];
50
51
        // init Twig
52 1
        $twig = $this->app->make('twig.environment');
53
54
        // add String Loader functions
55 1
        $functions += $this->getStringLoaderFunctions($twig);
56
57
        // add Config function
58 1
        $functions += $this->getConfigFunction();
59
60
        // add Session function
61 1
        $functions += $this->getSessionFunction();
62
63
        // add Trans function
64 1
        $functions += $this->getTransFunction();
65
66
        // add var_dump function
67 1
        $functions += $this->getVarDumpFunction();
68
69
        // add Text extensions
70 1
        $filters += $this->getTextFilters($twig);
71
72
        // add Intl extensions if php5-intl installed
73 1
        if (class_exists('IntlDateFormatter')) {
74 1
            $filters += $this->getLocalizedFilters($twig);
75 1
        }
76
77
        // add Array extensions
78 1
        $filters += $this->getArrayFilters();
79
80
        // add Time extensions
81 1
        $filters += $this->getTimeFilters($twig);
82
83
        // add PHP functions
84 1
        $filters += $this->getPhpFunctions();
85
86
        return [
87 1
            'filters'   => $filters,
88 1
            'functions' => $functions,
89 1
        ];
90
    }
91
92
    /**
93
     * Returns String Loader functions.
94
     *
95
     * @param \Twig_Environment $twig
96
     *
97
     * @return array
98
     */
99 1
    private function getStringLoaderFunctions($twig)
100
    {
101 1
        $stringLoader = new Twig_Extension_StringLoader();
102 1
        $stringLoaderFunc = $stringLoader->getFunctions();
103
104
        return [
105
            'template_from_string' => function($template) use ($twig, $stringLoaderFunc) {
106 1
                $callable = $stringLoaderFunc[0]->getCallable();
107 1
                return $callable($twig, $template);
108
            }
109 1
        ];
110
    }
111
112
    /**
113
     * Returns Text filters.
114
     *
115
     * @param \Twig_Environment $twig
116
     *
117
     * @return array
118
     */
119 3
    private function getTextFilters($twig)
120
    {
121 1
        $textExtension = new Twig_Extensions_Extension_Text();
122 1
        $textFilters = $textExtension->getFilters();
123
124
        return [
125 View Code Duplication
            'truncate' => function($value, $length = 30, $preserve = false, $separator = '...') use ($twig, $textFilters) {
126 3
                $callable = $textFilters[0]->getCallable();
127 3
                return $callable($twig, $value, $length, $preserve, $separator);
128 1
            },
129 View Code Duplication
            'wordwrap' => function($value, $length = 80, $separator = "\n", $preserve = false) use ($twig, $textFilters) {
130 1
                $callable = $textFilters[1]->getCallable();
131 1
                return $callable($twig, $value, $length, $separator, $preserve);
132
            }
133 1
        ];
134
    }
135
136
    /**
137
     * Returns Intl filters.
138
     *
139
     * @param \Twig_Environment $twig
140
     *
141
     * @return array
142
     */
143 1
    private function getLocalizedFilters($twig)
144
    {
145 1
        $intlExtension = new Twig_Extensions_Extension_Intl();
146 1
        $intlFilters = $intlExtension->getFilters();
147
148
        return [
149
            'localizeddate' => function($date, $dateFormat = 'medium', $timeFormat = 'medium', $locale = null, $timezone = null, $format = null) use ($twig, $intlFilters) {
150
                $callable = $intlFilters[0]->getCallable();
151
                return $callable($twig, $date, $dateFormat, $timeFormat, $locale, $timezone, $format);
152 1
            },
153
            'localizednumber' => function($number, $style = 'decimal', $type = 'default', $locale = null) use ($twig, $intlFilters) {
154
                $callable = $intlFilters[1]->getCallable();
155
                return $callable($number, $style, $type, $locale);
156 1
            },
157
            'localizedcurrency' => function($number, $currency = null, $locale = null) use ($twig, $intlFilters) {
158
                $callable = $intlFilters[2]->getCallable();
159
                return $callable($number, $currency, $locale);
160
            }
161 1
        ];
162
    }
163
164
    /**
165
     * Returns Array filters.
166
     *
167
     * @return array
168
     */
169 2
    private function getArrayFilters()
170
    {
171 1
        $arrayExtension = new Twig_Extensions_Extension_Array();
172 1
        $arrayFilters = $arrayExtension->getFilters();
173
174
        return [
175
            'shuffle' => function($array) use ($arrayFilters) {
176 2
                $callable = $arrayFilters[0]->getCallable();
177 2
                return $callable($array);
178
            }
179 1
        ];
180
    }
181
182
    /**
183
     * Returns Date filters.
184
     *
185
     * @param \Twig_Environment $twig
186
     *
187
     * @return array
188
     */
189 1
    private function getTimeFilters($twig)
190
    {
191 1
        $timeExtension = new Twig_Extensions_Extension_Date();
192 1
        $timeFilters = $timeExtension->getFilters();
193
194
        return [
195
            'time_diff' => function($date, $now = null) use ($twig, $timeFilters) {
196 1
                $callable = $timeFilters[0]->getCallable();
197 1
                return $callable($twig, $date, $now);
198
            }
199 1
        ];
200
    }
201
202
    /**
203
     * Returns plain PHP functions.
204
     *
205
     * @return array
206
     */
207 1
    private function getPhpFunctions()
208
    {
209
        return [
210
            'strftime' => function($time, $format = '%d.%m.%Y %H:%M:%S') {
211 1
                $timeObj = new Carbon($time);
212 1
                return strftime($format, $timeObj->getTimestamp());
213 1
            },
214
            'uppercase' => function($string) {
215 1
                return mb_convert_case($string, MB_CASE_UPPER, "UTF-8");
216 1
            },
217
            'lowercase' => function($string) {
218 1
                return mb_convert_case($string, MB_CASE_LOWER, "UTF-8");
219 1
            },
220
            'ucfirst' => function($string) {
221 1
                return mb_convert_case($string, MB_CASE_TITLE, "UTF-8");
222 1
            },
223
            'lcfirst' => function($string) {
224 1
                return lcfirst($string);
225 1
            },
226
            'ltrim' => function($string, $charlist = " \t\n\r\0\x0B") {
227 1
                return ltrim($string, $charlist);
228 1
            },
229
            'rtrim' => function($string, $charlist = " \t\n\r\0\x0B") {
230 1
                return rtrim($string, $charlist);
231 1
            },
232
            'str_repeat' => function($string, $multiplier = 1) {
233 1
                return str_repeat($string, $multiplier);
234 1
            },
235
            'plural' => function($string, $count = 2) {
236 1
                return str_plural($string, $count);
237 1
            },
238
            'strpad' => function($string, $pad_length, $pad_string = ' ') {
239 1
                return str_pad($string, $pad_length, $pad_string, $pad_type = STR_PAD_BOTH);
240 1
            },
241
            'leftpad' => function($string, $pad_length, $pad_string = ' ') {
242 1
                return str_pad($string, $pad_length, $pad_string, $pad_type = STR_PAD_LEFT);
243 1
            },
244
            'rightpad' => function($string, $pad_length, $pad_string = ' ') {
245 1
                return str_pad($string, $pad_length, $pad_string, $pad_type = STR_PAD_RIGHT);
246 1
            },
247
            'var_dump' => function($expression) {
248 1
                ob_start();
249 1
                var_dump($expression);
1 ignored issue
show
Security Debugging Code introduced by
var_dump($expression); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
250 1
                $result = ob_get_clean();
251
252 1
                return $result;
253 1
            },
254 1
        ];
255
    }
256
257
    /**
258
     * Works like the config() helper function.
259
     *
260
     * @return array
261
     */
262 1
    private function getConfigFunction()
263
    {
264
        return [
265
            'config' => function($key = null, $default = null) {
266 1
                return config($key, $default);
267 1
            },
268 1
        ];
269
    }
270
271
    /**
272
     * Works like the session() helper function.
273
     *
274
     * @return array
275
     */
276 1
    private function getSessionFunction()
277
    {
278
        return [
279
            'session' => function($key = null) {
280 1
                return session($key);
281 1
            },
282 1
        ];
283
    }
284
285
    /**
286
     * Works like the trans() helper function.
287
     *
288
     * @return array
289
     */
290 1
    private function getTransFunction()
291
    {
292
        return [
293
            'trans' => function($key = null) {
294 1
                return trans($key);
295 1
            },
296 1
        ];
297
    }
298
299
    /**
300
     * Dumps information about a variable.
301
     *
302
     * @return array
303
     */
304
    private function getVarDumpFunction()
305
    {
306
        return [
307 1
            'var_dump' => function($expression) {
308 1
                ob_start();
309 1
                var_dump($expression);
1 ignored issue
show
Security Debugging Code introduced by
var_dump($expression); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
310 1
                $result = ob_get_clean();
311
312 1
                return $result;
313 1
            },
314 1
        ];
315
    }
316
}
317