diff options
Diffstat (limited to 'app/Helper/HookHelper.php')
-rw-r--r-- | app/Helper/HookHelper.php | 55 |
1 files changed, 46 insertions, 9 deletions
diff --git a/app/Helper/HookHelper.php b/app/Helper/HookHelper.php index 2d13ebcc..24b7d00a 100644 --- a/app/Helper/HookHelper.php +++ b/app/Helper/HookHelper.php @@ -2,6 +2,7 @@ namespace Kanboard\Helper; +use Closure; use Kanboard\Core\Base; /** @@ -24,8 +25,8 @@ class HookHelper extends Base { $buffer = ''; - foreach ($this->hook->getListeners($hook) as $file) { - $buffer .= $this->helper->asset->$type($file); + foreach ($this->hook->getListeners($hook) as $params) { + $buffer .= $this->helper->asset->$type($params['template']); } return $buffer; @@ -43,8 +44,18 @@ class HookHelper extends Base { $buffer = ''; - foreach ($this->hook->getListeners($hook) as $template) { - $buffer .= $this->template->render($template, $variables); + foreach ($this->hook->getListeners($hook) as $params) { + if (! empty($params['variables'])) { + $variables = array_merge($variables, $params['variables']); + } elseif (! empty($params['callable'])) { + $result = call_user_func_array($params['callable'], $variables); + + if (is_array($result)) { + $variables = array_merge($variables, $result); + } + } + + $buffer .= $this->template->render($params['template'], $variables); } return $buffer; @@ -54,13 +65,39 @@ class HookHelper extends Base * Attach a template to a hook * * @access public - * @param string $hook - * @param string $template - * @return \Kanboard\Helper\Hook + * @param string $hook + * @param string $template + * @param array $variables + * @return $this + */ + public function attach($hook, $template, array $variables = array()) + { + $this->hook->on($hook, array( + 'template' => $template, + 'variables' => $variables, + )); + + return $this; + } + + /** + * Attach a template to a hook with a callable + * + * Arguments passed to the callback are the one passed to the hook + * + * @access public + * @param string $hook + * @param string $template + * @param Closure $callable + * @return $this */ - public function attach($hook, $template) + public function attachCallable($hook, $template, Closure $callable) { - $this->hook->on($hook, $template); + $this->hook->on($hook, array( + 'template' => $template, + 'callable' => $callable, + )); + return $this; } } |