mirror of
https://git.friendi.ca/friendica/friendica.git
synced 2025-06-17 12:05:15 +02:00
Add support for NamedEvent in HookEventBridge
This commit is contained in:
parent
ef0f2a1f3c
commit
f86330b8b7
2 changed files with 15 additions and 13 deletions
|
@ -10,8 +10,8 @@ declare(strict_types=1);
|
||||||
namespace Friendica\EventSubscriber;
|
namespace Friendica\EventSubscriber;
|
||||||
|
|
||||||
use Friendica\Core\Hook;
|
use Friendica\Core\Hook;
|
||||||
use Friendica\Event\DataFilterEvent;
|
|
||||||
use Friendica\Event\HtmlFilterEvent;
|
use Friendica\Event\HtmlFilterEvent;
|
||||||
|
use Friendica\Event\NamedEvent;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bridge between the EventDispatcher and the Hook class.
|
* Bridge between the EventDispatcher and the Hook class.
|
||||||
|
@ -26,7 +26,7 @@ final class HookEventBridge implements StaticEventSubscriber
|
||||||
private static $mockedCallHook = null;
|
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 = [
|
private static array $eventMapper = [
|
||||||
HtmlFilterEvent::HEAD => 'head',
|
HtmlFilterEvent::HEAD => 'head',
|
||||||
|
@ -41,7 +41,7 @@ final class HookEventBridge implements StaticEventSubscriber
|
||||||
public static function getStaticSubscribedEvents(): array
|
public static function getStaticSubscribedEvents(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
DataFilterEvent::class => 'onDataFilterEvent',
|
NamedEvent::class => 'onNamedEvent',
|
||||||
HtmlFilterEvent::HEAD => 'onHtmlFilterEvent',
|
HtmlFilterEvent::HEAD => 'onHtmlFilterEvent',
|
||||||
HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent',
|
HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent',
|
||||||
HtmlFilterEvent::PAGE_CONTENT_TOP => '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(
|
$name = $event->getName();
|
||||||
static::callHook($event->getName(), $event->getData())
|
|
||||||
);
|
$name = static::$eventMapper[$name] ?? $name;
|
||||||
|
|
||||||
|
static::callHook($name, '');
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function onHtmlFilterEvent(HtmlFilterEvent $event): void
|
public static function onHtmlFilterEvent(HtmlFilterEvent $event): void
|
||||||
|
|
|
@ -9,8 +9,8 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Friendica\Test\Unit\EventSubscriber;
|
namespace Friendica\Test\Unit\EventSubscriber;
|
||||||
|
|
||||||
use Friendica\Event\DataFilterEvent;
|
|
||||||
use Friendica\Event\HtmlFilterEvent;
|
use Friendica\Event\HtmlFilterEvent;
|
||||||
|
use Friendica\Event\NamedEvent;
|
||||||
use Friendica\EventSubscriber\HookEventBridge;
|
use Friendica\EventSubscriber\HookEventBridge;
|
||||||
use Friendica\EventSubscriber\StaticEventSubscriber;
|
use Friendica\EventSubscriber\StaticEventSubscriber;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
@ -28,7 +28,7 @@ class HookEventBridgeTest extends TestCase
|
||||||
public function testGetStaticSubscribedEventsReturnsStaticMethods(): void
|
public function testGetStaticSubscribedEventsReturnsStaticMethods(): void
|
||||||
{
|
{
|
||||||
$expected = [
|
$expected = [
|
||||||
DataFilterEvent::class => 'onDataFilterEvent',
|
NamedEvent::class => 'onNamedEvent',
|
||||||
HtmlFilterEvent::HEAD => 'onHtmlFilterEvent',
|
HtmlFilterEvent::HEAD => 'onHtmlFilterEvent',
|
||||||
HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent',
|
HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent',
|
||||||
HtmlFilterEvent::PAGE_CONTENT_TOP => '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 = new \ReflectionProperty(HookEventBridge::class, 'mockedCallHook');
|
||||||
$reflectionProperty->setAccessible(true);
|
$reflectionProperty->setAccessible(true);
|
||||||
|
|
||||||
$reflectionProperty->setValue(null, function (string $name, $data) {
|
$reflectionProperty->setValue(null, function (string $name, $data) {
|
||||||
$this->assertSame('test', $name);
|
$this->assertSame('test', $name);
|
||||||
$this->assertSame(['original'], $data);
|
$this->assertSame('', $data);
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
});
|
});
|
||||||
|
|
||||||
HookEventBridge::onDataFilterEvent($event);
|
HookEventBridge::onNamedEvent($event);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getHtmlFilterEventData(): array
|
public static function getHtmlFilterEventData(): array
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue