Create HookRunner class and the hook handler interface

Bug: T271013
Change-Id: Ie8e152530599d11da12cb9272a3b757da1cca1ab
This commit is contained in:
Umherirrender 2023-06-04 00:32:28 +02:00
parent 024d0332f3
commit 4d6a470aae
4 changed files with 76 additions and 5 deletions

View file

@ -5,6 +5,7 @@ namespace MediaWiki\Extension\CodeEditor;
use ErrorPageError; use ErrorPageError;
use ExtensionRegistry; use ExtensionRegistry;
use MediaWiki\EditPage\EditPage; use MediaWiki\EditPage\EditPage;
use MediaWiki\Extension\CodeEditor\Hooks\HookRunner;
use MediaWiki\Hook\EditPage__showEditForm_initialHook; use MediaWiki\Hook\EditPage__showEditForm_initialHook;
use MediaWiki\Hook\EditPage__showReadOnlyForm_initialHook; use MediaWiki\Hook\EditPage__showReadOnlyForm_initialHook;
use MediaWiki\HookContainer\HookContainer; use MediaWiki\HookContainer\HookContainer;
@ -25,8 +26,8 @@ class Hooks implements
/** @var UserOptionsLookup */ /** @var UserOptionsLookup */
private $userOptionsLookup; private $userOptionsLookup;
/** @var HookContainer */ /** @var HookRunner */
private $hookContainer; private $hookRunner;
/** /**
* @param UserOptionsLookup $userOptionsLookup * @param UserOptionsLookup $userOptionsLookup
@ -37,7 +38,7 @@ class Hooks implements
HookContainer $hookContainer HookContainer $hookContainer
) { ) {
$this->userOptionsLookup = $userOptionsLookup; $this->userOptionsLookup = $userOptionsLookup;
$this->hookContainer = $hookContainer; $this->hookRunner = new HookRunner( $hookContainer );
} }
/** /**
@ -56,9 +57,8 @@ class Hooks implements
} }
// Give extensions a chance // Give extensions a chance
// Note: $model and $format were added around the time of MediaWiki 1.28.
$lang = null; $lang = null;
$this->hookContainer->run( 'CodeEditorGetPageLanguage', [ $title, &$lang, $model, $format ] ); $this->hookRunner->onCodeEditorGetPageLanguage( $title, $lang, $model, $format );
return $lang; return $lang;
} }

View file

@ -0,0 +1,25 @@
<?php
namespace MediaWiki\Extension\CodeEditor\Hooks;
use MediaWiki\Title\Title;
/**
* This is a hook handler interface, see docs/Hooks.md in core.
* Use the hook name "CodeEditorGetPageLanguage" to register handlers implementing this interface.
*
* @stable to implement
* @ingroup Hooks
*/
interface CodeEditorGetPageLanguageHook {
/**
* Allows to set a code language for extensions content models
*
* @param Title $title The title the language is for
* @param string|null &$lang The language to use
* @param string $model The content model of the title
* @param string $format The content format of the title
* @return bool|void True or no return value to continue or false to abort
*/
public function onCodeEditorGetPageLanguage( Title $title, ?string &$lang, string $model, string $format );
}

View file

@ -0,0 +1,30 @@
<?php
namespace MediaWiki\Extension\CodeEditor\Hooks;
use MediaWiki\HookContainer\HookContainer;
use MediaWiki\Title\Title;
/**
* This is a hook runner class, see docs/Hooks.md in core.
* @internal
*/
class HookRunner implements
CodeEditorGetPageLanguageHook
{
private HookContainer $hookContainer;
public function __construct( HookContainer $hookContainer ) {
$this->hookContainer = $hookContainer;
}
/**
* @inheritDoc
*/
public function onCodeEditorGetPageLanguage( Title $title, ?string &$lang, string $model, string $format ) {
return $this->hookContainer->run(
'CodeEditorGetPageLanguage',
[ $title, &$lang, $model, $format ]
);
}
}

View file

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