From 649ddf7ab9145fdae497850ecedbe374afeb4008 Mon Sep 17 00:00:00 2001 From: Art4 Date: Sat, 8 Feb 2025 07:58:33 +0000 Subject: [PATCH 01/35] Add events for post_local, post_local_start and post_local_end --- src/Core/Hooks/HookEventBridge.php | 50 +++++++++++-------- src/Event/ArrayFilterEvent.php | 6 +++ tests/Unit/Core/Hooks/HookEventBridgeTest.php | 28 +++++++---- 3 files changed, 51 insertions(+), 33 deletions(-) diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index 2dad2d2b23..306434609b 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -34,17 +34,20 @@ final class HookEventBridge * This maps the new event names to the legacy Hook names. */ private static array $eventMapper = [ - Event::INIT => 'init_1', - ConfigLoadedEvent::CONFIG_LOADED => 'load_config', - ArrayFilterEvent::APP_MENU => 'app_menu', - ArrayFilterEvent::NAV_INFO => 'nav_info', - ArrayFilterEvent::FEATURE_ENABLED => 'isEnabled', - ArrayFilterEvent::FEATURE_GET => 'get', - HtmlFilterEvent::HEAD => 'head', - HtmlFilterEvent::FOOTER => 'footer', - HtmlFilterEvent::PAGE_HEADER => 'page_header', - HtmlFilterEvent::PAGE_CONTENT_TOP => 'page_content_top', - HtmlFilterEvent::PAGE_END => 'page_end', + Event::INIT => 'init_1', + ConfigLoadedEvent::CONFIG_LOADED => 'load_config', + ArrayFilterEvent::APP_MENU => 'app_menu', + ArrayFilterEvent::NAV_INFO => 'nav_info', + ArrayFilterEvent::FEATURE_ENABLED => 'isEnabled', + ArrayFilterEvent::FEATURE_GET => 'get', + ArrayFilterEvent::POST_LOCAL_START => 'post_local_start', + ArrayFilterEvent::POST_LOCAL => 'post_local', + ArrayFilterEvent::POST_LOCAL_END => 'post_local_end', + HtmlFilterEvent::HEAD => 'head', + HtmlFilterEvent::FOOTER => 'footer', + HtmlFilterEvent::PAGE_HEADER => 'page_header', + HtmlFilterEvent::PAGE_CONTENT_TOP => 'page_content_top', + HtmlFilterEvent::PAGE_END => 'page_end', ]; /** @@ -53,17 +56,20 @@ final class HookEventBridge public static function getStaticSubscribedEvents(): array { return [ - Event::INIT => 'onNamedEvent', - ConfigLoadedEvent::CONFIG_LOADED => 'onConfigLoadedEvent', - ArrayFilterEvent::APP_MENU => 'onArrayFilterEvent', - ArrayFilterEvent::NAV_INFO => 'onArrayFilterEvent', - ArrayFilterEvent::FEATURE_ENABLED => 'onArrayFilterEvent', - ArrayFilterEvent::FEATURE_GET => 'onArrayFilterEvent', - HtmlFilterEvent::HEAD => 'onHtmlFilterEvent', - HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent', - HtmlFilterEvent::PAGE_HEADER => 'onHtmlFilterEvent', - HtmlFilterEvent::PAGE_CONTENT_TOP => 'onHtmlFilterEvent', - HtmlFilterEvent::PAGE_END => 'onHtmlFilterEvent', + Event::INIT => 'onNamedEvent', + ConfigLoadedEvent::CONFIG_LOADED => 'onConfigLoadedEvent', + ArrayFilterEvent::APP_MENU => 'onArrayFilterEvent', + ArrayFilterEvent::NAV_INFO => 'onArrayFilterEvent', + ArrayFilterEvent::FEATURE_ENABLED => 'onArrayFilterEvent', + ArrayFilterEvent::FEATURE_GET => 'onArrayFilterEvent', + ArrayFilterEvent::POST_LOCAL_START => 'onArrayFilterEvent', + ArrayFilterEvent::POST_LOCAL => 'onArrayFilterEvent', + ArrayFilterEvent::POST_LOCAL_END => 'onArrayFilterEvent', + HtmlFilterEvent::HEAD => 'onHtmlFilterEvent', + HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent', + HtmlFilterEvent::PAGE_HEADER => 'onHtmlFilterEvent', + HtmlFilterEvent::PAGE_CONTENT_TOP => 'onHtmlFilterEvent', + HtmlFilterEvent::PAGE_END => 'onHtmlFilterEvent', ]; } diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index 839aa40b75..8d552755ee 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -24,6 +24,12 @@ final class ArrayFilterEvent extends Event public const FEATURE_GET = 'friendica.data.feature_get'; + public const POST_LOCAL_START = 'friendica.data.post_local_start'; + + public const POST_LOCAL = 'friendica.data.post_local'; + + public const POST_LOCAL_END = 'friendica.data.post_local_end'; + private array $array; public function __construct(string $name, array $array) diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index 671eb23e94..8a207684b1 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -22,17 +22,20 @@ class HookEventBridgeTest extends TestCase public function testGetStaticSubscribedEventsReturnsStaticMethods(): void { $expected = [ - Event::INIT => 'onNamedEvent', - ConfigLoadedEvent::CONFIG_LOADED => 'onConfigLoadedEvent', - ArrayFilterEvent::APP_MENU => 'onArrayFilterEvent', - ArrayFilterEvent::NAV_INFO => 'onArrayFilterEvent', - ArrayFilterEvent::FEATURE_ENABLED => 'onArrayFilterEvent', - ArrayFilterEvent::FEATURE_GET => 'onArrayFilterEvent', - HtmlFilterEvent::HEAD => 'onHtmlFilterEvent', - HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent', - HtmlFilterEvent::PAGE_HEADER => 'onHtmlFilterEvent', - HtmlFilterEvent::PAGE_CONTENT_TOP => 'onHtmlFilterEvent', - HtmlFilterEvent::PAGE_END => 'onHtmlFilterEvent', + Event::INIT => 'onNamedEvent', + ConfigLoadedEvent::CONFIG_LOADED => 'onConfigLoadedEvent', + ArrayFilterEvent::APP_MENU => 'onArrayFilterEvent', + ArrayFilterEvent::NAV_INFO => 'onArrayFilterEvent', + ArrayFilterEvent::FEATURE_ENABLED => 'onArrayFilterEvent', + ArrayFilterEvent::FEATURE_GET => 'onArrayFilterEvent', + ArrayFilterEvent::POST_LOCAL_START => 'onArrayFilterEvent', + ArrayFilterEvent::POST_LOCAL => 'onArrayFilterEvent', + ArrayFilterEvent::POST_LOCAL_END => 'onArrayFilterEvent', + HtmlFilterEvent::HEAD => 'onHtmlFilterEvent', + HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent', + HtmlFilterEvent::PAGE_HEADER => 'onHtmlFilterEvent', + HtmlFilterEvent::PAGE_CONTENT_TOP => 'onHtmlFilterEvent', + HtmlFilterEvent::PAGE_END => 'onHtmlFilterEvent', ]; $this->assertSame( @@ -119,6 +122,9 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::NAV_INFO, 'nav_info'], [ArrayFilterEvent::FEATURE_ENABLED, 'isEnabled'], [ArrayFilterEvent::FEATURE_GET, 'get'], + [ArrayFilterEvent::POST_LOCAL_START, 'post_local_start'], + [ArrayFilterEvent::POST_LOCAL, 'post_local'], + [ArrayFilterEvent::POST_LOCAL_END, 'post_local_end'], ]; } From aabf9a8b01f7de8a38047e0e1e67fa262512b88f Mon Sep 17 00:00:00 2001 From: Art4 Date: Sat, 8 Feb 2025 08:17:30 +0000 Subject: [PATCH 02/35] Replace hooks post_local_start, post_local and post_local_end with EventDispatcher --- mod/item.php | 13 +++++++++++-- src/Content/Item.php | 30 ++++++++++++++++++------------ src/Model/Item.php | 14 +++++++++----- src/Module/Post/Tag/Add.php | 13 ++++++++++--- 4 files changed, 48 insertions(+), 22 deletions(-) diff --git a/mod/item.php b/mod/item.php index de9a51915a..98528b59b1 100644 --- a/mod/item.php +++ b/mod/item.php @@ -24,6 +24,7 @@ use Friendica\Core\System; use Friendica\Core\Worker; use Friendica\Database\DBA; use Friendica\DI; +use Friendica\Event\ArrayFilterEvent; use Friendica\Model\Contact; use Friendica\Model\Item; use Friendica\Model\ItemURI; @@ -43,7 +44,11 @@ function item_post() item_drop($uid, $_REQUEST['dropitems']); } - Hook::callAll('post_local_start', $_REQUEST); + $eventDispatcher = DI::eventDispatcher(); + + $_REQUEST = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::POST_LOCAL_START, $_REQUEST) + )->getArray(); $return_path = $_REQUEST['return'] ?? ''; $preview = intval($_REQUEST['preview'] ?? 0); @@ -275,7 +280,11 @@ function item_process(array $post, array $request, bool $preview, string $return System::jsonExit(['preview' => $o]); } - Hook::callAll('post_local', $post); + $eventDispatcher = DI::eventDispatcher(); + + $post = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::POST_LOCAL, $post) + )->getArray(); unset($post['edit']); unset($post['self']); diff --git a/src/Content/Item.php b/src/Content/Item.php index aec01ae292..ee20e3d168 100644 --- a/src/Content/Item.php +++ b/src/Content/Item.php @@ -20,6 +20,7 @@ use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Core\System; use Friendica\Database\DBA; use Friendica\DI; +use Friendica\Event\ArrayFilterEvent; use Friendica\Model\Attach; use Friendica\Model\Circle; use Friendica\Model\Contact; @@ -43,6 +44,7 @@ use Friendica\Util\Proxy; use Friendica\Util\XML; use GuzzleHttp\Psr7\Uri; use ImagickException; +use Psr\EventDispatcher\EventDispatcherInterface; /** * A content helper class for displaying items @@ -69,19 +71,21 @@ class Item private $emailer; /** @var AppHelper */ private $appHelper; + private EventDispatcherInterface $eventDispatcher; - public function __construct(Profiler $profiler, Activity $activity, L10n $l10n, IHandleUserSessions $userSession, Video $bbCodeVideo, ACLFormatter $aclFormatter, IManagePersonalConfigValues $pConfig, BaseURL $baseURL, Emailer $emailer, AppHelper $appHelper) + public function __construct(Profiler $profiler, Activity $activity, L10n $l10n, IHandleUserSessions $userSession, Video $bbCodeVideo, ACLFormatter $aclFormatter, IManagePersonalConfigValues $pConfig, BaseURL $baseURL, Emailer $emailer, AppHelper $appHelper, EventDispatcherInterface $eventDispatcher) { - $this->profiler = $profiler; - $this->activity = $activity; - $this->l10n = $l10n; - $this->userSession = $userSession; - $this->bbCodeVideo = $bbCodeVideo; - $this->aclFormatter = $aclFormatter; - $this->baseURL = $baseURL; - $this->pConfig = $pConfig; - $this->emailer = $emailer; - $this->appHelper = $appHelper; + $this->profiler = $profiler; + $this->activity = $activity; + $this->l10n = $l10n; + $this->userSession = $userSession; + $this->bbCodeVideo = $bbCodeVideo; + $this->aclFormatter = $aclFormatter; + $this->baseURL = $baseURL; + $this->pConfig = $pConfig; + $this->emailer = $emailer; + $this->appHelper = $appHelper; + $this->eventDispatcher = $eventDispatcher; } /** @@ -1006,7 +1010,9 @@ class Item Tag::createImplicitMentions($post['uri-id'], $post['thr-parent-id']); } - Hook::callAll('post_local_end', $post); + $post = $this->eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::POST_LOCAL_END, $post) + )->getArray(); $author = DBA::selectFirst('contact', ['thumb'], ['uid' => $post['uid'], 'self' => true]); diff --git a/src/Model/Item.php b/src/Model/Item.php index 00435e2d4d..c796a1e5bf 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -22,6 +22,7 @@ use Friendica\Core\System; use Friendica\Core\Worker; use Friendica\Database\DBA; use Friendica\DI; +use Friendica\Event\ArrayFilterEvent; use Friendica\Model\Post\Category; use Friendica\Network\HTTPClient\Client\HttpClientAccept; use Friendica\Network\HTTPClient\Client\HttpClientOptions; @@ -1119,6 +1120,8 @@ class Item $item['private'] = self::PRIVATE; } + $eventDispatcher = DI::eventDispatcher(); + if ($notify && $post_local) { $item['edit'] = false; $item['parent'] = $parent_id; @@ -1137,7 +1140,9 @@ class Item $dummy_session = false; } - Hook::callAll('post_local', $item); + $item = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::POST_LOCAL, $item) + )->getArray(); if ($dummy_session) { unset($_SESSION['authenticated']); @@ -1237,7 +1242,9 @@ class Item } if (empty($item['event-id'])) { - unset($item['event-id']); + if (array_key_exists('event-id', $item)) { + unset($item['event-id']); + } $ev = Event::fromBBCode($item['body']); if ((!empty($ev['desc']) || !empty($ev['summary'])) && !empty($ev['start'])) { @@ -3010,9 +3017,6 @@ class Item * @return bool * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException - * @hook 'post_local_end' - * array $arr - * 'post_id' => ID of posted item */ public static function performActivity(int $item_id, string $verb, int $uid, string $allow_cid = null, string $allow_gid = null, string $deny_cid = null, string $deny_gid = null): bool { diff --git a/src/Module/Post/Tag/Add.php b/src/Module/Post/Tag/Add.php index b536ef848d..f1fa23a082 100644 --- a/src/Module/Post/Tag/Add.php +++ b/src/Module/Post/Tag/Add.php @@ -13,6 +13,7 @@ use Friendica\Core\L10n; use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Core\System; use Friendica\Core\Worker; +use Friendica\Event\ArrayFilterEvent; use Friendica\Model\Contact; use Friendica\Model\Item; use Friendica\Model\Post; @@ -22,6 +23,7 @@ use Friendica\Protocol\Activity; use Friendica\Protocol\Delivery; use Friendica\Util\Profiler; use Friendica\Util\XML; +use Psr\EventDispatcher\EventDispatcherInterface; use Psr\Log\LoggerInterface; /** @@ -31,12 +33,14 @@ class Add extends \Friendica\BaseModule { /** @var IHandleUserSessions */ private $session; + private EventDispatcherInterface $eventDispatcher; - public function __construct(IHandleUserSessions $session, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = []) + public function __construct(IHandleUserSessions $session, EventDispatcherInterface $eventDispatcher, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = []) { parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); - $this->session = $session; + $this->session = $session; + $this->eventDispatcher = $eventDispatcher; } protected function post(array $request = []) @@ -149,7 +153,10 @@ EOT; Tag::store($item['uri-id'], Tag::HASHTAG, $term); $post['id'] = $post_id; - Hook::callAll('post_local_end', $post); + + $post = $this->eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::POST_LOCAL_END, $post) + )->getArray(); $post = Post::selectFirst(['uri-id', 'uid'], ['id' => $post_id]); From 742cde89e628fff9fac4e6af2cd693c5f94088dd Mon Sep 17 00:00:00 2001 From: Art4 Date: Sat, 8 Feb 2025 16:33:07 +0000 Subject: [PATCH 03/35] 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'], ]; } From f4624b05912e2cea5559ad7b7b1d924c4d0063ee Mon Sep 17 00:00:00 2001 From: Art4 Date: Sat, 8 Feb 2025 16:38:01 +0000 Subject: [PATCH 04/35] Fix cs --- mod/item.php | 1 - src/Module/Home.php | 8 ++++---- src/Module/Post/Tag/Add.php | 1 - src/Module/User/Delegation.php | 14 +++++++------- 4 files changed, 11 insertions(+), 13 deletions(-) diff --git a/mod/item.php b/mod/item.php index 98528b59b1..46f2a219e3 100644 --- a/mod/item.php +++ b/mod/item.php @@ -18,7 +18,6 @@ use Friendica\Content\Conversation; use Friendica\Content\Text\BBCode; -use Friendica\Core\Hook; use Friendica\Core\Protocol; use Friendica\Core\System; use Friendica\Core\Worker; diff --git a/src/Module/Home.php b/src/Module/Home.php index 692d10ba5f..7e7d59d760 100644 --- a/src/Module/Home.php +++ b/src/Module/Home.php @@ -33,8 +33,8 @@ class Home extends BaseModule protected function content(array $request = []): string { - $basePath = DI::appHelper()->getBasePath(); - $config = DI::config(); + $basePath = DI::appHelper()->getBasePath(); + $config = DI::config(); $eventDispatcher = DI::eventDispatcher(); $eventDispatcher->dispatch( @@ -49,11 +49,11 @@ class Home extends BaseModule DI::baseUrl()->redirect('/profile/' . $config->get('system', 'singleuser')); } - $customHome = ''; + $customHome = ''; $defaultHeader = ($config->get('config', 'sitename') ? DI::l10n()->t('Welcome to %s', $config->get('config', 'sitename')) : ''); $homeFilePath = $basePath . '/home.html'; - $cssFilePath = $basePath . '/home.css'; + $cssFilePath = $basePath . '/home.css'; if (file_exists($homeFilePath)) { $customHome = $homeFilePath; diff --git a/src/Module/Post/Tag/Add.php b/src/Module/Post/Tag/Add.php index f1fa23a082..bf704a50c0 100644 --- a/src/Module/Post/Tag/Add.php +++ b/src/Module/Post/Tag/Add.php @@ -8,7 +8,6 @@ namespace Friendica\Module\Post\Tag; use Friendica\App; -use Friendica\Core\Hook; use Friendica\Core\L10n; use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Core\System; diff --git a/src/Module/User/Delegation.php b/src/Module/User/Delegation.php index 1658e72c18..935ac83a6b 100644 --- a/src/Module/User/Delegation.php +++ b/src/Module/User/Delegation.php @@ -53,13 +53,13 @@ class Delegation extends BaseModule { parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); - $this->session = $session; - $this->db = $db; - $this->auth = $auth; - $this->systemMessages = $systemMessages; - $this->notify = $notify; - $this->intro = $intro; - $this->appHelper = $appHelper; + $this->session = $session; + $this->db = $db; + $this->auth = $auth; + $this->systemMessages = $systemMessages; + $this->notify = $notify; + $this->intro = $intro; + $this->appHelper = $appHelper; $this->eventDispatcher = $eventDispatcher; } From 7298ad29c18a2f102bda7873d5f5a2171d519d67 Mon Sep 17 00:00:00 2001 From: Art4 Date: Sat, 8 Feb 2025 21:04:35 +0000 Subject: [PATCH 05/35] Implement photo_upload_form hook as Event --- mod/photos.php | 8 ++- src/Core/Hooks/HookEventBridge.php | 62 ++++++++++--------- src/Event/ArrayFilterEvent.php | 2 + tests/Unit/Core/Hooks/HookEventBridgeTest.php | 32 +++++----- 4 files changed, 57 insertions(+), 47 deletions(-) diff --git a/mod/photos.php b/mod/photos.php index c2831270ab..25eac8c533 100644 --- a/mod/photos.php +++ b/mod/photos.php @@ -12,12 +12,12 @@ use Friendica\Content\Pager; use Friendica\Content\Text\BBCode; use Friendica\Core\ACL; use Friendica\Core\Addon; -use Friendica\Core\Hook; use Friendica\Core\Renderer; use Friendica\Core\System; use Friendica\Database\DBA; use Friendica\Database\DBStructure; use Friendica\DI; +use Friendica\Event\ArrayFilterEvent; use Friendica\Model\Contact; use Friendica\Model\Item; use Friendica\Model\Photo; @@ -648,7 +648,11 @@ function photos_content() 'default_upload' => true ]; - Hook::callAll('photo_upload_form', $ret); + $eventDispatcher = DI::eventDispatcher(); + + $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::PHOTO_UPLOAD_FORM, $ret) + ); $default_upload_box = Renderer::replaceMacros(Renderer::getMarkupTemplate('photos_default_uploader_box.tpl'), []); $default_upload_submit = Renderer::replaceMacros(Renderer::getMarkupTemplate('photos_default_uploader_submit.tpl'), [ diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index 9f35828ebf..23cd209e11 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -34,21 +34,22 @@ final class HookEventBridge * This maps the new event names to the legacy Hook names. */ 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', - ArrayFilterEvent::FEATURE_ENABLED => 'isEnabled', - ArrayFilterEvent::FEATURE_GET => 'get', - ArrayFilterEvent::POST_LOCAL_START => 'post_local_start', - ArrayFilterEvent::POST_LOCAL => 'post_local', - ArrayFilterEvent::POST_LOCAL_END => 'post_local_end', - HtmlFilterEvent::HEAD => 'head', - HtmlFilterEvent::FOOTER => 'footer', - HtmlFilterEvent::PAGE_HEADER => 'page_header', - HtmlFilterEvent::PAGE_CONTENT_TOP => 'page_content_top', - HtmlFilterEvent::PAGE_END => 'page_end', + Event::INIT => 'init_1', + Event::HOME_INIT => 'home_init', + ConfigLoadedEvent::CONFIG_LOADED => 'load_config', + ArrayFilterEvent::APP_MENU => 'app_menu', + ArrayFilterEvent::NAV_INFO => 'nav_info', + ArrayFilterEvent::FEATURE_ENABLED => 'isEnabled', + ArrayFilterEvent::FEATURE_GET => 'get', + ArrayFilterEvent::POST_LOCAL_START => 'post_local_start', + ArrayFilterEvent::POST_LOCAL => 'post_local', + ArrayFilterEvent::POST_LOCAL_END => 'post_local_end', + ArrayFilterEvent::PHOTO_UPLOAD_FORM => 'photo_upload_form', + HtmlFilterEvent::HEAD => 'head', + HtmlFilterEvent::FOOTER => 'footer', + HtmlFilterEvent::PAGE_HEADER => 'page_header', + HtmlFilterEvent::PAGE_CONTENT_TOP => 'page_content_top', + HtmlFilterEvent::PAGE_END => 'page_end', ]; /** @@ -57,21 +58,22 @@ final class HookEventBridge public static function getStaticSubscribedEvents(): array { return [ - Event::INIT => 'onNamedEvent', - Event::HOME_INIT => 'onNamedEvent', - ConfigLoadedEvent::CONFIG_LOADED => 'onConfigLoadedEvent', - ArrayFilterEvent::APP_MENU => 'onArrayFilterEvent', - ArrayFilterEvent::NAV_INFO => 'onArrayFilterEvent', - ArrayFilterEvent::FEATURE_ENABLED => 'onArrayFilterEvent', - ArrayFilterEvent::FEATURE_GET => 'onArrayFilterEvent', - ArrayFilterEvent::POST_LOCAL_START => 'onArrayFilterEvent', - ArrayFilterEvent::POST_LOCAL => 'onArrayFilterEvent', - ArrayFilterEvent::POST_LOCAL_END => 'onArrayFilterEvent', - HtmlFilterEvent::HEAD => 'onHtmlFilterEvent', - HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent', - HtmlFilterEvent::PAGE_HEADER => 'onHtmlFilterEvent', - HtmlFilterEvent::PAGE_CONTENT_TOP => 'onHtmlFilterEvent', - HtmlFilterEvent::PAGE_END => 'onHtmlFilterEvent', + Event::INIT => 'onNamedEvent', + Event::HOME_INIT => 'onNamedEvent', + ConfigLoadedEvent::CONFIG_LOADED => 'onConfigLoadedEvent', + ArrayFilterEvent::APP_MENU => 'onArrayFilterEvent', + ArrayFilterEvent::NAV_INFO => 'onArrayFilterEvent', + ArrayFilterEvent::FEATURE_ENABLED => 'onArrayFilterEvent', + ArrayFilterEvent::FEATURE_GET => 'onArrayFilterEvent', + ArrayFilterEvent::POST_LOCAL_START => 'onArrayFilterEvent', + ArrayFilterEvent::POST_LOCAL => 'onArrayFilterEvent', + ArrayFilterEvent::POST_LOCAL_END => 'onArrayFilterEvent', + ArrayFilterEvent::PHOTO_UPLOAD_FORM => 'onArrayFilterEvent', + HtmlFilterEvent::HEAD => 'onHtmlFilterEvent', + HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent', + HtmlFilterEvent::PAGE_HEADER => 'onHtmlFilterEvent', + HtmlFilterEvent::PAGE_CONTENT_TOP => 'onHtmlFilterEvent', + HtmlFilterEvent::PAGE_END => 'onHtmlFilterEvent', ]; } diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index 8d552755ee..2afb92998f 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -30,6 +30,8 @@ final class ArrayFilterEvent extends Event public const POST_LOCAL_END = 'friendica.data.post_local_end'; + public const PHOTO_UPLOAD_FORM = 'friendica.data.photo_upload_form'; + private array $array; public function __construct(string $name, array $array) diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index 6e82ab09f6..adca43d239 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -22,21 +22,22 @@ class HookEventBridgeTest extends TestCase public function testGetStaticSubscribedEventsReturnsStaticMethods(): void { $expected = [ - Event::INIT => 'onNamedEvent', - Event::HOME_INIT => 'onNamedEvent', - ConfigLoadedEvent::CONFIG_LOADED => 'onConfigLoadedEvent', - ArrayFilterEvent::APP_MENU => 'onArrayFilterEvent', - ArrayFilterEvent::NAV_INFO => 'onArrayFilterEvent', - ArrayFilterEvent::FEATURE_ENABLED => 'onArrayFilterEvent', - ArrayFilterEvent::FEATURE_GET => 'onArrayFilterEvent', - ArrayFilterEvent::POST_LOCAL_START => 'onArrayFilterEvent', - ArrayFilterEvent::POST_LOCAL => 'onArrayFilterEvent', - ArrayFilterEvent::POST_LOCAL_END => 'onArrayFilterEvent', - HtmlFilterEvent::HEAD => 'onHtmlFilterEvent', - HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent', - HtmlFilterEvent::PAGE_HEADER => 'onHtmlFilterEvent', - HtmlFilterEvent::PAGE_CONTENT_TOP => 'onHtmlFilterEvent', - HtmlFilterEvent::PAGE_END => 'onHtmlFilterEvent', + Event::INIT => 'onNamedEvent', + Event::HOME_INIT => 'onNamedEvent', + ConfigLoadedEvent::CONFIG_LOADED => 'onConfigLoadedEvent', + ArrayFilterEvent::APP_MENU => 'onArrayFilterEvent', + ArrayFilterEvent::NAV_INFO => 'onArrayFilterEvent', + ArrayFilterEvent::FEATURE_ENABLED => 'onArrayFilterEvent', + ArrayFilterEvent::FEATURE_GET => 'onArrayFilterEvent', + ArrayFilterEvent::POST_LOCAL_START => 'onArrayFilterEvent', + ArrayFilterEvent::POST_LOCAL => 'onArrayFilterEvent', + ArrayFilterEvent::POST_LOCAL_END => 'onArrayFilterEvent', + ArrayFilterEvent::PHOTO_UPLOAD_FORM => 'onArrayFilterEvent', + HtmlFilterEvent::HEAD => 'onHtmlFilterEvent', + HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent', + HtmlFilterEvent::PAGE_HEADER => 'onHtmlFilterEvent', + HtmlFilterEvent::PAGE_CONTENT_TOP => 'onHtmlFilterEvent', + HtmlFilterEvent::PAGE_END => 'onHtmlFilterEvent', ]; $this->assertSame( @@ -127,6 +128,7 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::POST_LOCAL_START, 'post_local_start'], [ArrayFilterEvent::POST_LOCAL, 'post_local'], [ArrayFilterEvent::POST_LOCAL_END, 'post_local_end'], + [ArrayFilterEvent::PHOTO_UPLOAD_FORM, 'photo_upload_form'], ]; } From 18369854b784f6456259efbc69284418927c30f1 Mon Sep 17 00:00:00 2001 From: Art4 Date: Sat, 8 Feb 2025 21:59:28 +0000 Subject: [PATCH 06/35] Implement route_collection hook as CollectRoutesEvent --- src/App/Router.php | 17 +++-- src/Core/Hooks/HookEventBridge.php | 10 +++ src/Event/CollectRoutesEvent.php | 41 ++++++++++++ tests/Unit/Core/Hooks/HookEventBridgeTest.php | 33 +++++++++ tests/Unit/Event/CollectRoutesEventTest.php | 67 +++++++++++++++++++ 5 files changed, 162 insertions(+), 6 deletions(-) create mode 100644 src/Event/CollectRoutesEvent.php create mode 100644 tests/Unit/Event/CollectRoutesEventTest.php diff --git a/src/App/Router.php b/src/App/Router.php index deb0155cf0..2a8281a855 100644 --- a/src/App/Router.php +++ b/src/App/Router.php @@ -7,12 +7,10 @@ namespace Friendica\App; -use Dice\Dice; use FastRoute\DataGenerator\GroupCountBased; use FastRoute\Dispatcher; use FastRoute\RouteCollector; use FastRoute\RouteParser\Std; -use Friendica\Capabilities\ICanHandleRequests; use Friendica\Core\Addon; use Friendica\Core\Cache\Enum\Duration; use Friendica\Core\Cache\Capability\ICanCache; @@ -21,6 +19,7 @@ use Friendica\Core\Hook; use Friendica\Core\L10n; use Friendica\Core\Lock\Capability\ICanLock; use Friendica\Core\Session\Capability\IHandleUserSessions; +use Friendica\Event\CollectRoutesEvent; use Friendica\LegacyModule; use Friendica\Module\HTTPException\MethodNotAllowed; use Friendica\Module\HTTPException\PageNotFound; @@ -30,6 +29,7 @@ use Friendica\Network\HTTPException\InternalServerErrorException; use Friendica\Network\HTTPException\MethodNotAllowedException; use Friendica\Network\HTTPException\NotFoundException; use Friendica\Util\Router\FriendicaGroupCountBased; +use Psr\EventDispatcher\EventDispatcherInterface; use Psr\Log\LoggerInterface; /** @@ -86,6 +86,8 @@ class Router /** @var LoggerInterface */ private $logger; + private EventDispatcherInterface $eventDispatcher; + /** @var bool */ private $isLocalUser; @@ -110,7 +112,7 @@ class Router * @param IHandleUserSessions $userSession * @param RouteCollector|null $routeCollector */ - public function __construct(array $server, string $baseRoutesFilepath, L10n $l10n, ICanCache $cache, ICanLock $lock, IManageConfigValues $config, Arguments $args, LoggerInterface $logger, IHandleUserSessions $userSession, RouteCollector $routeCollector = null) + public function __construct(array $server, string $baseRoutesFilepath, L10n $l10n, ICanCache $cache, ICanLock $lock, IManageConfigValues $config, Arguments $args, LoggerInterface $logger, EventDispatcherInterface $eventDispatcher, IHandleUserSessions $userSession, RouteCollector $routeCollector = null) { $this->baseRoutesFilepath = $baseRoutesFilepath; $this->l10n = $l10n; @@ -120,6 +122,7 @@ class Router $this->config = $config; $this->server = $server; $this->logger = $logger; + $this->eventDispatcher = $eventDispatcher; $this->isLocalUser = !empty($userSession->getLocalUserId()); $this->routeCollector = $routeCollector ?? new RouteCollector(new Std(), new GroupCountBased()); @@ -147,10 +150,12 @@ class Router $this->addRoutes($routeCollector, $routes); - $this->routeCollector = $routeCollector; - // Add routes from addons - Hook::callAll('route_collection', $this->routeCollector); + $routeCollector = $this->eventDispatcher->dispatch( + new CollectRoutesEvent(CollectRoutesEvent::COLLECT_ROUTES, $routeCollector), + )->getRouteCollector(); + + $this->routeCollector = $routeCollector; return $this; } diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index 23cd209e11..1a66b74221 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -11,6 +11,7 @@ namespace Friendica\Core\Hooks; use Friendica\Core\Hook; use Friendica\Event\ArrayFilterEvent; +use Friendica\Event\CollectRoutesEvent; use Friendica\Event\ConfigLoadedEvent; use Friendica\Event\Event; use Friendica\Event\HtmlFilterEvent; @@ -37,6 +38,7 @@ final class HookEventBridge Event::INIT => 'init_1', Event::HOME_INIT => 'home_init', ConfigLoadedEvent::CONFIG_LOADED => 'load_config', + CollectRoutesEvent::COLLECT_ROUTES => 'route_collection', ArrayFilterEvent::APP_MENU => 'app_menu', ArrayFilterEvent::NAV_INFO => 'nav_info', ArrayFilterEvent::FEATURE_ENABLED => 'isEnabled', @@ -61,6 +63,7 @@ final class HookEventBridge Event::INIT => 'onNamedEvent', Event::HOME_INIT => 'onNamedEvent', ConfigLoadedEvent::CONFIG_LOADED => 'onConfigLoadedEvent', + CollectRoutesEvent::COLLECT_ROUTES => 'onCollectRoutesEvent', ArrayFilterEvent::APP_MENU => 'onArrayFilterEvent', ArrayFilterEvent::NAV_INFO => 'onArrayFilterEvent', ArrayFilterEvent::FEATURE_ENABLED => 'onArrayFilterEvent', @@ -87,6 +90,13 @@ final class HookEventBridge static::callHook($event->getName(), $event->getConfig()); } + public static function onCollectRoutesEvent(CollectRoutesEvent $event): void + { + $event->setRouteCollector( + static::callHook($event->getName(), $event->getRouteCollector()) + ); + } + public static function onArrayFilterEvent(ArrayFilterEvent $event): void { $event->setArray( diff --git a/src/Event/CollectRoutesEvent.php b/src/Event/CollectRoutesEvent.php new file mode 100644 index 0000000000..61e481ba9e --- /dev/null +++ b/src/Event/CollectRoutesEvent.php @@ -0,0 +1,41 @@ +routeCollector = $routeCollector; + } + + public function getRouteCollector(): RouteCollector + { + return $this->routeCollector; + } + + public function setRouteCollector(RouteCollector $routeCollector): void + { + $this->routeCollector = $routeCollector; + } +} diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index adca43d239..688e39ecfa 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -9,9 +9,11 @@ declare(strict_types=1); namespace Friendica\Test\Unit\Core\Hooks; +use FastRoute\RouteCollector; use Friendica\Core\Config\Util\ConfigFileManager; use Friendica\Core\Hooks\HookEventBridge; use Friendica\Event\ArrayFilterEvent; +use Friendica\Event\CollectRoutesEvent; use Friendica\Event\ConfigLoadedEvent; use Friendica\Event\Event; use Friendica\Event\HtmlFilterEvent; @@ -25,6 +27,7 @@ class HookEventBridgeTest extends TestCase Event::INIT => 'onNamedEvent', Event::HOME_INIT => 'onNamedEvent', ConfigLoadedEvent::CONFIG_LOADED => 'onConfigLoadedEvent', + CollectRoutesEvent::COLLECT_ROUTES => 'onCollectRoutesEvent', ArrayFilterEvent::APP_MENU => 'onArrayFilterEvent', ArrayFilterEvent::NAV_INFO => 'onArrayFilterEvent', ArrayFilterEvent::FEATURE_ENABLED => 'onArrayFilterEvent', @@ -117,6 +120,36 @@ class HookEventBridgeTest extends TestCase HookEventBridge::onConfigLoadedEvent($event); } + public static function getCollectRoutesEventData(): array + { + return [ + ['test', 'test'], + [CollectRoutesEvent::COLLECT_ROUTES, 'route_collection'], + ]; + } + + /** + * @dataProvider getCollectRoutesEventData + */ + public function testOnCollectRoutesEventCallsHookWithCorrectValue($name, $expected): void + { + $routeCollector = $this->createStub(RouteCollector::class); + + $event = new CollectRoutesEvent($name, $routeCollector); + + $reflectionProperty = new \ReflectionProperty(HookEventBridge::class, 'mockedCallHook'); + $reflectionProperty->setAccessible(true); + + $reflectionProperty->setValue(null, function (string $name, $data) use ($expected, $routeCollector) { + $this->assertSame($expected, $name); + $this->assertSame($routeCollector, $data); + + return $data; + }); + + HookEventBridge::onCollectRoutesEvent($event); + } + public static function getArrayFilterEventData(): array { return [ diff --git a/tests/Unit/Event/CollectRoutesEventTest.php b/tests/Unit/Event/CollectRoutesEventTest.php new file mode 100644 index 0000000000..0a1bb88d59 --- /dev/null +++ b/tests/Unit/Event/CollectRoutesEventTest.php @@ -0,0 +1,67 @@ +createStub(RouteCollector::class)); + + $this->assertInstanceOf(NamedEvent::class, $event); + } + + public static function getPublicConstants(): array + { + return [ + [CollectRoutesEvent::COLLECT_ROUTES, 'friendica.collect_routes'], + ]; + } + + /** + * @dataProvider getPublicConstants + */ + public function testPublicConstantsAreAvailable($value, $expected): void + { + $this->assertSame($expected, $value); + } + + public function testGetNameReturnsName(): void + { + $event = new CollectRoutesEvent('test', $this->createStub(RouteCollector::class)); + + $this->assertSame('test', $event->getName()); + } + + public function testGetRouteCollectorReturnsCorrectString(): void + { + $routeCollector = $this->createStub(RouteCollector::class); + + $event = new CollectRoutesEvent('test', $routeCollector); + + $this->assertSame($routeCollector, $event->getRouteCollector()); + } + + public function testSetRouteCollector(): void + { + $event = new CollectRoutesEvent('test', $this->createStub(RouteCollector::class)); + + $routeCollector = $this->createStub(RouteCollector::class); + + $event->setRouteCollector($routeCollector); + + $this->assertSame($routeCollector, $event->getRouteCollector()); + } +} From 23ce65c5d043254db5a1957cfaae65096b507a57 Mon Sep 17 00:00:00 2001 From: Art4 Date: Sat, 8 Feb 2025 22:00:30 +0000 Subject: [PATCH 07/35] Fix code style --- src/App/Router.php | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/App/Router.php b/src/App/Router.php index 2a8281a855..5490f07e19 100644 --- a/src/App/Router.php +++ b/src/App/Router.php @@ -114,16 +114,16 @@ class Router */ public function __construct(array $server, string $baseRoutesFilepath, L10n $l10n, ICanCache $cache, ICanLock $lock, IManageConfigValues $config, Arguments $args, LoggerInterface $logger, EventDispatcherInterface $eventDispatcher, IHandleUserSessions $userSession, RouteCollector $routeCollector = null) { - $this->baseRoutesFilepath = $baseRoutesFilepath; - $this->l10n = $l10n; - $this->cache = $cache; - $this->lock = $lock; - $this->args = $args; - $this->config = $config; - $this->server = $server; - $this->logger = $logger; - $this->eventDispatcher = $eventDispatcher; - $this->isLocalUser = !empty($userSession->getLocalUserId()); + $this->baseRoutesFilepath = $baseRoutesFilepath; + $this->l10n = $l10n; + $this->cache = $cache; + $this->lock = $lock; + $this->args = $args; + $this->config = $config; + $this->server = $server; + $this->logger = $logger; + $this->eventDispatcher = $eventDispatcher; + $this->isLocalUser = !empty($userSession->getLocalUserId()); $this->routeCollector = $routeCollector ?? new RouteCollector(new Std(), new GroupCountBased()); @@ -280,14 +280,14 @@ class Router try { // Check if the HTTP method is OPTIONS and return the special Options Module with the possible HTTP methods if ($this->args->getMethod() === static::OPTIONS) { - $this->moduleClass = Options::class; + $this->moduleClass = Options::class; $this->parameters[] = ['AllowedMethods' => $dispatcher->getOptions($cmd)]; } else { $routeInfo = $dispatcher->dispatch($this->args->getMethod(), $cmd); if ($routeInfo[0] === Dispatcher::FOUND) { - $this->moduleClass = $routeInfo[1]; + $this->moduleClass = $routeInfo[1]; $this->parameters[] = $routeInfo[2]; - } else if ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED) { + } elseif ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED) { throw new HTTPException\MethodNotAllowedException($this->l10n->t('Method not allowed for this module. Allowed method(s): %s', implode(', ', $routeInfo[1]))); } else { throw new HTTPException\NotFoundException($this->l10n->t('Page not found.')); From 3295b9055f28ab520e4f7fa74aa09b74feb888b2 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 13 Feb 2025 15:29:52 +0000 Subject: [PATCH 08/35] Create event for network_to_name hook --- src/Content/ContactSelector.php | 7 ++++++- src/Core/Hooks/HookEventBridge.php | 2 ++ src/Event/ArrayFilterEvent.php | 2 ++ tests/Unit/Core/Hooks/HookEventBridgeTest.php | 2 ++ 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Content/ContactSelector.php b/src/Content/ContactSelector.php index 1bd891a844..4cdb78e996 100644 --- a/src/Content/ContactSelector.php +++ b/src/Content/ContactSelector.php @@ -11,6 +11,7 @@ use Friendica\Core\Hook; use Friendica\Core\Protocol; use Friendica\Database\DBA; use Friendica\DI; +use Friendica\Event\ArrayFilterEvent; use Friendica\Util\Strings; /** @@ -113,6 +114,8 @@ class ContactSelector */ public static function networkToName(string $network, string $protocol = '', int $gsid = null): string { + $eventDispatcher = DI::eventDispatcher(); + $nets = [ Protocol::DFRN => DI::l10n()->t('DFRN'), Protocol::OSTATUS => DI::l10n()->t('OStatus'), @@ -135,7 +138,9 @@ class ContactSelector Protocol::BLUESKY => DI::l10n()->t('Bluesky'), ]; - Hook::callAll('network_to_name', $nets); + $nets = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::NETWORK_TO_NAME, $nets), + )->getArray(); $search = array_keys($nets); $replace = array_values($nets); diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index 1a66b74221..fca179a75b 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -47,6 +47,7 @@ final class HookEventBridge ArrayFilterEvent::POST_LOCAL => 'post_local', ArrayFilterEvent::POST_LOCAL_END => 'post_local_end', ArrayFilterEvent::PHOTO_UPLOAD_FORM => 'photo_upload_form', + ArrayFilterEvent::NETWORK_TO_NAME => 'network_to_name', HtmlFilterEvent::HEAD => 'head', HtmlFilterEvent::FOOTER => 'footer', HtmlFilterEvent::PAGE_HEADER => 'page_header', @@ -72,6 +73,7 @@ final class HookEventBridge ArrayFilterEvent::POST_LOCAL => 'onArrayFilterEvent', ArrayFilterEvent::POST_LOCAL_END => 'onArrayFilterEvent', ArrayFilterEvent::PHOTO_UPLOAD_FORM => 'onArrayFilterEvent', + ArrayFilterEvent::NETWORK_TO_NAME => 'onArrayFilterEvent', HtmlFilterEvent::HEAD => 'onHtmlFilterEvent', HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent', HtmlFilterEvent::PAGE_HEADER => 'onHtmlFilterEvent', diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index 2afb92998f..15579788a6 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -32,6 +32,8 @@ final class ArrayFilterEvent extends Event public const PHOTO_UPLOAD_FORM = 'friendica.data.photo_upload_form'; + public const NETWORK_TO_NAME = 'friendica.data.network_to_name'; + private array $array; public function __construct(string $name, array $array) diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index 688e39ecfa..195f0a0d0b 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -36,6 +36,7 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::POST_LOCAL => 'onArrayFilterEvent', ArrayFilterEvent::POST_LOCAL_END => 'onArrayFilterEvent', ArrayFilterEvent::PHOTO_UPLOAD_FORM => 'onArrayFilterEvent', + ArrayFilterEvent::NETWORK_TO_NAME => 'onArrayFilterEvent', HtmlFilterEvent::HEAD => 'onHtmlFilterEvent', HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent', HtmlFilterEvent::PAGE_HEADER => 'onHtmlFilterEvent', @@ -162,6 +163,7 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::POST_LOCAL, 'post_local'], [ArrayFilterEvent::POST_LOCAL_END, 'post_local_end'], [ArrayFilterEvent::PHOTO_UPLOAD_FORM, 'photo_upload_form'], + [ArrayFilterEvent::NETWORK_TO_NAME, 'network_to_name'], ]; } From 134d85b9c19b7fd1d6cfc382fd49be4b978ff936 Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 14 Feb 2025 07:24:47 +0000 Subject: [PATCH 09/35] Fix code style --- src/Content/ContactSelector.php | 47 ++++++++++++++++----------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/src/Content/ContactSelector.php b/src/Content/ContactSelector.php index 4cdb78e996..c864f159bb 100644 --- a/src/Content/ContactSelector.php +++ b/src/Content/ContactSelector.php @@ -7,7 +7,6 @@ namespace Friendica\Content; -use Friendica\Core\Hook; use Friendica\Core\Protocol; use Friendica\Database\DBA; use Friendica\DI; @@ -25,8 +24,8 @@ class ContactSelector const SVG_COLOR_WHITE = 2; const SVG_WHITE = 3; - static $serverdata = []; - static $server_id = []; + public static $serverdata = []; + public static $server_id = []; /** * @param string $current current @@ -36,7 +35,7 @@ class ContactSelector public static function pollInterval(string $current, bool $disabled = false): string { $dis = (($disabled) ? ' disabled="disabled" ' : ''); - $o = ''; + $o = ''; $o .= "