Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 42 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
MustacheDomTemplateParser | |
0.00% |
0 / 42 |
|
0.00% |
0 / 2 |
42 | |
0.00% |
0 / 1 |
getTemplateFilename | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
compile | |
0.00% |
0 / 40 |
|
0.00% |
0 / 1 |
30 |
1 | <?php |
2 | |
3 | namespace Wikibase\MediaInfo; |
4 | |
5 | use FileContentsHasher; |
6 | use LightnCandy\LightnCandy; |
7 | use MediaWiki\Html\TemplateParser; |
8 | use RuntimeException; |
9 | |
10 | /** |
11 | * A simple core Mustache TemplateParser override to allow it to |
12 | * compile files with `.mustache+dom` extension, as might be used |
13 | * on the frontend for more advanced DOM-based rendering. |
14 | */ |
15 | class MustacheDomTemplateParser extends TemplateParser { |
16 | |
17 | /** |
18 | * @inheritDoc |
19 | */ |
20 | protected function getTemplateFilename( $templateName ) { |
21 | // perform parent's validity checks... |
22 | parent::getTemplateFilename( $templateName ); |
23 | |
24 | // but use a different filename |
25 | return "{$this->templateDir}/{$templateName}.mustache+dom"; |
26 | } |
27 | |
28 | /** |
29 | * @suppress PhanTypeMismatchArgument |
30 | * @inheritDoc |
31 | */ |
32 | protected function compile( $templateName ) { |
33 | $filename = $this->getTemplateFilename( $templateName ); |
34 | |
35 | if ( !file_exists( $filename ) ) { |
36 | throw new RuntimeException( "Could not find template `{$templateName}` at {$filename}" ); |
37 | } |
38 | |
39 | $files = [ $filename ]; |
40 | $contents = file_get_contents( $filename ); |
41 | $compiled = LightnCandy::compile( |
42 | $contents, |
43 | [ |
44 | 'flags' => $this->compileFlags, |
45 | 'basedir' => $this->templateDir, |
46 | 'fileext' => '.mustache+dom', |
47 | 'partialresolver' => function ( $cx, $partialName ) use ( $templateName, &$files ) { |
48 | $filename = "{$this->templateDir}/{$partialName}.mustache+dom"; |
49 | if ( !file_exists( $filename ) ) { |
50 | throw new RuntimeException( sprintf( |
51 | 'Could not compile template `%s`: Could not find partial `%s` at %s', |
52 | $templateName, |
53 | $partialName, |
54 | $filename |
55 | ) ); |
56 | } |
57 | |
58 | $fileContents = file_get_contents( $filename ); |
59 | |
60 | if ( $fileContents === false ) { |
61 | throw new RuntimeException( sprintf( |
62 | 'Could not compile template `%s`: Could not find partial `%s` at %s', |
63 | $templateName, |
64 | $partialName, |
65 | $filename |
66 | ) ); |
67 | } |
68 | |
69 | $files[] = $filename; |
70 | |
71 | return $fileContents; |
72 | } |
73 | ] |
74 | ); |
75 | if ( !$compiled ) { |
76 | // This shouldn't happen because LightnCandy::FLAG_ERROR_EXCEPTION is set |
77 | // Errors should throw exceptions instead of returning false |
78 | // Check anyway for paranoia |
79 | throw new RuntimeException( "Could not compile template `{$filename}`" ); |
80 | } |
81 | |
82 | return [ |
83 | 'phpCode' => $compiled, |
84 | 'files' => $files, |
85 | 'filesHash' => FileContentsHasher::getFileContentsHash( $files ), |
86 | ]; |
87 | } |
88 | |
89 | } |