Migrate ResourceLoaderCodeMirrorModule to a virtual file callback

Bug: T47514
Depends-On: I97d61b5793159cea365740e0563f7b733e0f16de
Change-Id: I31b80fc1c7701fc1075d655270706e341942415d
This commit is contained in:
Tim Starling 2023-05-05 16:10:57 +10:00
parent 025910fccd
commit 7a3f46b490
3 changed files with 20 additions and 36 deletions

View file

@ -51,7 +51,12 @@
]
},
"ext.CodeMirror.data": {
"class": "MediaWiki\\Extension\\CodeMirror\\ResourceLoaderCodeMirrorModule"
"scripts": [
{
"name": "ext.CodeMirror.data.js",
"callback": "MediaWiki\\Extension\\CodeMirror\\DataScript::makeScript"
}
]
},
"ext.CodeMirror.lib": {
"scripts": [

View file

@ -1,6 +1,6 @@
<?php
/**
* ResourceLoader ext.CodeMirror module
* ResourceLoader callback for ext.CodeMirror.data module
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -28,34 +28,17 @@ use MediaWiki\ResourceLoader as RL;
use MediaWiki\ResourceLoader\ResourceLoader;
/**
* ResourceLoader module for ext.CodeMirror
* ResourceLoader callback for ext.CodeMirror.data
*/
class ResourceLoaderCodeMirrorModule extends RL\FileModule {
class DataScript {
/**
* @inheritDoc
* @param RL\Context $context
* @return string
*/
public function getScript( RL\Context $context ) {
public static function makeScript( RL\Context $context ) {
return ResourceLoader::makeConfigSetScript(
[ 'extCodeMirrorConfig' => $this->getFrontendConfiguraton() ]
)
. "\n"
. parent::getScript( $context );
}
/**
* @inheritDoc
*/
public function supportsURLLoading() {
// This module does not support loading URLs, because it inserts
// JS config vars into the module by the getScript function.
return false;
}
/**
* @inheritDoc
*/
public function enableModuleContentVersion() {
return true;
[ 'extCodeMirrorConfig' => self::getFrontendConfiguraton() ]
);
}
/**
@ -63,7 +46,7 @@ class ResourceLoaderCodeMirrorModule extends RL\FileModule {
*
* @return array
*/
private function getFrontendConfiguraton() {
private static function getFrontendConfiguraton() {
// Use the content language, not the user language. (See T170130.)
$lang = MediaWikiServices::getInstance()->getContentLanguage();
$registry = ExtensionRegistry::getInstance();

View file

@ -2,22 +2,18 @@
namespace MediaWiki\Extension\CodeMirror\Tests;
use MediaWiki\Extension\CodeMirror\ResourceLoaderCodeMirrorModule;
use MediaWiki\Extension\CodeMirror\DataScript;
use MediaWiki\ResourceLoader\Context;
/**
* @covers \MediaWiki\Extension\CodeMirror\ResourceLoaderCodeMirrorModule
* @covers \MediaWiki\Extension\CodeMirror\DataScript
*/
class ResourceLoaderCodeMirrorModuleTest extends \MediaWikiIntegrationTestCase {
class DataScriptTest extends \MediaWikiIntegrationTestCase {
public function testResourceLoaderModule() {
public function testMakeScript() {
$context = $this->createMock( Context::class );
$module = new ResourceLoaderCodeMirrorModule();
$this->assertFalse( $module->supportsURLLoading() );
$this->assertTrue( $module->enableModuleContentVersion() );
$script = $module->getScript( $context );
$script = DataScript::makeScript( $context );
$this->assertStringContainsString( '"extCodeMirrorConfig":', $script );
$this->assertStringContainsString( '"pluginModules":', $script );
$this->assertStringContainsString( '"tagModes":', $script );