Create ConfigLoadedEvent

This commit is contained in:
Art4 2025-01-27 16:02:58 +00:00 committed by Hypolite Petovan
parent 7e199f034b
commit 0c406a3696
3 changed files with 53 additions and 2 deletions

View 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);

View 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)
{

View 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 [