Skip to content

Commit 7921b19

Browse files
committed
Merge pull request slimphp#912 from wadewomersley/master
Allow multiple arguments for applyHook, pass to callable in order given (master)
2 parents 5ef5fab + 53738cb commit 7921b19

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

Slim/Slim.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,10 +1187,10 @@ public function hook($name, $callable, $priority = 10)
11871187

11881188
/**
11891189
* Invoke hook
1190-
* @param string $name The hook name
1191-
* @param mixed $hookArg (Optional) Argument for hooked functions
1190+
* @param string $name The hook name
1191+
* @param mixed ... (Optional) Argument(s) for hooked functions, can specify multiple arguments
11921192
*/
1193-
public function applyHook($name, $hookArg = null)
1193+
public function applyHook($name)
11941194
{
11951195
if (!isset($this->hooks[$name])) {
11961196
$this->hooks[$name] = array(array());
@@ -1200,10 +1200,14 @@ public function applyHook($name, $hookArg = null)
12001200
if (count($this->hooks[$name]) > 1) {
12011201
ksort($this->hooks[$name]);
12021202
}
1203+
1204+
$args = func_get_args();
1205+
array_shift($args);
1206+
12031207
foreach ($this->hooks[$name] as $priority) {
12041208
if (!empty($priority)) {
12051209
foreach ($priority as $callable) {
1206-
call_user_func($callable, $hookArg);
1210+
call_user_func_array($callable, $args);
12071211
}
12081212
}
12091213
}

tests/SlimTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,6 +1612,32 @@ public function testHookClear()
16121612
$this->assertEquals(array(array()), $app->getHooks('test.hook.one'));
16131613
}
16141614

1615+
/**
1616+
* Test hooks accept multiple arguments
1617+
*
1618+
* Pre-conditions:
1619+
* Slim app instantiated;
1620+
* Hook name does not exist;
1621+
* Listener is a callable object;
1622+
*
1623+
* Post-conditions:
1624+
* Callable invoked with 2 arguments
1625+
*/
1626+
public function testHooksMultipleArguments()
1627+
{
1628+
$testArgA = 'argumentA';
1629+
$testArgB = 'argumentB';
1630+
1631+
$this->expectOutputString($testArgA . $testArgB);
1632+
1633+
$app = new \Slim\Slim();
1634+
1635+
$app->hook('test.hook.one', function ($argA, $argB) {
1636+
echo $argA . $argB;
1637+
});
1638+
$app->applyHook('test.hook.one', $testArgA, $testArgB);
1639+
}
1640+
16151641
/**
16161642
* Test late static binding
16171643
*

0 commit comments

Comments
 (0)