mirror of
https://github.com/friendica/friendica
synced 2025-03-13 10:28:26 +00:00
Create ConfigLoadedEvent
This commit is contained in:
parent
7e199f034b
commit
0c406a3696
3 changed files with 53 additions and 2 deletions
|
@ -33,6 +33,7 @@ use Friendica\Core\System;
|
||||||
use Friendica\Core\Update;
|
use Friendica\Core\Update;
|
||||||
use Friendica\Database\Definition\DbaDefinition;
|
use Friendica\Database\Definition\DbaDefinition;
|
||||||
use Friendica\Database\Definition\ViewDefinition;
|
use Friendica\Database\Definition\ViewDefinition;
|
||||||
|
use Friendica\Event\ConfigLoadedEvent;
|
||||||
use Friendica\Event\Event;
|
use Friendica\Event\Event;
|
||||||
use Friendica\EventSubscriber\HookEventBridge;
|
use Friendica\EventSubscriber\HookEventBridge;
|
||||||
use Friendica\Module\Maintenance;
|
use Friendica\Module\Maintenance;
|
||||||
|
@ -177,6 +178,7 @@ class App
|
||||||
$this->mode,
|
$this->mode,
|
||||||
$this->config,
|
$this->config,
|
||||||
$this->profiler,
|
$this->profiler,
|
||||||
|
$this->container->create(EventDispatcherInterface::class),
|
||||||
$this->appHelper,
|
$this->appHelper,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -217,6 +219,7 @@ class App
|
||||||
$this->container->create(Mode::class),
|
$this->container->create(Mode::class),
|
||||||
$this->container->create(IManageConfigValues::class),
|
$this->container->create(IManageConfigValues::class),
|
||||||
$this->container->create(Profiler::class),
|
$this->container->create(Profiler::class),
|
||||||
|
$this->container->create(EventDispatcherInterface::class),
|
||||||
$this->container->create(AppHelper::class),
|
$this->container->create(AppHelper::class),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -247,6 +250,7 @@ class App
|
||||||
$this->container->create(Mode::class),
|
$this->container->create(Mode::class),
|
||||||
$this->container->create(IManageConfigValues::class),
|
$this->container->create(IManageConfigValues::class),
|
||||||
$this->container->create(Profiler::class),
|
$this->container->create(Profiler::class),
|
||||||
|
$this->container->create(EventDispatcherInterface::class),
|
||||||
$this->container->create(AppHelper::class),
|
$this->container->create(AppHelper::class),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -336,6 +340,7 @@ class App
|
||||||
Mode $mode,
|
Mode $mode,
|
||||||
IManageConfigValues $config,
|
IManageConfigValues $config,
|
||||||
Profiler $profiler,
|
Profiler $profiler,
|
||||||
|
EventDispatcherInterface $eventDispatcher,
|
||||||
AppHelper $appHelper
|
AppHelper $appHelper
|
||||||
): void {
|
): void {
|
||||||
if ($config->get('system', 'ini_max_execution_time') !== false) {
|
if ($config->get('system', 'ini_max_execution_time') !== false) {
|
||||||
|
@ -359,7 +364,8 @@ class App
|
||||||
if ($mode->has(Mode::DBAVAILABLE)) {
|
if ($mode->has(Mode::DBAVAILABLE)) {
|
||||||
Core\Hook::loadHooks();
|
Core\Hook::loadHooks();
|
||||||
$loader = (new Config())->createConfigFileManager($appHelper->getBasePath(), $serverParams);
|
$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
|
// Hooks are now working, reload the whole definitions with hook enabled
|
||||||
$dbaDefinition->load(true);
|
$dbaDefinition->load(true);
|
||||||
|
|
|
@ -10,6 +10,7 @@ declare(strict_types=1);
|
||||||
namespace Friendica\EventSubscriber;
|
namespace Friendica\EventSubscriber;
|
||||||
|
|
||||||
use Friendica\Core\Hook;
|
use Friendica\Core\Hook;
|
||||||
|
use Friendica\Event\ConfigLoadedEvent;
|
||||||
use Friendica\Event\Event;
|
use Friendica\Event\Event;
|
||||||
use Friendica\Event\HtmlFilterEvent;
|
use Friendica\Event\HtmlFilterEvent;
|
||||||
use Friendica\Event\NamedEvent;
|
use Friendica\Event\NamedEvent;
|
||||||
|
@ -33,6 +34,7 @@ final class HookEventBridge
|
||||||
*/
|
*/
|
||||||
private static array $eventMapper = [
|
private static array $eventMapper = [
|
||||||
Event::INIT => 'init_1',
|
Event::INIT => 'init_1',
|
||||||
|
ConfigLoadedEvent::CONFIG_LOADED => 'load_config',
|
||||||
HtmlFilterEvent::HEAD => 'head',
|
HtmlFilterEvent::HEAD => 'head',
|
||||||
HtmlFilterEvent::FOOTER => 'footer',
|
HtmlFilterEvent::FOOTER => 'footer',
|
||||||
HtmlFilterEvent::PAGE_CONTENT_TOP => 'page_content_top',
|
HtmlFilterEvent::PAGE_CONTENT_TOP => 'page_content_top',
|
||||||
|
@ -46,6 +48,7 @@ final class HookEventBridge
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
Event::INIT => 'onNamedEvent',
|
Event::INIT => 'onNamedEvent',
|
||||||
|
ConfigLoadedEvent::CONFIG_LOADED => 'onConfigLoadedEvent',
|
||||||
HtmlFilterEvent::HEAD => 'onHtmlFilterEvent',
|
HtmlFilterEvent::HEAD => 'onHtmlFilterEvent',
|
||||||
HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent',
|
HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent',
|
||||||
HtmlFilterEvent::PAGE_CONTENT_TOP => '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
|
* @param string|array $data
|
||||||
*
|
*
|
||||||
* @return string|array
|
* @return string|array|object
|
||||||
*/
|
*/
|
||||||
private static function callHook(string $name, $data)
|
private static function callHook(string $name, $data)
|
||||||
{
|
{
|
||||||
|
|
|
@ -9,6 +9,8 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Friendica\Test\Unit\EventSubscriber;
|
namespace Friendica\Test\Unit\EventSubscriber;
|
||||||
|
|
||||||
|
use Friendica\Core\Config\Util\ConfigFileManager;
|
||||||
|
use Friendica\Event\ConfigLoadedEvent;
|
||||||
use Friendica\Event\Event;
|
use Friendica\Event\Event;
|
||||||
use Friendica\Event\HtmlFilterEvent;
|
use Friendica\Event\HtmlFilterEvent;
|
||||||
use Friendica\EventSubscriber\HookEventBridge;
|
use Friendica\EventSubscriber\HookEventBridge;
|
||||||
|
@ -20,6 +22,7 @@ class HookEventBridgeTest extends TestCase
|
||||||
{
|
{
|
||||||
$expected = [
|
$expected = [
|
||||||
Event::INIT => 'onNamedEvent',
|
Event::INIT => 'onNamedEvent',
|
||||||
|
ConfigLoadedEvent::CONFIG_LOADED => 'onConfigLoadedEvent',
|
||||||
HtmlFilterEvent::HEAD => 'onHtmlFilterEvent',
|
HtmlFilterEvent::HEAD => 'onHtmlFilterEvent',
|
||||||
HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent',
|
HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent',
|
||||||
HtmlFilterEvent::PAGE_CONTENT_TOP => 'onHtmlFilterEvent',
|
HtmlFilterEvent::PAGE_CONTENT_TOP => 'onHtmlFilterEvent',
|
||||||
|
@ -72,6 +75,36 @@ class HookEventBridgeTest extends TestCase
|
||||||
HookEventBridge::onNamedEvent($event);
|
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
|
public static function getHtmlFilterEventData(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
|
Loading…
Add table
Reference in a new issue