Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
MixinController
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 8
342
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getContext
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getMagicWords
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 loadPhp
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
 getPreloadJsSnippets
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
 getResourceLoaderModules
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 registerMagicWord
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 renderMagicWord
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3use MediaWiki\Context\IContextSource;
4
5class MixinController {
6    /** @var array */
7    private $mixins;
8
9    /** @var array */
10    private $magicWords = [];
11    /** @var IContextSource */
12    private $uiContext;
13
14    public function __construct( IContextSource $uiContext, $mixins ) {
15        $this->uiContext = $uiContext;
16        $this->mixins = $mixins;
17
18        $this->loadPhp();
19    }
20
21    public function getContext() {
22        return $this->uiContext;
23    }
24
25    public function getMagicWords() {
26        $words = array_keys( $this->magicWords );
27        sort( $words );
28        return $words;
29    }
30
31    /**
32     * Initialize php modules.
33     */
34    public function loadPhp() {
35        foreach ( $this->mixins as $name => $info ) {
36            if ( !empty( $info['php'] ) ) {
37                // The module must register itself with this controller.
38                $php_module_path = $info['localBasePath'] . DIRECTORY_SEPARATOR . $info['php'];
39                // Strip the file extension and assume the mixin class is eponymous.
40                // TODO: maybe they should be registered using hooks instead...
41                $php_module_name = preg_replace( "/[.].+$/", "", $info['php'] );
42                require_once $php_module_path;
43                $mod = new $php_module_name();
44                if ( !( $mod instanceof IBannerMixin ) ) {
45                    throw new MixinNotFoundException( $name );
46                }
47                $mod->register( $this );
48            }
49        }
50    }
51
52    public function getPreloadJsSnippets() {
53        $snippets = [];
54        foreach ( $this->mixins as $name => $info ) {
55            if ( !empty( $info['preloadJs'] ) ) {
56                $filename = $info['localBasePath'] . DIRECTORY_SEPARATOR . $info['preloadJs'];
57                $snippet = file_get_contents( $filename );
58                if ( !$snippet ) {
59                    throw new MixinNotFoundException( $name );
60                }
61                $snippets[$name] = $snippet;
62            }
63        }
64        return $snippets;
65    }
66
67    public function getResourceLoaderModules() {
68        $modules = [];
69        foreach ( $this->mixins as $name => $info ) {
70            if ( !empty( $info['resourceLoader'] ) ) {
71                $modules[$name] = $info['resourceLoader'];
72            }
73        }
74        return $modules;
75    }
76
77    public function registerMagicWord( $word, $callback ) {
78        $this->magicWords[$word] = $callback;
79    }
80
81    public function renderMagicWord( $word, $params = [] ) {
82        if ( array_key_exists( $word, $this->magicWords ) ) {
83            $callback = $this->magicWords[$word];
84            if ( is_callable( $callback ) ) {
85                return call_user_func_array( $callback, $params );
86            }
87        }
88    }
89}