Skip to content

Commit 25c9110

Browse files
PatchRangersamdark
authored andcommitted
Added support for callable as a source for Twig_Function and Twig_Filter
See yiiext/twig-renderer#32 .
1 parent 1ba2613 commit 25c9110

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

ViewRenderer.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -230,25 +230,25 @@ public function setLexerOptions($options)
230230
*/
231231
private function _addCustom($classType, $elements)
232232
{
233-
$classFunction = 'Twig_' . $classType . '_Function';
233+
$classFunction = 'Twig_' . $classType;
234234

235235
foreach ($elements as $name => $func) {
236236
$twigElement = null;
237237

238238
switch ($func) {
239-
// Just a name of function
240-
case is_string($func):
241-
$twigElement = new $classFunction($func);
239+
// Callable (including just a name of function).
240+
case is_callable($func):
241+
$twigElement = new $classFunction($name, $func);
242242
break;
243-
// Name of function + options array
244-
case is_array($func) && is_string($func[0]) && isset($func[1]) && is_array($func[1]):
245-
$twigElement = new $classFunction($func[0], $func[1]);
243+
// Callable (including just a name of function) + options array.
244+
case is_array($func) && is_callable($func[0]):
245+
$twigElement = new $classFunction($name, $func[0], (!empty($func[1]) && is_array($func[1])) ? $func[1] : []);
246246
break;
247247
}
248248

249249
if ($twigElement !== null) {
250-
$this->twig->{'add'.$classType}($name, $twigElement);
251-
} elseif ($func instanceof \Twig_SimpleFunction || $func instanceof \Twig_SimpleFilter) {
250+
$this->twig->{'add'.$classType}($twigElement);
251+
} elseif ($func instanceof \Twig_Function || $func instanceof \Twig_Filter) {
252252
$this->twig->{'add'.$classType}($func);
253253
} else {
254254
throw new \Exception("Incorrect options for \"$classType\" $name.");

0 commit comments

Comments
 (0)