-
Notifications
You must be signed in to change notification settings - Fork 26
/
Plugin.php
187 lines (164 loc) · 5.05 KB
/
Plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php namespace VojtaSvoboda\TwigExtensions;
use Carbon\Carbon;
use Event;
use System\Classes\PluginBase;
use Twig\Environment;
use Twig\Extension\StringLoaderExtension;
use Twig\Extra\Intl\IntlExtension;
/**
* Twig Extensions Plugin.
*/
class Plugin extends PluginBase
{
/**
* Returns information about this plugin.
*
* @return array
*/
public function pluginDetails()
{
return [
'name' => 'Twig Extensions',
'description' => 'Add more Twig filters to your templates.',
'author' => 'Vojta Svoboda',
'icon' => 'icon-plus',
'homepage' => 'https://github.com/vojtasvoboda/oc-twigextensions-plugin',
];
}
public function boot()
{
// add Intl extension if php_intl.dll installed
if (class_exists('IntlDateFormatter')) {
Event::listen('system.extendTwig', function(Environment $twig) {
$twig->addExtension(new IntlExtension());
});
}
}
/**
* Add Twig extensions.
*/
public function registerMarkupTags(): array
{
$filters = [];
$functions = [];
// add String Loader functions
$functions += $this->getStringLoaderFunctions();
// add Session function
$functions += $this->getSessionFunction();
// add Trans function
$functions += $this->getTransFunction();
// add var_dump function
$functions += $this->getVarDumpFunction();
// add PHP functions
$filters += $this->getPhpFunctions();
// add File Version filter
$filters += $this->getFileRevision();
return [
'filters' => $filters,
'functions' => $functions,
];
}
/**
* Returns String Loader functions.
*/
private function getStringLoaderFunctions(): array
{
$stringLoader = new StringLoaderExtension();
$functions = [];
foreach ($stringLoader->getFunctions() as $function) {
$functions[$function->getName()] = $function->getCallable();
}
return $functions;
}
/**
* Returns plain PHP functions.
*/
private function getPhpFunctions(): array
{
return [
'strftime' => function($time, $format = '%d.%m.%Y %H:%M:%S') {
$timeObj = new Carbon($time);
return strftime($format, $timeObj->getTimestamp());
},
'ltrim' => function($string, $charlist = " \t\n\r\0\x0B") {
return ltrim($string, $charlist);
},
'rtrim' => function($string, $charlist = " \t\n\r\0\x0B") {
return rtrim($string, $charlist);
},
'strip_tags' => function($string, $allow = '') {
return strip_tags($string, $allow);
},
'var_dump' => function($expression) {
ob_start();
var_dump($expression);
$result = ob_get_clean();
return $result;
},
'wordwrap' => function(string $string, int $width = 75, string $break = "\n", bool $cut_long_words = false) {
return wordwrap($string, $width, $break, $cut_long_words);
}
];
}
/**
* Works like the session() helper function.
*/
private function getSessionFunction(): array
{
return [
'session' => function($key = null) {
return session($key);
},
];
}
/**
* Works like the trans() helper function.
*/
private function getTransFunction(): array
{
return [
'trans' => function($key = null, $parameters = []) {
return trans($key, $parameters);
},
];
}
/**
* Dumps information about a variable.
*/
private function getVarDumpFunction(): array
{
return [
'var_dump' => function($expression) {
ob_start();
var_dump($expression);
$result = ob_get_clean();
return $result;
},
];
}
/**
* Appends this pattern: ? . {last modified date}
* to an assets filename to force browser to reload
* cached modified file.
*
* See: https://github.com/vojtasvoboda/oc-twigextensions-plugin/issues/25
*
* @return array
*/
private function getFileRevision()
{
return [
'revision' => function($filename, $format = null) {
// Remove http/web address from the file name if there is one to load it locally
$prefix = url('/');
$filename_ = trim(preg_replace('/^'.preg_quote($prefix, '/').'/', '', $filename), '/');
if (file_exists($filename_)) {
$timestamp = filemtime($filename_);
$prepend = ($format) ? date($format, $timestamp) : $timestamp;
return $filename.'?'.$prepend;
}
return $filename;
},
];
}
}