From 18d1949f59c5e867b9ab90151e15380a2c187c90 Mon Sep 17 00:00:00 2001 From: Dmitry Danilson Date: Mon, 30 Mar 2015 13:09:37 +0500 Subject: [PATCH 1/2] Added support for callable as a source for Twig_Function and Twig_Filter See https://github.com/yiiext/twig-renderer/pull/32 . --- ViewRenderer.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/ViewRenderer.php b/ViewRenderer.php index ca78371b6..f1bb561f5 100644 --- a/ViewRenderer.php +++ b/ViewRenderer.php @@ -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."); From d8f9d46505fbcdd3dd502ffdeb259ec61ad37006 Mon Sep 17 00:00:00 2001 From: Dmitry Danilson Date: Tue, 31 Mar 2015 08:36:00 +0500 Subject: [PATCH 2/2] Callable as a source for Twig_Function and Twig_Filter: making tests passed --- ViewRenderer.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ViewRenderer.php b/ViewRenderer.php index f1bb561f5..98b7e14cc 100644 --- a/ViewRenderer.php +++ b/ViewRenderer.php @@ -230,7 +230,7 @@ public function setLexerOptions($options) */ private function _addCustom($classType, $elements) { - $classFunction = 'Twig_' . $classType; + $classFunction = 'Twig_Simple' . $classType; foreach ($elements as $name => $func) { $twigElement = null; @@ -248,7 +248,7 @@ private function _addCustom($classType, $elements) if ($twigElement !== null) { $this->twig->{'add'.$classType}($twigElement); - } elseif ($func instanceof \Twig_Function || $func instanceof \Twig_Filter) { + } elseif ($func instanceof \Twig_SimpleFunction || $func instanceof \Twig_SimpleFilter) { $this->twig->{'add'.$classType}($func); } else { throw new \Exception("Incorrect options for \"$classType\" $name.");