Add support for NamedEvent in HookEventBridge

This commit is contained in:
Art4 2024-12-30 23:38:18 +00:00
parent ef0f2a1f3c
commit f86330b8b7
2 changed files with 15 additions and 13 deletions

View file

@ -10,8 +10,8 @@ declare(strict_types=1);
namespace Friendica\EventSubscriber;
use Friendica\Core\Hook;
use Friendica\Event\DataFilterEvent;
use Friendica\Event\HtmlFilterEvent;
use Friendica\Event\NamedEvent;
/**
* Bridge between the EventDispatcher and the Hook class.
@ -26,7 +26,7 @@ final class HookEventBridge implements StaticEventSubscriber
private static $mockedCallHook = null;
/**
* This maps the event names to the legacy Hook names.
* This maps the new event names to the legacy Hook names.
*/
private static array $eventMapper = [
HtmlFilterEvent::HEAD => 'head',
@ -41,7 +41,7 @@ final class HookEventBridge implements StaticEventSubscriber
public static function getStaticSubscribedEvents(): array
{
return [
DataFilterEvent::class => 'onDataFilterEvent',
NamedEvent::class => 'onNamedEvent',
HtmlFilterEvent::HEAD => 'onHtmlFilterEvent',
HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent',
HtmlFilterEvent::PAGE_CONTENT_TOP => 'onHtmlFilterEvent',
@ -49,11 +49,13 @@ final class HookEventBridge implements StaticEventSubscriber
];
}
public static function onDataFilterEvent(DataFilterEvent $event): void
public static function onNamedEvent(NamedEvent $event): void
{
$event->setData(
static::callHook($event->getName(), $event->getData())
);
$name = $event->getName();
$name = static::$eventMapper[$name] ?? $name;
static::callHook($name, '');
}
public static function onHtmlFilterEvent(HtmlFilterEvent $event): void

View file

@ -9,8 +9,8 @@ declare(strict_types=1);
namespace Friendica\Test\Unit\EventSubscriber;
use Friendica\Event\DataFilterEvent;
use Friendica\Event\HtmlFilterEvent;
use Friendica\Event\NamedEvent;
use Friendica\EventSubscriber\HookEventBridge;
use Friendica\EventSubscriber\StaticEventSubscriber;
use PHPUnit\Framework\TestCase;
@ -28,7 +28,7 @@ class HookEventBridgeTest extends TestCase
public function testGetStaticSubscribedEventsReturnsStaticMethods(): void
{
$expected = [
DataFilterEvent::class => 'onDataFilterEvent',
NamedEvent::class => 'onNamedEvent',
HtmlFilterEvent::HEAD => 'onHtmlFilterEvent',
HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent',
HtmlFilterEvent::PAGE_CONTENT_TOP => 'onHtmlFilterEvent',
@ -53,21 +53,21 @@ class HookEventBridgeTest extends TestCase
}
}
public function testOnDataFilterEventCallsHook(): void
public function testOnNamedEventCallsHook(): void
{
$event = new DataFilterEvent('test', ['original']);
$event = new NamedEvent('test');
$reflectionProperty = new \ReflectionProperty(HookEventBridge::class, 'mockedCallHook');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue(null, function (string $name, $data) {
$this->assertSame('test', $name);
$this->assertSame(['original'], $data);
$this->assertSame('', $data);
return $data;
});
HookEventBridge::onDataFilterEvent($event);
HookEventBridge::onNamedEvent($event);
}
public static function getHtmlFilterEventData(): array