|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package Twig |
|
5
|
|
|
* @author Iurii Makukh <[email protected]> |
|
6
|
|
|
* @copyright Copyright (c) 2015, Iurii Makukh |
|
7
|
|
|
* @license https://www.gnu.org/licenses/gpl.html GNU/GPLv3 |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace gplcart\modules\twig; |
|
11
|
|
|
|
|
12
|
|
|
use gplcart\core\Module, |
|
13
|
|
|
gplcart\core\Library; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Main class for Twig module |
|
17
|
|
|
*/ |
|
18
|
|
|
class Twig extends Module |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Library class instance |
|
23
|
|
|
* @var \gplcart\core\Library |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $library; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* An array of TWIG instances keyed by file directory |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $twig = array(); |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* An array of module settings |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $settings = array(); |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param Library $library |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(Library $library) |
|
43
|
|
|
{ |
|
44
|
|
|
parent::__construct(); |
|
45
|
|
|
|
|
46
|
|
|
$this->library = $library; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Implements hook "library.list" |
|
51
|
|
|
* @param array $libraries |
|
52
|
|
|
*/ |
|
53
|
|
|
public function hookLibraryList(array &$libraries) |
|
54
|
|
|
{ |
|
55
|
|
|
$libraries['twig'] = array( |
|
56
|
|
|
'name' => 'Twig', |
|
57
|
|
|
'description' => 'Twig is a template engine for PHP', |
|
58
|
|
|
'url' => 'https://github.com/twigphp/Twig', |
|
59
|
|
|
'download' => 'https://github.com/twigphp/Twig/archive/v1.33.0.zip', |
|
60
|
|
|
'type' => 'php', |
|
61
|
|
|
'module' => 'twig', |
|
62
|
|
|
'version_source' => array( |
|
63
|
|
|
'lines' => 100, |
|
64
|
|
|
'pattern' => '/.*VERSION.*(\\d+\\.+\\d+\\.+\\d+)/', |
|
65
|
|
|
'file' => 'vendor/twig/twig/lib/Twig/Environment.php' |
|
66
|
|
|
), |
|
67
|
|
|
'files' => array( |
|
68
|
|
|
'vendor/autoload.php' |
|
69
|
|
|
) |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Implements hook "route.list" |
|
75
|
|
|
* @param array $routes |
|
76
|
|
|
*/ |
|
77
|
|
|
public function hookRouteList(array &$routes) |
|
78
|
|
|
{ |
|
79
|
|
|
// Module settings page |
|
80
|
|
|
$routes['admin/module/settings/twig'] = array( |
|
81
|
|
|
'access' => 'module_edit', |
|
82
|
|
|
'handlers' => array( |
|
83
|
|
|
'controller' => array('gplcart\\modules\\twig\\controllers\\Settings', 'editSettings') |
|
84
|
|
|
) |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Implements hook "template.render" |
|
90
|
|
|
* @param string $template |
|
91
|
|
|
* @param array $data |
|
92
|
|
|
* @param null|string $rendered |
|
93
|
|
|
* @param \gplcart\core\Controller $object |
|
94
|
|
|
*/ |
|
95
|
|
|
public function hookTemplateRender($template, $data, &$rendered, $object) |
|
96
|
|
|
{ |
|
97
|
|
|
$template .= '.twig'; |
|
98
|
|
|
|
|
99
|
|
|
if (is_file($template)) { |
|
100
|
|
|
$rendered = $this->render($template, $data, $object); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Returns a TWIG instance for the given file directory |
|
106
|
|
|
* @param string $path |
|
107
|
|
|
* @param \gplcart\core\Controller $object |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getTwigInstance($path, $object) |
|
110
|
|
|
{ |
|
111
|
|
|
if (empty($this->twig)) { |
|
112
|
|
|
$this->library->load('twig'); |
|
113
|
|
|
$this->settings = $this->config->module('twig'); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
if (isset($this->twig[$path])) { |
|
117
|
|
|
return $this->twig[$path]; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$options = $this->settings; |
|
121
|
|
|
|
|
122
|
|
|
if (!empty($this->settings['cache'])) { |
|
123
|
|
|
$options['cache'] = __DIR__ . '/cache'; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$twig = new \Twig_Environment(new \Twig_Loader_Filesystem($path), $options); |
|
127
|
|
|
|
|
128
|
|
|
if (!empty($options['debug'])) { |
|
129
|
|
|
$twig->addExtension(new \Twig_Extension_Debug()); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
foreach ($this->getDefaultFunctions($object) as $function) { |
|
133
|
|
|
$twig->addFunction($function); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return $this->twig[$path] = $twig; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Renders a .twig template |
|
141
|
|
|
* @param string $template |
|
142
|
|
|
* @param array $data |
|
143
|
|
|
* @param \gplcart\core\Controller $object |
|
144
|
|
|
* @return string |
|
145
|
|
|
*/ |
|
146
|
|
|
public function render($template, $data, $object) |
|
147
|
|
|
{ |
|
148
|
|
|
$parts = explode('/', $template); |
|
149
|
|
|
$file = array_pop($parts); |
|
150
|
|
|
|
|
151
|
|
|
$twig = $this->getTwigInstance(implode('/', $parts), $object); |
|
152
|
|
|
|
|
153
|
|
|
$controller_data = $object->getData(); |
|
154
|
|
|
$merged = gplcart_array_merge($controller_data, $data); |
|
155
|
|
|
|
|
156
|
|
|
return $twig->loadTemplate($file)->render($merged); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Adds custom functions and returns an array of Twig_SimpleFunction objects |
|
161
|
|
|
* @param \gplcart\core\Controller $object |
|
162
|
|
|
* @return array |
|
163
|
|
|
*/ |
|
164
|
|
|
protected function getDefaultFunctions($object) |
|
165
|
|
|
{ |
|
166
|
|
|
$functions = array(); |
|
167
|
|
|
|
|
168
|
|
|
$functions[] = new \Twig_SimpleFunction('error', function ($key = null, $has_error = null, $no_error = '') use ($object) { |
|
169
|
|
|
return $object->error($key, $has_error, $no_error); |
|
170
|
|
|
}, array('is_safe' => array('all'))); |
|
171
|
|
|
|
|
172
|
|
|
$functions[] = new \Twig_SimpleFunction('text', function ($text, $arguments = array()) use ($object) { |
|
173
|
|
|
return $object->text($text, $arguments); |
|
174
|
|
|
}, array('is_safe' => array('all'))); |
|
175
|
|
|
|
|
176
|
|
|
$functions[] = new \Twig_SimpleFunction('access', function ($permission) use ($object) { |
|
177
|
|
|
return $object->access($permission); |
|
178
|
|
|
}); |
|
179
|
|
|
|
|
180
|
|
|
$functions[] = new \Twig_SimpleFunction('url', function ($path = '', array $query = array(), $absolute = false) use ($object) { |
|
181
|
|
|
return $object->url($path, $query, $absolute); |
|
182
|
|
|
}); |
|
183
|
|
|
|
|
184
|
|
|
$functions[] = new \Twig_SimpleFunction('isSuperadmin', function ($user_id = null) use ($object) { |
|
185
|
|
|
return $object->isSuperadmin($user_id); |
|
186
|
|
|
}); |
|
187
|
|
|
|
|
188
|
|
|
$functions[] = new \Twig_SimpleFunction('date', function ($timestamp = null, $full = true, $unix_format = '') use ($object) { |
|
189
|
|
|
return $object->date($timestamp, $full, $unix_format); |
|
190
|
|
|
}); |
|
191
|
|
|
|
|
192
|
|
|
$functions[] = new \Twig_SimpleFunction('attributes', function ($attributes) use ($object) { |
|
193
|
|
|
return $object->attributes($attributes); |
|
194
|
|
|
}, array('is_safe' => array('all'))); |
|
195
|
|
|
|
|
196
|
|
|
$functions[] = new \Twig_SimpleFunction('config', function ($key = null, $default = null) use ($object) { |
|
197
|
|
|
return $object->config($key, $default); |
|
198
|
|
|
}); |
|
199
|
|
|
|
|
200
|
|
|
$functions[] = new \Twig_SimpleFunction('settings', function ($key = null, $default = null) use ($object) { |
|
201
|
|
|
return $object->settings($key, $default); |
|
202
|
|
|
}); |
|
203
|
|
|
|
|
204
|
|
|
$functions[] = new \Twig_SimpleFunction('summary', function ($text, $xss = false, $filter = null) use ($object) { |
|
205
|
|
|
return $object->summary($text, $xss, $filter); |
|
206
|
|
|
}, array('is_safe' => array('all'))); |
|
207
|
|
|
|
|
208
|
|
|
$functions[] = new \Twig_SimpleFunction('user', function ($item = null) use ($object) { |
|
209
|
|
|
return $object->user($item); |
|
210
|
|
|
}); |
|
211
|
|
|
|
|
212
|
|
|
$functions[] = new \Twig_SimpleFunction('store', function ($item = null) use ($object) { |
|
213
|
|
|
return $object->store($item); |
|
214
|
|
|
}); |
|
215
|
|
|
|
|
216
|
|
|
$functions[] = new \Twig_SimpleFunction('d', function ($key = null) use ($object) { |
|
217
|
|
|
if (function_exists('d')) { |
|
218
|
|
|
d($object->getData($key)); // Kint |
|
219
|
|
|
} else { |
|
220
|
|
|
print_r($object->getData($key), true); |
|
221
|
|
|
} |
|
222
|
|
|
}); |
|
223
|
|
|
|
|
224
|
|
|
$functions[] = new \Twig_SimpleFunction('filter', function ($text, $filter = null) use ($object) { |
|
225
|
|
|
return $object->filter($text, $filter); |
|
226
|
|
|
}, array('is_safe' => array('all'))); |
|
227
|
|
|
|
|
228
|
|
|
$functions[] = new \Twig_SimpleFunction('truncate', function ($string, $length = 100, $trimmarker = '...') use ($object) { |
|
229
|
|
|
return $object->truncate($string, $length, $trimmarker); |
|
230
|
|
|
}); |
|
231
|
|
|
|
|
232
|
|
|
$functions[] = new \Twig_SimpleFunction('cart', function ($key = null) use ($object) { |
|
233
|
|
|
return $object->cart($key); |
|
234
|
|
|
}); |
|
235
|
|
|
|
|
236
|
|
|
$functions[] = new \Twig_SimpleFunction('compare', function ($key = null) use ($object) { |
|
237
|
|
|
return $object->compare($key); |
|
238
|
|
|
}); |
|
239
|
|
|
|
|
240
|
|
|
$functions[] = new \Twig_SimpleFunction('wishlist', function ($key = null) use ($object) { |
|
241
|
|
|
return $object->wishlist($key); |
|
242
|
|
|
}); |
|
243
|
|
|
|
|
244
|
|
|
$functions[] = new \Twig_SimpleFunction('menu', function (array $options = array()) use ($object) { |
|
245
|
|
|
return $object->menu($options); |
|
246
|
|
|
}, array('is_safe' => array('all'))); |
|
247
|
|
|
|
|
248
|
|
|
$functions[] = new \Twig_SimpleFunction('prop', function ($name) use ($object) { |
|
249
|
|
|
return $object->prop($name); |
|
250
|
|
|
}); |
|
251
|
|
|
|
|
252
|
|
|
$functions[] = new \Twig_SimpleFunction('path', function ($path = null) use ($object) { |
|
253
|
|
|
return $object->path($path); |
|
254
|
|
|
}); |
|
255
|
|
|
|
|
256
|
|
|
return $functions; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Implements hook "module.enable.after" |
|
261
|
|
|
*/ |
|
262
|
|
|
public function hookModuleEnableAfter() |
|
263
|
|
|
{ |
|
264
|
|
|
$this->library->clearCache(); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* Implements hook "module.disable.after" |
|
269
|
|
|
*/ |
|
270
|
|
|
public function hookModuleDisableAfter() |
|
271
|
|
|
{ |
|
272
|
|
|
$this->library->clearCache(); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* Implements hook "module.install.after" |
|
277
|
|
|
*/ |
|
278
|
|
|
public function hookModuleInstallAfter() |
|
279
|
|
|
{ |
|
280
|
|
|
$this->library->clearCache(); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Implements hook "module.uninstall.after" |
|
285
|
|
|
*/ |
|
286
|
|
|
public function hookModuleUninstallAfter() |
|
287
|
|
|
{ |
|
288
|
|
|
$this->library->clearCache(); |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
} |
|
292
|
|
|
|