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\Library, |
13
|
|
|
gplcart\core\Module; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Main class for Twig module |
17
|
|
|
*/ |
18
|
|
|
class Twig |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* An array of TWIG instances keyed by a file directory |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $twig = array(); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Module class instance |
29
|
|
|
* @var \gplcart\core\Module $module |
30
|
|
|
*/ |
31
|
|
|
protected $module; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Library class instance |
35
|
|
|
* @var \gplcart\core\Library $library |
36
|
|
|
*/ |
37
|
|
|
protected $library; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param Module $module |
41
|
|
|
* @param Library $library |
42
|
|
|
*/ |
43
|
|
|
public function __construct(Module $module, Library $library) |
44
|
|
|
{ |
45
|
|
|
$this->module = $module; |
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
|
|
|
$routes['admin/module/settings/twig'] = array( |
80
|
|
|
'access' => 'module_edit', |
81
|
|
|
'handlers' => array( |
82
|
|
|
'controller' => array('gplcart\\modules\\twig\\controllers\\Settings', 'editSettings') |
83
|
|
|
) |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Implements hook "template.render" |
89
|
|
|
* @param array $templates |
90
|
|
|
* @param array $data |
91
|
|
|
* @param null|string $rendered |
92
|
|
|
* @param \gplcart\core\Controller $controller |
93
|
|
|
*/ |
94
|
|
|
public function hookTemplateRender($templates, $data, &$rendered, $controller) |
95
|
|
|
{ |
96
|
|
|
$this->setRenderedTemplate($templates, $data, $rendered, $controller); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Implements hook "module.enable.after" |
101
|
|
|
*/ |
102
|
|
|
public function hookModuleEnableAfter() |
103
|
|
|
{ |
104
|
|
|
$this->library->clearCache(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Implements hook "module.disable.after" |
109
|
|
|
*/ |
110
|
|
|
public function hookModuleDisableAfter() |
111
|
|
|
{ |
112
|
|
|
$this->library->clearCache(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Implements hook "module.install.after" |
117
|
|
|
*/ |
118
|
|
|
public function hookModuleInstallAfter() |
119
|
|
|
{ |
120
|
|
|
$this->library->clearCache(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Implements hook "module.uninstall.after" |
125
|
|
|
*/ |
126
|
|
|
public function hookModuleUninstallAfter() |
127
|
|
|
{ |
128
|
|
|
$this->library->clearCache(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Returns a TWIG instance for the given file directory |
133
|
|
|
* @param string $path |
134
|
|
|
* @param \gplcart\core\Controller $controller |
135
|
|
|
* @return \Twig_Environment |
136
|
|
|
* @throws \InvalidArgumentException |
137
|
|
|
*/ |
138
|
|
|
public function getTwigInstance($path, $controller) |
139
|
|
|
{ |
140
|
|
|
if (!$controller instanceof \gplcart\core\Controller) { |
|
|
|
|
141
|
|
|
throw new \InvalidArgumentException('Second argument must be instance of \gplcart\core\Controller'); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$options = array(); |
145
|
|
|
|
146
|
|
|
if (empty($this->twig)) { |
147
|
|
|
$this->library->load('twig'); |
148
|
|
|
$options = $this->module->getSettings('twig'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if (isset($this->twig[$path])) { |
152
|
|
|
return $this->twig[$path]; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if (!empty($options['cache'])) { |
156
|
|
|
$options['cache'] = __DIR__ . '/cache'; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$twig = new \Twig_Environment(new \Twig_Loader_Filesystem($path), $options); |
160
|
|
|
|
161
|
|
|
if (!empty($options['debug'])) { |
162
|
|
|
$twig->addExtension(new \Twig_Extension_Debug()); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
foreach ($this->getDefaultFunctions($controller) as $function) { |
166
|
|
|
$twig->addFunction($function); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $this->twig[$path] = $twig; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Renders a .twig template |
174
|
|
|
* @param string $template |
175
|
|
|
* @param array $data |
176
|
|
|
* @param \gplcart\core\Controller $controller |
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
|
|
public function render($template, $data, $controller) |
180
|
|
|
{ |
181
|
|
|
try { |
182
|
|
|
$parts = explode('/', $template); |
183
|
|
|
$file = array_pop($parts); |
184
|
|
|
$twig = $this->getTwigInstance(implode('/', $parts), $controller); |
185
|
|
|
$controller_data = $controller->getData(); |
186
|
|
|
return $twig->loadTemplate($file)->render(array_merge($controller_data, $data)); |
187
|
|
|
} catch (\Exception $ex) { |
188
|
|
|
return $ex->getMessage(); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Validate a TWIG template syntax |
194
|
|
|
* @param string $file |
195
|
|
|
* @param \gplcart\core\Controller $controller |
196
|
|
|
* @return boolean|string |
197
|
|
|
*/ |
198
|
|
|
public function validate($file, $controller) |
199
|
|
|
{ |
200
|
|
|
try { |
201
|
|
|
$pathinfo = pathinfo($file); |
202
|
|
|
$twig = $this->getTwigInstance($pathinfo['dirname'], $controller); |
203
|
|
|
$content = file_get_contents($file); |
204
|
|
|
$twig->parse($twig->tokenize(new \Twig_Source($content, $pathinfo['basename']))); |
205
|
|
|
return true; |
206
|
|
|
} catch (\Exception $ex) { |
207
|
|
|
return $ex->getMessage(); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Sets rendered .twig template |
213
|
|
|
* @param array $templates |
214
|
|
|
* @param array $data |
215
|
|
|
* @param null|string $rendered |
216
|
|
|
* @param \gplcart\core\Controller $controller |
217
|
|
|
*/ |
218
|
|
|
protected function setRenderedTemplate($templates, $data, &$rendered, $controller) |
219
|
|
|
{ |
220
|
|
|
list($original, $overridden) = $templates; |
221
|
|
|
|
222
|
|
|
if (is_file("$overridden.twig")) { |
223
|
|
|
$rendered = $this->render("$overridden.twig", $data, $controller); |
224
|
|
|
} else if (is_file("$original.twig")) { |
225
|
|
|
$rendered = $this->render("$original.twig", $data, $controller); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Adds custom functions and returns an array of Twig_SimpleFunction objects |
231
|
|
|
* @param \gplcart\core\Controller $controller |
232
|
|
|
* @return array |
233
|
|
|
*/ |
234
|
|
|
protected function getDefaultFunctions($controller) |
235
|
|
|
{ |
236
|
|
|
$functions = array(); |
237
|
|
|
|
238
|
|
|
$functions[] = new \Twig_SimpleFunction('error', function ($key = null, $has_error = null, $no_error = '') use ($controller) { |
239
|
|
|
return $controller->error($key, $has_error, $no_error); |
240
|
|
|
}, array('is_safe' => array('all'))); |
241
|
|
|
|
242
|
|
|
$functions[] = new \Twig_SimpleFunction('text', function ($text, $arguments = array()) use ($controller) { |
243
|
|
|
return $controller->text($text, $arguments); |
244
|
|
|
}, array('is_safe' => array('all'))); |
245
|
|
|
|
246
|
|
|
$functions[] = new \Twig_SimpleFunction('access', function ($permission) use ($controller) { |
247
|
|
|
return $controller->access($permission); |
248
|
|
|
}); |
249
|
|
|
|
250
|
|
|
$functions[] = new \Twig_SimpleFunction('url', function ($path = '', array $query = array(), $absolute = false) use ($controller) { |
251
|
|
|
return $controller->url($path, $query, $absolute); |
252
|
|
|
}); |
253
|
|
|
|
254
|
|
|
$functions[] = new \Twig_SimpleFunction('date', function ($timestamp = null, $full = true, $unix_format = '') use ($controller) { |
255
|
|
|
return $controller->date($timestamp, $full, $unix_format); |
256
|
|
|
}); |
257
|
|
|
|
258
|
|
|
$functions[] = new \Twig_SimpleFunction('attributes', function ($attributes) use ($controller) { |
259
|
|
|
return $controller->attributes($attributes); |
260
|
|
|
}, array('is_safe' => array('all'))); |
261
|
|
|
|
262
|
|
|
$functions[] = new \Twig_SimpleFunction('config', function ($key = null, $default = null) use ($controller) { |
263
|
|
|
return $controller->config($key, $default); |
264
|
|
|
}); |
265
|
|
|
|
266
|
|
|
$functions[] = new \Twig_SimpleFunction('configTheme', function ($key = null, $default = null) use ($controller) { |
267
|
|
|
return $controller->configTheme($key, $default); |
268
|
|
|
}); |
269
|
|
|
|
270
|
|
|
$functions[] = new \Twig_SimpleFunction('teaser', function ($text, $xss = false, $filter = null) use ($controller) { |
271
|
|
|
return $controller->teaser($text, $xss, $filter); |
272
|
|
|
}, array('is_safe' => array('all'))); |
273
|
|
|
|
274
|
|
|
$functions[] = new \Twig_SimpleFunction('filter', function ($text, $filter = null) use ($controller) { |
275
|
|
|
return $controller->filter($text, $filter); |
276
|
|
|
}, array('is_safe' => array('all'))); |
277
|
|
|
|
278
|
|
|
$functions[] = new \Twig_SimpleFunction('truncate', function ($string, $length = 100, $trimmarker = '...') use ($controller) { |
279
|
|
|
return $controller->truncate($string, $length, $trimmarker); |
280
|
|
|
}); |
281
|
|
|
|
282
|
|
|
$functions[] = new \Twig_SimpleFunction('path', function ($path = null) use ($controller) { |
283
|
|
|
return $controller->path($path); |
284
|
|
|
}); |
285
|
|
|
|
286
|
|
|
return $functions; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
} |
290
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.