|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Selami\View\Twig; |
|
5
|
|
|
|
|
6
|
|
|
use Selami\View\ExtensionsAbstract; |
|
7
|
|
|
use Twig_Environment; |
|
8
|
|
|
use Camel\CaseTransformer; |
|
9
|
|
|
use Camel\Format; |
|
10
|
|
|
use InvalidArgumentException; |
|
11
|
|
|
use BadMethodCallException; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class TwigExtensions extends ViewExtensionsAbstracts |
|
15
|
|
|
* |
|
16
|
|
|
* @package Selami\View\Twig |
|
17
|
|
|
*/ |
|
18
|
|
|
class TwigExtensions extends ExtensionsAbstract |
|
19
|
|
|
{ |
|
20
|
|
|
private $twig; |
|
21
|
|
|
private $config; |
|
22
|
|
|
private $toCamelCase; |
|
23
|
|
|
private $toSnakeCase; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct(Twig_Environment $twig, array $config) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->twig = $twig; |
|
28
|
|
|
$this->config = $config; |
|
29
|
|
|
$this->toCamelCase = new CaseTransformer(new Format\SnakeCase, new Format\StudlyCaps); |
|
30
|
|
|
$this->toSnakeCase = new CaseTransformer(new Format\CamelCase, new Format\SnakeCase); |
|
31
|
|
|
$this->loadExtensions(); |
|
32
|
|
|
$this->loadFunctions(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
protected function loadExtensions() : void |
|
36
|
|
|
{ |
|
37
|
|
|
$this->twig->addExtension(new \Twig_Extensions_Extension_Date()); |
|
38
|
|
|
$this->twig->addExtension(new \Twig_Extensions_Extension_Intl()); |
|
39
|
|
|
$this->twig->addExtension(new \Twig_Extensions_Extension_Text()); |
|
40
|
|
|
$this->twig->addExtension(new \Twig_Extensions_Extension_I18n()); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Build translate array. |
|
45
|
|
|
* |
|
46
|
|
|
* @param $translateArray |
|
47
|
|
|
* @return array |
|
48
|
|
|
*/ |
|
49
|
|
|
private function buildTranslateArray(array $translateArray) : array |
|
|
|
|
|
|
50
|
|
|
{ |
|
51
|
|
|
$tmpArray = []; |
|
52
|
|
|
foreach ($translateArray as $key => $value) { |
|
53
|
|
|
$tmpArray['@'.$key] = $value; |
|
54
|
|
|
} |
|
55
|
|
|
return $tmpArray; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
protected function extendForGetUrl() : void |
|
59
|
|
|
{ |
|
60
|
|
|
$filter = new \Twig_SimpleFunction( |
|
61
|
|
|
'getUrl', |
|
62
|
|
|
function ( |
|
63
|
|
|
$alias, |
|
64
|
|
|
$params = [] |
|
65
|
|
|
) { |
|
66
|
|
|
if (array_key_exists($alias, $this->config['aliases'])) { |
|
67
|
|
|
$data = $this->config['aliases'][$alias]; |
|
68
|
|
|
$relative_path = $data; |
|
69
|
|
|
foreach ($params as $param => $value) { |
|
70
|
|
|
if (strpos($param, ':') === strlen($param)-1) { |
|
71
|
|
|
$relative_path = preg_replace('/{'.$param.'(.*?)}/msi', $value, $relative_path); |
|
72
|
|
|
} else { |
|
73
|
|
|
$relative_path = str_replace('{'.$param.'}', $value, $relative_path); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
return $this->config['base_url'] . '/' . $relative_path; |
|
77
|
|
|
} |
|
78
|
|
|
return ''; |
|
79
|
|
|
}, |
|
80
|
|
|
array('is_safe' => array('html')) |
|
81
|
|
|
); |
|
82
|
|
|
$this->twig->addFunction($filter); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
protected function extendForWidget() : void |
|
86
|
|
|
{ |
|
87
|
|
|
$filter = new \Twig_SimpleFunction( |
|
88
|
|
|
'Widget_*_*', |
|
89
|
|
|
function ($widgetNameStr, $widgetActionStr, $args = []) { |
|
90
|
|
|
$widgetAction = $this->toCamelCase->transform($widgetActionStr); |
|
91
|
|
|
$widgetName = $this->toCamelCase->transform($widgetNameStr); |
|
92
|
|
|
$widget = '\\' . $this->config['app_namespace'] . '\\Widget\\' . $widgetName; |
|
93
|
|
|
if (!class_exists($widget)) { |
|
94
|
|
|
$message = 'Widget ' . $widgetName . '_' . $widgetAction . ' has not class name as ' . $widget; |
|
95
|
|
|
throw new BadMethodCallException($message); |
|
96
|
|
|
} |
|
97
|
|
|
$widgetInstance = new $widget($args); |
|
98
|
|
|
if (!method_exists($widgetInstance, $widgetAction)) { |
|
99
|
|
|
$message = 'Widget ' . $widget . ' has not method name as ' . $widgetAction; |
|
100
|
|
|
throw new BadMethodCallException($message); |
|
101
|
|
|
} |
|
102
|
|
|
$templateFileBasename = $args['template'] ?? $this->toSnakeCase->transform($widgetActionStr) . '.twig'; |
|
103
|
|
|
$templateFullPath = $this->config['templates_dir'] . '/_widgets/' |
|
104
|
|
|
. $this->toSnakeCase->transform($widgetNameStr) . '/' . $templateFileBasename; |
|
105
|
|
|
|
|
106
|
|
|
if (!file_exists($templateFullPath)) { |
|
107
|
|
|
$message = sprintf( |
|
108
|
|
|
'%s template file not found! %s needs a main template file at: %s', |
|
109
|
|
|
$templateFileBasename, |
|
110
|
|
|
$widgetNameStr . '_' . $widgetActionStr, |
|
111
|
|
|
$templateFullPath |
|
112
|
|
|
); |
|
113
|
|
|
throw new InvalidArgumentException($message); |
|
114
|
|
|
} |
|
115
|
|
|
$templateFile = '_widgets/' |
|
116
|
|
|
. $this->toSnakeCase->transform($widgetNameStr) . '/' . $templateFileBasename; |
|
117
|
|
|
$widgetData = $widgetInstance->{$widgetAction}(); |
|
118
|
|
|
return $this->twig->render($templateFile, $widgetData); |
|
119
|
|
|
}, |
|
120
|
|
|
array('is_safe' => array('html')) |
|
121
|
|
|
); |
|
122
|
|
|
$this->twig->addFunction($filter); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
View Code Duplication |
protected function extendForQueryParams() : void |
|
|
|
|
|
|
126
|
|
|
{ |
|
127
|
|
|
$filter = new \Twig_SimpleFunction( |
|
128
|
|
|
'queryParams', |
|
129
|
|
|
function ($queryParams, $prefix = '?') { |
|
130
|
|
|
return $prefix . http_build_query($queryParams); |
|
131
|
|
|
}, |
|
132
|
|
|
array('is_safe' => array('html')) |
|
133
|
|
|
); |
|
134
|
|
|
$this->twig->addFunction($filter); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
View Code Duplication |
protected function extendForSiteUrl() : void |
|
|
|
|
|
|
138
|
|
|
{ |
|
139
|
|
|
$filter = new \Twig_SimpleFunction( |
|
140
|
|
|
'siteUrl', |
|
141
|
|
|
function ($path = '') { |
|
142
|
|
|
return $this->config['base_url'] . $path; |
|
143
|
|
|
}, |
|
144
|
|
|
array('is_safe' => array('html')) |
|
145
|
|
|
); |
|
146
|
|
|
$this->twig->addFunction($filter); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
protected function extendForVarDump() : void |
|
150
|
|
|
{ |
|
151
|
|
|
$filter = new \Twig_SimpleFunction( |
|
152
|
|
|
'varDump', |
|
153
|
|
|
function ($args) { |
|
154
|
|
|
/** |
|
155
|
|
|
* @noinspection ForgottenDebugOutputInspection |
|
156
|
|
|
*/ |
|
157
|
|
|
var_dump($args); |
|
|
|
|
|
|
158
|
|
|
}, |
|
159
|
|
|
array('is_safe' => array('html')) |
|
160
|
|
|
); |
|
161
|
|
|
$this->twig->addFunction($filter); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
protected function extendForPagination() : void |
|
165
|
|
|
{ |
|
166
|
|
|
/** |
|
167
|
|
|
* @noinspection MoreThanThreeArgumentsInspection |
|
168
|
|
|
*/ |
|
169
|
|
|
$filter = new \Twig_SimpleFunction( |
|
170
|
|
|
'Pagination', |
|
171
|
|
|
function ( |
|
172
|
|
|
int $total, |
|
173
|
|
|
int $current, |
|
174
|
|
|
string $linkTemplate, |
|
175
|
|
|
string $parentTemplate = '<ul class="pagination">(items)</ul>', |
|
176
|
|
|
string $itemTemplate = '<li class="(item_class)">(link)</li>', |
|
177
|
|
|
string $linkItemTemplate = '<a href="(href)" class="(link_class)">(text)</a>', |
|
178
|
|
|
string $ellipsesTemplate = '<li><a>...</a></li>' |
|
179
|
|
|
) { |
|
180
|
|
|
$items = ''; |
|
181
|
|
|
$pageGroupLimit = 3; |
|
182
|
|
|
$useEllipses = ($total >10) ? 1 : 0; |
|
183
|
|
|
$renderedEllipses = 0; |
|
184
|
|
|
$values = [ |
|
185
|
|
|
'(item_class)' => '', |
|
186
|
|
|
'(href)' => '', |
|
187
|
|
|
'(link_class)' => '', |
|
188
|
|
|
'(text)' => '' |
|
189
|
|
|
]; |
|
190
|
|
|
for ($i=1; $i <= $total; $i++) { |
|
191
|
|
|
$values['(link_class)'] = ''; |
|
192
|
|
|
$values['(item_class)'] = ''; |
|
193
|
|
|
if ($i === $current) { |
|
194
|
|
|
$values['(link_class)'] = 'active'; |
|
195
|
|
|
$values['(item_class)'] = 'active'; |
|
196
|
|
|
} |
|
197
|
|
|
$values['(text)'] = $i; |
|
198
|
|
|
$values['(href)'] = str_replace('(page_num)', $i, $linkTemplate); |
|
199
|
|
|
$link = strtr($linkItemTemplate, $values); |
|
200
|
|
|
$useLink = 1; |
|
201
|
|
|
if ($useEllipses === 1) { |
|
202
|
|
|
$useLink = 0; |
|
203
|
|
|
if (($i <= $pageGroupLimit) |
|
204
|
|
|
|| ($i >= ($current-1) && $i <= ($current+1)) |
|
205
|
|
|
|| ($i >= ($total-2)) |
|
206
|
|
|
) { |
|
207
|
|
|
$useLink = 1; |
|
208
|
|
|
$renderedEllipses = 0; |
|
209
|
|
|
} else { |
|
210
|
|
|
if ($renderedEllipses === 0) { |
|
211
|
|
|
$items .= $ellipsesTemplate; |
|
212
|
|
|
} |
|
213
|
|
|
$renderedEllipses = 1; |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
if ($useLink === 1) { |
|
217
|
|
|
$thisItemTemplate = str_replace( |
|
218
|
|
|
array('(link)'), |
|
219
|
|
|
array($link), |
|
220
|
|
|
$itemTemplate |
|
221
|
|
|
); |
|
222
|
|
|
$items .= strtr($thisItemTemplate, $values); |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
return str_replace('(items)', $items, $parentTemplate); |
|
226
|
|
|
}, |
|
227
|
|
|
array('is_safe' => array('html')) |
|
228
|
|
|
); |
|
229
|
|
|
$this->twig->addFunction($filter); |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|