2025-01-24 14:36:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// Copyright (C) 2010-2024, the Friendica project
|
|
|
|
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Friendica\EventSubscriber;
|
|
|
|
|
|
|
|
use Friendica\Core\Hook;
|
2025-01-27 16:02:58 +00:00
|
|
|
use Friendica\Event\ConfigLoadedEvent;
|
2025-01-24 14:36:05 +00:00
|
|
|
use Friendica\Event\Event;
|
|
|
|
use Friendica\Event\HtmlFilterEvent;
|
|
|
|
use Friendica\Event\NamedEvent;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bridge between the EventDispatcher and the Hook class.
|
2025-01-27 15:10:04 +00:00
|
|
|
*
|
|
|
|
* @internal Provides BC
|
2025-01-24 14:36:05 +00:00
|
|
|
*/
|
2025-01-27 15:11:01 +00:00
|
|
|
final class HookEventBridge
|
2025-01-24 14:36:05 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* This allows us to mock the Hook call in tests.
|
|
|
|
*
|
|
|
|
* @var \Closure|null
|
|
|
|
*/
|
|
|
|
private static $mockedCallHook = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This maps the new event names to the legacy Hook names.
|
|
|
|
*/
|
|
|
|
private static array $eventMapper = [
|
|
|
|
Event::INIT => 'init_1',
|
2025-01-27 16:02:58 +00:00
|
|
|
ConfigLoadedEvent::CONFIG_LOADED => 'load_config',
|
2025-01-24 14:36:05 +00:00
|
|
|
HtmlFilterEvent::HEAD => 'head',
|
|
|
|
HtmlFilterEvent::FOOTER => 'footer',
|
|
|
|
HtmlFilterEvent::PAGE_CONTENT_TOP => 'page_content_top',
|
|
|
|
HtmlFilterEvent::PAGE_END => 'page_end',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<string, string>
|
|
|
|
*/
|
|
|
|
public static function getStaticSubscribedEvents(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
Event::INIT => 'onNamedEvent',
|
2025-01-27 16:02:58 +00:00
|
|
|
ConfigLoadedEvent::CONFIG_LOADED => 'onConfigLoadedEvent',
|
2025-01-24 14:36:05 +00:00
|
|
|
HtmlFilterEvent::HEAD => 'onHtmlFilterEvent',
|
|
|
|
HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent',
|
|
|
|
HtmlFilterEvent::PAGE_CONTENT_TOP => 'onHtmlFilterEvent',
|
|
|
|
HtmlFilterEvent::PAGE_END => 'onHtmlFilterEvent',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function onNamedEvent(NamedEvent $event): void
|
|
|
|
{
|
2025-01-28 07:23:44 +00:00
|
|
|
static::callHook($event->getName(), '');
|
|
|
|
}
|
2025-01-24 14:36:05 +00:00
|
|
|
|
2025-01-28 07:23:44 +00:00
|
|
|
public static function onConfigLoadedEvent(ConfigLoadedEvent $event): void
|
|
|
|
{
|
|
|
|
static::callHook($event->getName(), $event->getConfig());
|
2025-01-24 14:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function onHtmlFilterEvent(HtmlFilterEvent $event): void
|
|
|
|
{
|
|
|
|
$event->setHtml(
|
2025-01-28 07:23:44 +00:00
|
|
|
static::callHook($event->getName(), $event->getHtml())
|
2025-01-24 14:36:05 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string|array $data
|
|
|
|
*
|
2025-01-27 16:02:58 +00:00
|
|
|
* @return string|array|object
|
2025-01-24 14:36:05 +00:00
|
|
|
*/
|
|
|
|
private static function callHook(string $name, $data)
|
|
|
|
{
|
2025-01-28 07:23:44 +00:00
|
|
|
// If possible, map the event name to the legacy Hook name
|
|
|
|
$name = static::$eventMapper[$name] ?? $name;
|
|
|
|
|
2025-01-24 14:36:05 +00:00
|
|
|
// Little hack to allow mocking the Hook call in tests.
|
|
|
|
if (static::$mockedCallHook instanceof \Closure) {
|
|
|
|
return (static::$mockedCallHook)->__invoke($name, $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
Hook::callAll($name, $data);
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
}
|