]> git.mxchange.org Git - friendica.git/commitdiff
Create ConfigLoadedEvent
authorArt4 <art4@wlabs.de>
Mon, 27 Jan 2025 16:02:58 +0000 (16:02 +0000)
committerHypolite Petovan <hypolite@mrpetovan.com>
Tue, 4 Feb 2025 18:22:53 +0000 (13:22 -0500)
src/App.php
src/EventSubscriber/HookEventBridge.php
tests/Unit/EventSubscriber/HookEventBridgeTest.php

index ace40117208ad56b169321391379b5cd40b2f6f0..738370ede35c461443937b79dd7bc02d3d925b2e 100644 (file)
@@ -33,6 +33,7 @@ use Friendica\Core\System;
 use Friendica\Core\Update;
 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;
@@ -177,6 +178,7 @@ class App
                        $this->mode,
                        $this->config,
                        $this->profiler,
+                       $this->container->create(EventDispatcherInterface::class),
                        $this->appHelper,
                );
 
@@ -217,6 +219,7 @@ class App
                        $this->container->create(Mode::class),
                        $this->container->create(IManageConfigValues::class),
                        $this->container->create(Profiler::class),
+                       $this->container->create(EventDispatcherInterface::class),
                        $this->container->create(AppHelper::class),
                );
 
@@ -247,6 +250,7 @@ class App
                        $this->container->create(Mode::class),
                        $this->container->create(IManageConfigValues::class),
                        $this->container->create(Profiler::class),
+                       $this->container->create(EventDispatcherInterface::class),
                        $this->container->create(AppHelper::class),
                );
 
@@ -336,6 +340,7 @@ class App
                Mode $mode,
                IManageConfigValues $config,
                Profiler $profiler,
+               EventDispatcherInterface $eventDispatcher,
                AppHelper $appHelper
        ): void {
                if ($config->get('system', 'ini_max_execution_time') !== false) {
@@ -359,7 +364,8 @@ class App
                if ($mode->has(Mode::DBAVAILABLE)) {
                        Core\Hook::loadHooks();
                        $loader = (new Config())->createConfigFileManager($appHelper->getBasePath(), $serverParams);
-                       Core\Hook::callAll('load_config', $loader);
+
+                       $eventDispatcher->dispatch(new ConfigLoadedEvent(ConfigLoadedEvent::CONFIG_LOADED, $loader));
 
                        // Hooks are now working, reload the whole definitions with hook enabled
                        $dbaDefinition->load(true);
index f4da4f9dd51466a82d9bbfc70eb29e6e43f9126e..3c4e6adc28c61d2dce61279442e921953ada13db 100644 (file)
@@ -10,6 +10,7 @@ declare(strict_types=1);
 namespace Friendica\EventSubscriber;
 
 use Friendica\Core\Hook;
+use Friendica\Event\ConfigLoadedEvent;
 use Friendica\Event\Event;
 use Friendica\Event\HtmlFilterEvent;
 use Friendica\Event\NamedEvent;
@@ -33,6 +34,7 @@ final class HookEventBridge
         */
        private static array $eventMapper = [
                Event::INIT                       => 'init_1',
+               ConfigLoadedEvent::CONFIG_LOADED  => 'load_config',
                HtmlFilterEvent::HEAD             => 'head',
                HtmlFilterEvent::FOOTER           => 'footer',
                HtmlFilterEvent::PAGE_CONTENT_TOP => 'page_content_top',
@@ -46,6 +48,7 @@ final class HookEventBridge
        {
                return [
                        Event::INIT                       => 'onNamedEvent',
+                       ConfigLoadedEvent::CONFIG_LOADED  => 'onConfigLoadedEvent',
                        HtmlFilterEvent::HEAD             => 'onHtmlFilterEvent',
                        HtmlFilterEvent::FOOTER           => 'onHtmlFilterEvent',
                        HtmlFilterEvent::PAGE_CONTENT_TOP => 'onHtmlFilterEvent',
@@ -73,10 +76,19 @@ final class HookEventBridge
                );
        }
 
+       public static function onConfigLoadedEvent(ConfigLoadedEvent $event): void
+       {
+               $name = $event->getName();
+
+               $name = static::$eventMapper[$name] ?? $name;
+
+               static::callHook($name, $event->getConfig());
+       }
+
        /**
         * @param string|array $data
         *
-        * @return string|array
+        * @return string|array|object
         */
        private static function callHook(string $name, $data)
        {
index ec04ded87ec91745e9377b950cf3b41e1a134917..ea5460eca36ea3c19c0e1e876e401e24d48765ca 100644 (file)
@@ -9,6 +9,8 @@ declare(strict_types=1);
 
 namespace Friendica\Test\Unit\EventSubscriber;
 
+use Friendica\Core\Config\Util\ConfigFileManager;
+use Friendica\Event\ConfigLoadedEvent;
 use Friendica\Event\Event;
 use Friendica\Event\HtmlFilterEvent;
 use Friendica\EventSubscriber\HookEventBridge;
@@ -20,6 +22,7 @@ class HookEventBridgeTest extends TestCase
        {
                $expected = [
                        Event::INIT                       => 'onNamedEvent',
+                       ConfigLoadedEvent::CONFIG_LOADED  => 'onConfigLoadedEvent',
                        HtmlFilterEvent::HEAD             => 'onHtmlFilterEvent',
                        HtmlFilterEvent::FOOTER           => 'onHtmlFilterEvent',
                        HtmlFilterEvent::PAGE_CONTENT_TOP => 'onHtmlFilterEvent',
@@ -72,6 +75,36 @@ class HookEventBridgeTest extends TestCase
                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 getHtmlFilterEventData(): array
        {
                return [