Skip to content

Commit

Permalink
Added support for callable as a source for Twig_Function and Twig_Filter
Browse files Browse the repository at this point in the history
  • Loading branch information
PatchRanger committed Mar 30, 2015
1 parent 8a86383 commit 18d1949
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions ViewRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,25 +230,25 @@ public function setLexerOptions($options)
*/
private function _addCustom($classType, $elements)
{
$classFunction = 'Twig_' . $classType . '_Function';
$classFunction = 'Twig_' . $classType;

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

switch ($func) {
// Just a name of function
case is_string($func):
$twigElement = new $classFunction($func);
// Callable (including just a name of function).
case is_callable($func):
$twigElement = new $classFunction($name, $func);
break;
// Name of function + options array
case is_array($func) && is_string($func[0]) && isset($func[1]) && is_array($func[1]):
$twigElement = new $classFunction($func[0], $func[1]);
// Callable (including just a name of function) + options array.
case is_array($func) && is_callable($func[0]):
$twigElement = new $classFunction($name, $func[0], (!empty($func[1]) && is_array($func[1])) ? $func[1] : []);
break;
}

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

0 comments on commit 18d1949

Please sign in to comment.