Create HookRunner class and the hook handler interfaces

Bug: T271026
Change-Id: If525e0f950b3a098162b19373edb3b443f559bf2
This commit is contained in:
Umherirrender 2023-06-04 16:54:02 +02:00 committed by jenkins-bot
parent 8811ea784e
commit 2ee5768ef5
5 changed files with 99 additions and 4 deletions

View file

@ -0,0 +1,40 @@
<?php
namespace MediaWiki\Extension\Scribunto\Hooks;
use MediaWiki\HookContainer\HookContainer;
/**
* This is a hook runner class, see docs/Hooks.md in core.
* @internal
*/
class HookRunner implements
ScribuntoExternalLibrariesHook,
ScribuntoExternalLibraryPathsHook
{
private HookContainer $hookContainer;
public function __construct( HookContainer $hookContainer ) {
$this->hookContainer = $hookContainer;
}
/**
* @inheritDoc
*/
public function onScribuntoExternalLibraries( string $engine, array &$extraLibraries ) {
return $this->hookContainer->run(
'ScribuntoExternalLibraries',
[ $engine, &$extraLibraries ]
);
}
/**
* @inheritDoc
*/
public function onScribuntoExternalLibraryPaths( string $engine, array &$extraLibraryPaths ) {
return $this->hookContainer->run(
'ScribuntoExternalLibraryPaths',
[ $engine, &$extraLibraryPaths ]
);
}
}

View file

@ -0,0 +1,19 @@
<?php
namespace MediaWiki\Extension\Scribunto\Hooks;
/**
* This is a hook handler interface, see docs/Hooks.md in core.
* Use the hook name "ScribuntoExternalLibraries" to register handlers implementing this interface.
*
* @stable to implement
* @ingroup Hooks
*/
interface ScribuntoExternalLibrariesHook {
/**
* @param string $engine
* @param array &$extraLibraries
* @return bool|void True or no return value to continue or false to abort
*/
public function onScribuntoExternalLibraries( string $engine, array &$extraLibraries );
}

View file

@ -0,0 +1,19 @@
<?php
namespace MediaWiki\Extension\Scribunto\Hooks;
/**
* This is a hook handler interface, see docs/Hooks.md in core.
* Use the hook name "ScribuntoExternalLibraryPaths" to register handlers implementing this interface.
*
* @stable to implement
* @ingroup Hooks
*/
interface ScribuntoExternalLibraryPathsHook {
/**
* @param string $engine
* @param array &$extraLibraryPaths
* @return bool|void True or no return value to continue or false to abort
*/
public function onScribuntoExternalLibraryPaths( string $engine, array &$extraLibraryPaths );
}

View file

@ -22,6 +22,7 @@
namespace MediaWiki\Extension\Scribunto;
use MediaWiki\Extension\Scribunto\Hooks\HookRunner;
use MediaWiki\MediaWikiServices;
use Parser;
use ParserOutput;
@ -256,8 +257,8 @@ abstract class ScribuntoEngineBase {
*/
protected function getLibraries( $engine, array $coreLibraries = [] ) {
$extraLibraries = [];
MediaWikiServices::getInstance()->getHookContainer()
->run( 'ScribuntoExternalLibraries', [ $engine, &$extraLibraries ] );
( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )
->onScribuntoExternalLibraries( $engine, $extraLibraries );
return $coreLibraries + $extraLibraries;
}
@ -270,8 +271,8 @@ abstract class ScribuntoEngineBase {
*/
protected function getLibraryPaths( $engine, array $coreLibraryPaths = [] ) {
$extraLibraryPaths = [];
MediaWikiServices::getInstance()->getHookContainer()
->run( 'ScribuntoExternalLibraryPaths', [ $engine, &$extraLibraryPaths ] );
( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )
->onScribuntoExternalLibraryPaths( $engine, $extraLibraryPaths );
return array_merge( $coreLibraryPaths, $extraLibraryPaths );
}

View file

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