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