From b6119e7dee84869a619dedccd9c80df4422a4f5b Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sat, 23 Jul 2016 14:05:15 -0400 Subject: Added internal task links to activity stream --- app/Helper/HookHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/Helper/HookHelper.php') diff --git a/app/Helper/HookHelper.php b/app/Helper/HookHelper.php index 2d13ebcc..cb4dc1ef 100644 --- a/app/Helper/HookHelper.php +++ b/app/Helper/HookHelper.php @@ -56,7 +56,7 @@ class HookHelper extends Base * @access public * @param string $hook * @param string $template - * @return \Kanboard\Helper\Hook + * @return $this */ public function attach($hook, $template) { -- cgit v1.2.3 From 2ebe8b32728c341ec16e1197fe2e12d32ddd5de5 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sat, 13 Aug 2016 18:08:46 -0400 Subject: Add the possibility to attach template hooks with local variables --- ChangeLog | 1 + app/Helper/HookHelper.php | 25 +++++++++++++++------- doc/plugin-hooks.markdown | 8 +++++++ tests/units/Helper/HookHelperTest.php | 40 +++++++++++++++++++++++++++-------- 4 files changed, 57 insertions(+), 17 deletions(-) (limited to 'app/Helper/HookHelper.php') diff --git a/ChangeLog b/ChangeLog index 25ce7eea..d013ad9a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,7 @@ Version 1.0.33 (unreleased) Improvements: +* Add the possibility to attach template hooks with local variables * Add "reference" hooks * Show project name in task forms * Convert vanilla CSS to SASS diff --git a/app/Helper/HookHelper.php b/app/Helper/HookHelper.php index cb4dc1ef..418c55a0 100644 --- a/app/Helper/HookHelper.php +++ b/app/Helper/HookHelper.php @@ -24,8 +24,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 +43,12 @@ 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']); + } + + $buffer .= $this->template->render($params['template'], $variables); } return $buffer; @@ -54,13 +58,18 @@ class HookHelper extends Base * Attach a template to a hook * * @access public - * @param string $hook - * @param string $template + * @param string $hook + * @param string $template + * @param array $variables * @return $this */ - public function attach($hook, $template) + public function attach($hook, $template, array $variables = array()) { - $this->hook->on($hook, $template); + $this->hook->on($hook, array( + 'template' => $template, + 'variables' => $variables, + )); + return $this; } } diff --git a/doc/plugin-hooks.markdown b/doc/plugin-hooks.markdown index 9a4bdab2..a700e34b 100644 --- a/doc/plugin-hooks.markdown +++ b/doc/plugin-hooks.markdown @@ -153,6 +153,14 @@ Example to add new content in the dashboard sidebar: $this->template->hook->attach('template:dashboard:sidebar', 'myplugin:dashboard/sidebar'); ``` +Example to attach a template with local variables: + +```php +$this->template->hook->attach('template:dashboard:sidebar', 'myplugin:dashboard/sidebar', array( + 'variable' => 'foobar', +)); +``` + This call is usually defined in the `initialize()` method. The first argument is name of the hook and the second argument is the template name. diff --git a/tests/units/Helper/HookHelperTest.php b/tests/units/Helper/HookHelperTest.php index 6e03acd1..66d13381 100644 --- a/tests/units/Helper/HookHelperTest.php +++ b/tests/units/Helper/HookHelperTest.php @@ -6,6 +6,28 @@ use Kanboard\Helper\HookHelper; class HookHelperTest extends Base { + public function testAttachLocalVariables() + { + $this->container['template'] = $this + ->getMockBuilder('\Kanboard\Core\Template') + ->setConstructorArgs(array($this->container['helper'])) + ->setMethods(array('render')) + ->getMock(); + + $this->container['template'] + ->expects($this->once()) + ->method('render') + ->with( + $this->equalTo('tpl1'), + $this->equalTo(array('k0' => 'v0', 'k1' => 'v1')) + ) + ->will($this->returnValue('tpl1_content')); + + $hookHelper = new HookHelper($this->container); + $hookHelper->attach('test', 'tpl1', array('k1' => 'v1')); + $this->assertEquals('tpl1_content', $hookHelper->render('test', array('k0' => 'v0'))); + } + public function testMultipleHooks() { $this->container['template'] = $this @@ -32,10 +54,10 @@ class HookHelperTest extends Base ) ->will($this->returnValue('tpl2_content')); - $h = new HookHelper($this->container); - $h->attach('test', 'tpl1'); - $h->attach('test', 'tpl2'); - $this->assertEquals('tpl1_contenttpl2_content', $h->render('test')); + $hookHelper = new HookHelper($this->container); + $hookHelper->attach('test', 'tpl1'); + $hookHelper->attach('test', 'tpl2'); + $this->assertEquals('tpl1_contenttpl2_content', $hookHelper->render('test')); } public function testAssetHooks() @@ -64,11 +86,11 @@ class HookHelperTest extends Base ) ->will($this->returnValue('')); - $h = new HookHelper($this->container); - $h->attach('test1', 'skin.css'); - $h->attach('test2', 'skin.js'); + $hookHelper = new HookHelper($this->container); + $hookHelper->attach('test1', 'skin.css'); + $hookHelper->attach('test2', 'skin.js'); - $this->assertContains('', $h->asset('css', 'test1')); - $this->assertContains('', $h->asset('js', 'test2')); + $this->assertContains('', $hookHelper->asset('css', 'test1')); + $this->assertContains('', $hookHelper->asset('js', 'test2')); } } -- cgit v1.2.3 From 010199e8f846f6c0b4f23336338bfda17ec04901 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sat, 13 Aug 2016 18:41:01 -0400 Subject: Add the possibility to attach template hooks with a callback --- ChangeLog | 2 +- app/Helper/HookHelper.php | 27 +++++++++++++++++++ doc/plugin-hooks.markdown | 8 ++++++ tests/units/Helper/HookHelperTest.php | 51 +++++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 1 deletion(-) (limited to 'app/Helper/HookHelper.php') diff --git a/ChangeLog b/ChangeLog index d013ad9a..25a92168 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,7 +3,7 @@ Version 1.0.33 (unreleased) Improvements: -* Add the possibility to attach template hooks with local variables +* Add the possibility to attach template hooks with local variables and callback * Add "reference" hooks * Show project name in task forms * Convert vanilla CSS to SASS diff --git a/app/Helper/HookHelper.php b/app/Helper/HookHelper.php index 418c55a0..e43cfdfd 100644 --- a/app/Helper/HookHelper.php +++ b/app/Helper/HookHelper.php @@ -46,6 +46,12 @@ class HookHelper extends Base 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); @@ -72,4 +78,25 @@ class HookHelper extends Base 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 callable $callable + * @return $this + */ + public function attachCallable($hook, $template, callable $callable) + { + $this->hook->on($hook, array( + 'template' => $template, + 'callable' => $callable, + )); + + return $this; + } } diff --git a/doc/plugin-hooks.markdown b/doc/plugin-hooks.markdown index a700e34b..97816d5f 100644 --- a/doc/plugin-hooks.markdown +++ b/doc/plugin-hooks.markdown @@ -161,6 +161,14 @@ $this->template->hook->attach('template:dashboard:sidebar', 'myplugin:dashboard/ )); ``` +Example to attach a template with a callable: + +```php +$this->template->hook->attach('template:dashboard:sidebar', 'myplugin:dashboard/sidebar', function($hook_param1, $hook_param2) { + return array('new_template_variable' => 'foobar'); // Inject a new variable into the plugin template +}); +``` + This call is usually defined in the `initialize()` method. The first argument is name of the hook and the second argument is the template name. diff --git a/tests/units/Helper/HookHelperTest.php b/tests/units/Helper/HookHelperTest.php index 66d13381..a67eaed9 100644 --- a/tests/units/Helper/HookHelperTest.php +++ b/tests/units/Helper/HookHelperTest.php @@ -6,6 +6,57 @@ use Kanboard\Helper\HookHelper; class HookHelperTest extends Base { + public function testAttachCallable() + { + $this->container['template'] = $this + ->getMockBuilder('\Kanboard\Core\Template') + ->setConstructorArgs(array($this->container['helper'])) + ->setMethods(array('render')) + ->getMock(); + + $this->container['template'] + ->expects($this->once()) + ->method('render') + ->with( + $this->equalTo('tpl1'), + $this->equalTo(array('k0' => 'v0', 'k1' => 'v1')) + ) + ->will($this->returnValue('tpl1_content')); + + $hookHelper = new HookHelper($this->container); + $hookHelper->attachCallable('test', 'tpl1', function() { + return array( + 'k1' => 'v1', + ); + }); + + $this->assertEquals('tpl1_content', $hookHelper->render('test', array('k0' => 'v0'))); + } + + public function testAttachCallableWithNoResult() + { + $this->container['template'] = $this + ->getMockBuilder('\Kanboard\Core\Template') + ->setConstructorArgs(array($this->container['helper'])) + ->setMethods(array('render')) + ->getMock(); + + $this->container['template'] + ->expects($this->once()) + ->method('render') + ->with( + $this->equalTo('tpl1'), + $this->equalTo(array('k0' => 'v0')) + ) + ->will($this->returnValue('tpl1_content')); + + $hookHelper = new HookHelper($this->container); + $hookHelper->attachCallable('test', 'tpl1', function() { + }); + + $this->assertEquals('tpl1_content', $hookHelper->render('test', array('k0' => 'v0'))); + } + public function testAttachLocalVariables() { $this->container['template'] = $this -- cgit v1.2.3 From 2ca3cc7f722893e18d3bf62bd71a0f5629eba096 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sat, 13 Aug 2016 23:47:58 -0400 Subject: Fix PHP 5.3 compatibility issue --- app/Helper/HookHelper.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'app/Helper/HookHelper.php') diff --git a/app/Helper/HookHelper.php b/app/Helper/HookHelper.php index e43cfdfd..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; /** @@ -87,10 +88,10 @@ class HookHelper extends Base * @access public * @param string $hook * @param string $template - * @param callable $callable + * @param Closure $callable * @return $this */ - public function attachCallable($hook, $template, callable $callable) + public function attachCallable($hook, $template, Closure $callable) { $this->hook->on($hook, array( 'template' => $template, -- cgit v1.2.3