From 742cde89e628fff9fac4e6af2cd693c5f94088dd Mon Sep 17 00:00:00 2001 From: Art4 Date: Sat, 8 Feb 2025 16:33:07 +0000 Subject: [PATCH] Implement home_init hook as Event --- src/Core/Hooks/HookEventBridge.php | 2 ++ src/Event/Event.php | 2 ++ src/Module/Home.php | 9 +++++---- src/Module/User/Delegation.php | 12 ++++++++---- tests/Unit/Core/Hooks/HookEventBridgeTest.php | 2 ++ 5 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index 306434609b..9f35828ebf 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -35,6 +35,7 @@ final class HookEventBridge */ private static array $eventMapper = [ Event::INIT => 'init_1', + Event::HOME_INIT => 'home_init', ConfigLoadedEvent::CONFIG_LOADED => 'load_config', ArrayFilterEvent::APP_MENU => 'app_menu', ArrayFilterEvent::NAV_INFO => 'nav_info', @@ -57,6 +58,7 @@ final class HookEventBridge { return [ Event::INIT => 'onNamedEvent', + Event::HOME_INIT => 'onNamedEvent', ConfigLoadedEvent::CONFIG_LOADED => 'onConfigLoadedEvent', ArrayFilterEvent::APP_MENU => 'onArrayFilterEvent', ArrayFilterEvent::NAV_INFO => 'onArrayFilterEvent', diff --git a/src/Event/Event.php b/src/Event/Event.php index 90defc3204..7a6fca3840 100644 --- a/src/Event/Event.php +++ b/src/Event/Event.php @@ -21,6 +21,8 @@ class Event implements NamedEvent */ public const INIT = 'friendica.init'; + public const HOME_INIT = 'friendica.home_init'; + private string $name; public function __construct(string $name) diff --git a/src/Module/Home.php b/src/Module/Home.php index 9c72398f7a..692d10ba5f 100644 --- a/src/Module/Home.php +++ b/src/Module/Home.php @@ -11,6 +11,7 @@ use Friendica\BaseModule; use Friendica\Core\Hook; use Friendica\Core\Renderer; use Friendica\DI; +use Friendica\Event\Event; use Friendica\Model\User; use Friendica\Module\Security\Login; use Friendica\Protocol\ActivityPub; @@ -34,11 +35,11 @@ class Home extends BaseModule { $basePath = DI::appHelper()->getBasePath(); $config = DI::config(); + $eventDispatcher = DI::eventDispatcher(); - // currently no returned data is used - $ret = []; - - Hook::callAll('home_init', $ret); + $eventDispatcher->dispatch( + new Event(Event::HOME_INIT) + ); if (DI::userSession()->getLocalUserId() && (DI::userSession()->getLocalUserNickname())) { DI::baseUrl()->redirect('network'); diff --git a/src/Module/User/Delegation.php b/src/Module/User/Delegation.php index 2c13891348..1658e72c18 100644 --- a/src/Module/User/Delegation.php +++ b/src/Module/User/Delegation.php @@ -12,11 +12,11 @@ use Friendica\App\BaseURL; use Friendica\AppHelper; use Friendica\BaseModule; use Friendica\Contact\Introduction\Repository\Introduction; -use Friendica\Core\Hook; use Friendica\Core\L10n; use Friendica\Core\Renderer; use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Database\Database; +use Friendica\Event\Event; use Friendica\Model\Notification; use Friendica\Model\User; use Friendica\Module\Response; @@ -25,6 +25,7 @@ use Friendica\Navigation\SystemMessages; use Friendica\Network\HTTPException\ForbiddenException; use Friendica\Security\Authentication; use Friendica\Util; +use Psr\EventDispatcher\EventDispatcherInterface; use Psr\Log\LoggerInterface; /** @@ -46,8 +47,9 @@ class Delegation extends BaseModule private $intro; /** @var AppHelper */ private $appHelper; + private EventDispatcherInterface $eventDispatcher; - public function __construct(AppHelper $appHelper, Introduction $intro, Notify $notify, SystemMessages $systemMessages, Authentication $auth, Database $db, IHandleUserSessions $session, L10n $l10n, BaseURL $baseUrl, Arguments $args, LoggerInterface $logger, Util\Profiler $profiler, Response $response, array $server, array $parameters = []) + public function __construct(EventDispatcherInterface $eventDispatcher, AppHelper $appHelper, Introduction $intro, Notify $notify, SystemMessages $systemMessages, Authentication $auth, Database $db, IHandleUserSessions $session, L10n $l10n, BaseURL $baseUrl, Arguments $args, LoggerInterface $logger, Util\Profiler $profiler, Response $response, array $server, array $parameters = []) { parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); @@ -58,6 +60,7 @@ class Delegation extends BaseModule $this->notify = $notify; $this->intro = $intro; $this->appHelper = $appHelper; + $this->eventDispatcher = $eventDispatcher; } protected function post(array $request = []) @@ -128,8 +131,9 @@ class Delegation extends BaseModule $this->session->setSubManagedUserId($original_id); } - $ret = []; - Hook::callAll('home_init', $ret); + $this->eventDispatcher->dispatch( + new Event(Event::HOME_INIT) + ); $this->systemMessages->addNotice($this->t('You are now logged in as %s', $user['username'])); diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index 8a207684b1..6e82ab09f6 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -23,6 +23,7 @@ class HookEventBridgeTest extends TestCase { $expected = [ Event::INIT => 'onNamedEvent', + Event::HOME_INIT => 'onNamedEvent', ConfigLoadedEvent::CONFIG_LOADED => 'onConfigLoadedEvent', ArrayFilterEvent::APP_MENU => 'onArrayFilterEvent', ArrayFilterEvent::NAV_INFO => 'onArrayFilterEvent', @@ -61,6 +62,7 @@ class HookEventBridgeTest extends TestCase return [ ['test', 'test'], [Event::INIT, 'init_1'], + [Event::HOME_INIT, 'home_init'], ]; }