Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
8 / 8 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
TemplateModule | |
100.00% |
8 / 8 |
|
100.00% |
2 / 2 |
2 | |
100.00% |
1 / 1 |
getScript | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
getVersion | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace Wikibase\Lexeme\Presentation\View; |
4 | |
5 | use FormatJson; |
6 | // phpcs:disable MediaWiki.Classes.FullQualifiedClassName -- T308814 |
7 | use MediaWiki\ResourceLoader as RL; |
8 | use Wikibase\View\Template\TemplateRegistry; |
9 | |
10 | /** |
11 | * Injects templates into JavaScript. |
12 | * |
13 | * @license GPL-2.0-or-later |
14 | */ |
15 | class TemplateModule { |
16 | |
17 | /** |
18 | * Get templates.php as a JavaScript function call |
19 | * |
20 | * @param RL\Context $context |
21 | * |
22 | * @return string |
23 | */ |
24 | public static function getScript( RL\Context $context ) { |
25 | $templates = include __DIR__ . '/../../../resources/templates.php'; |
26 | $templateRegistry = new TemplateRegistry( $templates ); |
27 | |
28 | $templatesJson = FormatJson::encode( $templateRegistry->getTemplates() ); |
29 | |
30 | // template store JavaScript initialisation |
31 | return <<<JS |
32 | ( function () { |
33 | 'use strict'; |
34 | |
35 | mw.wbTemplates.store.set( $.extend( $templatesJson, mw.wbTemplates.store.values ) ); |
36 | |
37 | }() ); |
38 | JS; |
39 | } |
40 | |
41 | /** |
42 | * Get the version corresponding to getScript() |
43 | * |
44 | * @param RL\Context $context |
45 | * @return RL\FilePath |
46 | */ |
47 | public static function getVersion( RL\Context $context ) { |
48 | return new RL\FilePath( 'templates.php' ); |
49 | } |
50 | |
51 | } |