Create HookRunner class and the hook handler interface

Bug: T273303
Change-Id: I6d00848f07fc7d17f3dc7f44b7b6b61553219558
This commit is contained in:
Umherirrender 2023-06-04 20:12:56 +02:00
parent 6295079d77
commit 378769a974
4 changed files with 73 additions and 2 deletions

View file

@ -0,0 +1,23 @@
<?php
namespace MediaWiki\Extension\DiscussionTools\Hooks;
use Config;
use MessageLocalizer;
/**
* This is a hook handler interface, see docs/Hooks.md in core.
* Use the hook name "DiscussionToolsTermsOfUseMessages" to register handlers implementing this interface.
*
* @stable to implement
* @ingroup Hooks
*/
interface DiscussionToolsTermsOfUseMessagesHook {
/**
* @param array &$messages
* @param MessageLocalizer $context
* @param Config $config
* @return bool|void True or no return value to continue or false to abort
*/
public function onDiscussionToolsTermsOfUseMessages( array &$messages, MessageLocalizer $context, Config $config );
}

View file

@ -0,0 +1,31 @@
<?php
namespace MediaWiki\Extension\DiscussionTools\Hooks;
use Config;
use MediaWiki\HookContainer\HookContainer;
use MessageLocalizer;
/**
* This is a hook runner class, see docs/Hooks.md in core.
* @internal
*/
class HookRunner implements
DiscussionToolsTermsOfUseMessagesHook
{
private HookContainer $hookContainer;
public function __construct( HookContainer $hookContainer ) {
$this->hookContainer = $hookContainer;
}
/**
* @inheritDoc
*/
public function onDiscussionToolsTermsOfUseMessages( array &$messages, MessageLocalizer $context, Config $config ) {
return $this->hookContainer->run(
'DiscussionToolsTermsOfUseMessages',
[ &$messages, $context, $config ]
);
}
}

View file

@ -11,6 +11,7 @@ namespace MediaWiki\Extension\DiscussionTools;
use Config;
use ExtensionRegistry;
use MediaWiki\Extension\DiscussionTools\Hooks\HookRunner;
use MediaWiki\MediaWikiServices;
use MediaWiki\ResourceLoader as RL;
use MessageLocalizer;
@ -84,8 +85,8 @@ class ResourceLoaderData {
$context->msg( 'discussiontools-replywidget-newtopic' )->text() ],
];
$hookContainer = MediaWikiServices::getInstance()->getHookContainer();
$hookContainer->run( 'DiscussionToolsTermsOfUseMessages', [ &$messages, $context, $config ] );
$hookRunner = new HookRunner( MediaWikiServices::getInstance()->getHookContainer() );
$hookRunner->onDiscussionToolsTermsOfUseMessages( $messages, $context, $config );
return $messages;
}

View file

@ -0,0 +1,16 @@
<?php
namespace MediaWiki\Extension\DiscussionTools\Tests\Unit;
use MediaWiki\Extension\DiscussionTools\Hooks\HookRunner;
use MediaWiki\Tests\HookContainer\HookRunnerTestBase;
/**
* @covers \MediaWiki\Extension\DiscussionTools\Hooks\HookRunner
*/
class HookRunnerTest extends HookRunnerTestBase {
public static function provideHookRunners() {
yield HookRunner::class => [ HookRunner::class ];
}
}