]> git.mxchange.org Git - friendica.git/commitdiff
Move HookEventBridge into Friendica\Core\Hooks namespace
authorArt4 <art4@wlabs.de>
Sat, 1 Feb 2025 16:12:25 +0000 (16:12 +0000)
committerHypolite Petovan <hypolite@mrpetovan.com>
Tue, 4 Feb 2025 18:23:50 +0000 (13:23 -0500)
src/App.php
src/Core/Hooks/HookEventBridge.php [new file with mode: 0644]
src/EventSubscriber/HookEventBridge.php [deleted file]
tests/Unit/Core/Hooks/HookEventBridgeTest.php [new file with mode: 0644]
tests/Unit/EventSubscriber/HookEventBridgeTest.php [deleted file]

index 738370ede35c461443937b79dd7bc02d3d925b2e..7f8e03b5e8b7344737f9230137698a51b56073eb 100644 (file)
@@ -20,6 +20,7 @@ use Friendica\Content\Nav;
 use Friendica\Core\Addon\Capability\ICanLoadAddons;
 use Friendica\Core\Config\Factory\Config;
 use Friendica\Core\Container;
+use Friendica\Core\Hooks\HookEventBridge;
 use Friendica\Core\Logger\LoggerManager;
 use Friendica\Core\Renderer;
 use Friendica\Core\Session\Capability\IHandleUserSessions;
@@ -35,7 +36,6 @@ use Friendica\Database\Definition\DbaDefinition;
 use Friendica\Database\Definition\ViewDefinition;
 use Friendica\Event\ConfigLoadedEvent;
 use Friendica\Event\Event;
-use Friendica\EventSubscriber\HookEventBridge;
 use Friendica\Module\Maintenance;
 use Friendica\Module\Special\HTTPException as ModuleHTTPException;
 use Friendica\Network\HTTPException;
diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php
new file mode 100644 (file)
index 0000000..2dad2d2
--- /dev/null
@@ -0,0 +1,113 @@
+<?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\Core\Hooks;
+
+use Friendica\Core\Hook;
+use Friendica\Event\ArrayFilterEvent;
+use Friendica\Event\ConfigLoadedEvent;
+use Friendica\Event\Event;
+use Friendica\Event\HtmlFilterEvent;
+use Friendica\Event\NamedEvent;
+
+/**
+ * Bridge between the EventDispatcher and the Hook class.
+ *
+ * @internal Provides BC
+ */
+final class HookEventBridge
+{
+       /**
+        * @internal 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',
+               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',
+       ];
+
+       /**
+        * @return array<string, string>
+        */
+       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',
+               ];
+       }
+
+       public static function onNamedEvent(NamedEvent $event): void
+       {
+               static::callHook($event->getName(), '');
+       }
+
+       public static function onConfigLoadedEvent(ConfigLoadedEvent $event): void
+       {
+               static::callHook($event->getName(), $event->getConfig());
+       }
+
+       public static function onArrayFilterEvent(ArrayFilterEvent $event): void
+       {
+               $event->setArray(
+                       static::callHook($event->getName(), $event->getArray())
+               );
+       }
+
+       public static function onHtmlFilterEvent(HtmlFilterEvent $event): void
+       {
+               $event->setHtml(
+                       static::callHook($event->getName(), $event->getHtml())
+               );
+       }
+
+       /**
+        * @param string|array|object $data
+        *
+        * @return string|array|object
+        */
+       private static function callHook(string $name, $data)
+       {
+               // If possible, map the event name to the legacy Hook name
+               $name = static::$eventMapper[$name] ?? $name;
+
+               // 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;
+       }
+}
diff --git a/src/EventSubscriber/HookEventBridge.php b/src/EventSubscriber/HookEventBridge.php
deleted file mode 100644 (file)
index f5dde4f..0000000
+++ /dev/null
@@ -1,113 +0,0 @@
-<?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;
-use Friendica\Event\ArrayFilterEvent;
-use Friendica\Event\ConfigLoadedEvent;
-use Friendica\Event\Event;
-use Friendica\Event\HtmlFilterEvent;
-use Friendica\Event\NamedEvent;
-
-/**
- * Bridge between the EventDispatcher and the Hook class.
- *
- * @internal Provides BC
- */
-final class HookEventBridge
-{
-       /**
-        * @internal 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',
-               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',
-       ];
-
-       /**
-        * @return array<string, string>
-        */
-       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',
-               ];
-       }
-
-       public static function onNamedEvent(NamedEvent $event): void
-       {
-               static::callHook($event->getName(), '');
-       }
-
-       public static function onConfigLoadedEvent(ConfigLoadedEvent $event): void
-       {
-               static::callHook($event->getName(), $event->getConfig());
-       }
-
-       public static function onArrayFilterEvent(ArrayFilterEvent $event): void
-       {
-               $event->setArray(
-                       static::callHook($event->getName(), $event->getArray())
-               );
-       }
-
-       public static function onHtmlFilterEvent(HtmlFilterEvent $event): void
-       {
-               $event->setHtml(
-                       static::callHook($event->getName(), $event->getHtml())
-               );
-       }
-
-       /**
-        * @param string|array|object $data
-        *
-        * @return string|array|object
-        */
-       private static function callHook(string $name, $data)
-       {
-               // If possible, map the event name to the legacy Hook name
-               $name = static::$eventMapper[$name] ?? $name;
-
-               // 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;
-       }
-}
diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php
new file mode 100644 (file)
index 0000000..671eb23
--- /dev/null
@@ -0,0 +1,176 @@
+<?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\Test\Unit\Core\Hooks;
+
+use Friendica\Core\Config\Util\ConfigFileManager;
+use Friendica\Core\Hooks\HookEventBridge;
+use Friendica\Event\ArrayFilterEvent;
+use Friendica\Event\ConfigLoadedEvent;
+use Friendica\Event\Event;
+use Friendica\Event\HtmlFilterEvent;
+use PHPUnit\Framework\TestCase;
+
+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',
+               ];
+
+               $this->assertSame(
+                       $expected,
+                       HookEventBridge::getStaticSubscribedEvents()
+               );
+
+               foreach ($expected as $methodName) {
+                       $this->assertTrue(
+                               method_exists(HookEventBridge::class, $methodName),
+                               $methodName . '() is not defined'
+                       );
+
+                       $this->assertTrue(
+                               (new \ReflectionMethod(HookEventBridge::class, $methodName))->isStatic(),
+                               $methodName . '() is not static'
+                       );
+               }
+       }
+
+       public static function getNamedEventData(): array
+       {
+               return [
+                       ['test', 'test'],
+                       [Event::INIT, 'init_1'],
+               ];
+       }
+
+       /**
+        * @dataProvider getNamedEventData
+        */
+       public function testOnNamedEventCallsHook($name, $expected): void
+       {
+               $event = new Event($name);
+
+               $reflectionProperty = new \ReflectionProperty(HookEventBridge::class, 'mockedCallHook');
+               $reflectionProperty->setAccessible(true);
+
+               $reflectionProperty->setValue(null, function (string $name, $data) use ($expected) {
+                       $this->assertSame($expected, $name);
+                       $this->assertSame('', $data);
+
+                       return $data;
+               });
+
+               HookEventBridge::onNamedEvent($event);
+       }
+
+       public static function getConfigLoadedEventData(): array
+       {
+               return [
+                       ['test', 'test'],
+                       [ConfigLoadedEvent::CONFIG_LOADED, 'load_config'],
+               ];
+       }
+
+       /**
+        * @dataProvider getConfigLoadedEventData
+        */
+       public function testOnConfigLoadedEventCallsHookWithCorrectValue($name, $expected): void
+       {
+               $config = $this->createStub(ConfigFileManager::class);
+
+               $event = new ConfigLoadedEvent($name, $config);
+
+               $reflectionProperty = new \ReflectionProperty(HookEventBridge::class, 'mockedCallHook');
+               $reflectionProperty->setAccessible(true);
+
+               $reflectionProperty->setValue(null, function (string $name, $data) use ($expected, $config) {
+                       $this->assertSame($expected, $name);
+                       $this->assertSame($config, $data);
+
+                       return $data;
+               });
+
+               HookEventBridge::onConfigLoadedEvent($event);
+       }
+
+       public static function getArrayFilterEventData(): array
+       {
+               return [
+                       ['test', 'test'],
+                       [ArrayFilterEvent::APP_MENU, 'app_menu'],
+                       [ArrayFilterEvent::NAV_INFO, 'nav_info'],
+                       [ArrayFilterEvent::FEATURE_ENABLED, 'isEnabled'],
+                       [ArrayFilterEvent::FEATURE_GET, 'get'],
+               ];
+       }
+
+       /**
+        * @dataProvider getArrayFilterEventData
+        */
+       public function testOnArrayFilterEventCallsHookWithCorrectValue($name, $expected): void
+       {
+               $event = new ArrayFilterEvent($name, ['original']);
+
+               $reflectionProperty = new \ReflectionProperty(HookEventBridge::class, 'mockedCallHook');
+               $reflectionProperty->setAccessible(true);
+
+               $reflectionProperty->setValue(null, function (string $name, $data) use ($expected) {
+                       $this->assertSame($expected, $name);
+                       $this->assertSame(['original'], $data);
+
+                       return $data;
+               });
+
+               HookEventBridge::onArrayFilterEvent($event);
+       }
+
+       public static function getHtmlFilterEventData(): array
+       {
+               return [
+                       ['test', 'test'],
+                       [HtmlFilterEvent::HEAD, 'head'],
+                       [HtmlFilterEvent::FOOTER, 'footer'],
+                       [HtmlFilterEvent::PAGE_HEADER, 'page_header'],
+                       [HtmlFilterEvent::PAGE_CONTENT_TOP, 'page_content_top'],
+                       [HtmlFilterEvent::PAGE_END, 'page_end'],
+               ];
+       }
+
+       /**
+        * @dataProvider getHtmlFilterEventData
+        */
+       public function testOnHtmlFilterEventCallsHookWithCorrectValue($name, $expected): void
+       {
+               $event = new HtmlFilterEvent($name, 'original');
+
+               $reflectionProperty = new \ReflectionProperty(HookEventBridge::class, 'mockedCallHook');
+               $reflectionProperty->setAccessible(true);
+
+               $reflectionProperty->setValue(null, function (string $name, $data) use ($expected) {
+                       $this->assertSame($expected, $name);
+                       $this->assertSame('original', $data);
+
+                       return $data;
+               });
+
+               HookEventBridge::onHtmlFilterEvent($event);
+       }
+}
diff --git a/tests/Unit/EventSubscriber/HookEventBridgeTest.php b/tests/Unit/EventSubscriber/HookEventBridgeTest.php
deleted file mode 100644 (file)
index 07bfa81..0000000
+++ /dev/null
@@ -1,176 +0,0 @@
-<?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\Test\Unit\EventSubscriber;
-
-use Friendica\Core\Config\Util\ConfigFileManager;
-use Friendica\Event\ArrayFilterEvent;
-use Friendica\Event\ConfigLoadedEvent;
-use Friendica\Event\Event;
-use Friendica\Event\HtmlFilterEvent;
-use Friendica\EventSubscriber\HookEventBridge;
-use PHPUnit\Framework\TestCase;
-
-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',
-               ];
-
-               $this->assertSame(
-                       $expected,
-                       HookEventBridge::getStaticSubscribedEvents()
-               );
-
-               foreach ($expected as $methodName) {
-                       $this->assertTrue(
-                               method_exists(HookEventBridge::class, $methodName),
-                               $methodName . '() is not defined'
-                       );
-
-                       $this->assertTrue(
-                               (new \ReflectionMethod(HookEventBridge::class, $methodName))->isStatic(),
-                               $methodName . '() is not static'
-                       );
-               }
-       }
-
-       public static function getNamedEventData(): array
-       {
-               return [
-                       ['test', 'test'],
-                       [Event::INIT, 'init_1'],
-               ];
-       }
-
-       /**
-        * @dataProvider getNamedEventData
-        */
-       public function testOnNamedEventCallsHook($name, $expected): void
-       {
-               $event = new Event($name);
-
-               $reflectionProperty = new \ReflectionProperty(HookEventBridge::class, 'mockedCallHook');
-               $reflectionProperty->setAccessible(true);
-
-               $reflectionProperty->setValue(null, function (string $name, $data) use ($expected) {
-                       $this->assertSame($expected, $name);
-                       $this->assertSame('', $data);
-
-                       return $data;
-               });
-
-               HookEventBridge::onNamedEvent($event);
-       }
-
-       public static function getConfigLoadedEventData(): array
-       {
-               return [
-                       ['test', 'test'],
-                       [ConfigLoadedEvent::CONFIG_LOADED, 'load_config'],
-               ];
-       }
-
-       /**
-        * @dataProvider getConfigLoadedEventData
-        */
-       public function testOnConfigLoadedEventCallsHookWithCorrectValue($name, $expected): void
-       {
-               $config = $this->createStub(ConfigFileManager::class);
-
-               $event = new ConfigLoadedEvent($name, $config);
-
-               $reflectionProperty = new \ReflectionProperty(HookEventBridge::class, 'mockedCallHook');
-               $reflectionProperty->setAccessible(true);
-
-               $reflectionProperty->setValue(null, function (string $name, $data) use ($expected, $config) {
-                       $this->assertSame($expected, $name);
-                       $this->assertSame($config, $data);
-
-                       return $data;
-               });
-
-               HookEventBridge::onConfigLoadedEvent($event);
-       }
-
-       public static function getArrayFilterEventData(): array
-       {
-               return [
-                       ['test', 'test'],
-                       [ArrayFilterEvent::APP_MENU, 'app_menu'],
-                       [ArrayFilterEvent::NAV_INFO, 'nav_info'],
-                       [ArrayFilterEvent::FEATURE_ENABLED, 'isEnabled'],
-                       [ArrayFilterEvent::FEATURE_GET, 'get'],
-               ];
-       }
-
-       /**
-        * @dataProvider getArrayFilterEventData
-        */
-       public function testOnArrayFilterEventCallsHookWithCorrectValue($name, $expected): void
-       {
-               $event = new ArrayFilterEvent($name, ['original']);
-
-               $reflectionProperty = new \ReflectionProperty(HookEventBridge::class, 'mockedCallHook');
-               $reflectionProperty->setAccessible(true);
-
-               $reflectionProperty->setValue(null, function (string $name, $data) use ($expected) {
-                       $this->assertSame($expected, $name);
-                       $this->assertSame(['original'], $data);
-
-                       return $data;
-               });
-
-               HookEventBridge::onArrayFilterEvent($event);
-       }
-
-       public static function getHtmlFilterEventData(): array
-       {
-               return [
-                       ['test', 'test'],
-                       [HtmlFilterEvent::HEAD, 'head'],
-                       [HtmlFilterEvent::FOOTER, 'footer'],
-                       [HtmlFilterEvent::PAGE_HEADER, 'page_header'],
-                       [HtmlFilterEvent::PAGE_CONTENT_TOP, 'page_content_top'],
-                       [HtmlFilterEvent::PAGE_END, 'page_end'],
-               ];
-       }
-
-       /**
-        * @dataProvider getHtmlFilterEventData
-        */
-       public function testOnHtmlFilterEventCallsHookWithCorrectValue($name, $expected): void
-       {
-               $event = new HtmlFilterEvent($name, 'original');
-
-               $reflectionProperty = new \ReflectionProperty(HookEventBridge::class, 'mockedCallHook');
-               $reflectionProperty->setAccessible(true);
-
-               $reflectionProperty->setValue(null, function (string $name, $data) use ($expected) {
-                       $this->assertSame($expected, $name);
-                       $this->assertSame('original', $data);
-
-                       return $data;
-               });
-
-               HookEventBridge::onHtmlFilterEvent($event);
-       }
-}