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