Better callable support in Event default actions

Instead of parsing the passed callback ourselves, this patch relies on
call_user_func_array() instead to call an Event's default action. This
ensures all possible ways to define a callback (including static
methods) can be used.

This should fix a problem mentioned in #2943
This commit is contained in:
Andreas Gohr 2020-01-22 13:24:50 +01:00
parent d9051c44e9
commit 4352f9743c
2 changed files with 44 additions and 6 deletions

View File

@ -0,0 +1,43 @@
<?php
namespace tests\inc\Extension;
use dokuwiki\Extension\Event;
class EventTest extends \DokuWikiTest
{
static public function staticFunc(&$data)
{
$data['test'] = strtoupper($data['test']);
}
public function dynamicFunc(&$data)
{
$data['test'] = strtoupper($data['test']);
}
public function testGlobal()
{
$data = 'test';
$result = Event::createAndTrigger('TESTTRIGGER', $data, 'strtoupper');
$this->assertEquals('TEST', $result);
}
public function testDynamic()
{
$data = ['test' => 'test'];
Event::createAndTrigger('TESTTRIGGER', $data, [$this, 'dynamicFunc']);
$this->assertEquals(['test' => 'TEST'], $data);
}
public function testStatic()
{
$data = ['test' => 'test'];
Event::createAndTrigger('TESTTRIGGER', $data, 'tests\inc\Extension\EventTest::staticFunc');
$this->assertEquals(['test' => 'TEST'], $data);
$data = ['test' => 'test'];
Event::createAndTrigger('TESTTRIGGER', $data, ['tests\inc\Extension\EventTest', 'staticFunc']);
$this->assertEquals(['test' => 'TEST'], $data);
}
}

View File

@ -126,12 +126,7 @@ class Event
}
if ($this->advise_before($enablePrevent) && is_callable($action)) {
if (is_array($action)) {
list($obj, $method) = $action;
$this->result = $obj->$method($this->data);
} else {
$this->result = $action($this->data);
}
$this->result = call_user_func_array($action, [&$this->data]);
}
$this->advise_after();