Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
SkinComponentRegistry
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 5
240
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getComponent
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 getComponents
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 registerComponent
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
72
 registerComponents
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 */
18
19namespace MediaWiki\Skin;
20
21use MediaWiki\SpecialPage\SpecialPage;
22use RuntimeException;
23
24/**
25 * @internal for use inside Skin and SkinTemplate classes only
26 * @unstable
27 */
28class SkinComponentRegistry {
29    /** @var SkinComponent[]|null null if not initialized. */
30    private $components = null;
31
32    /** @var SkinComponentRegistryContext */
33    private $skinContext;
34
35    public function __construct( SkinComponentRegistryContext $skinContext ) {
36        $this->skinContext = $skinContext;
37    }
38
39    /**
40     * Get a component. This method has side effects in that
41     * if registered components have been not initialized they
42     * will be registered as part of this method.
43     *
44     * @param string $name
45     * @throws RuntimeException with unknown name
46     * @return SkinComponent
47     */
48    public function getComponent( string $name ): SkinComponent {
49        if ( $this->components === null ) {
50            $this->registerComponents();
51        }
52        $component = $this->components[$name] ?? null;
53        if ( !$component ) {
54            throw new RuntimeException( 'Unknown component: ' . $name );
55        }
56        return $component;
57    }
58
59    /**
60     * Return all registered components.
61     *
62     * @since 1.38
63     * @return SkinComponent[]
64     */
65    public function getComponents() {
66        if ( $this->components === null ) {
67            $this->registerComponents();
68        }
69        return $this->components;
70    }
71
72    /**
73     * Registers a component for use with the skin.
74     * Private for now, but in future we may consider making this a
75     * public method to allow skins to extend component definitions.
76     *
77     * @param string $name
78     * @throws RuntimeException if given an unknown name
79     */
80    private function registerComponent( string $name ) {
81        $skin = $this->skinContext;
82        switch ( $name ) {
83            case 'copyright':
84                $component = new SkinComponentCopyright(
85                    $skin
86                );
87                break;
88            case 'logos':
89                $component = new SkinComponentLogo(
90                    $skin->getConfig(),
91                    $skin->getLanguage()
92                );
93                break;
94            case 'search-box':
95                $component = new SkinComponentSearch(
96                    $skin->getConfig(),
97                    $skin->getMessageLocalizer(),
98                    SpecialPage::newSearchPage( $skin->getUser() )
99                );
100                break;
101            case 'toc':
102                $component = new SkinComponentTableOfContents( $skin->getOutput() );
103                break;
104            case 'last-modified':
105                $component = new SkinComponentLastModified(
106                    $skin, $skin->getOutput()->getRevisionTimestamp()
107                );
108                break;
109            case 'footer':
110                $component = new SkinComponentFooter( $skin );
111                break;
112            default:
113                throw new RuntimeException( 'Unknown component: ' . $name );
114        }
115        $this->components[$name] = $component;
116    }
117
118    /**
119     * Registers components used by skin.
120     */
121    private function registerComponents() {
122        $this->registerComponent( 'copyright' );
123        $this->registerComponent( 'last-modified' );
124        $this->registerComponent( 'logos' );
125        $this->registerComponent( 'toc' );
126        $this->registerComponent( 'search-box' );
127        $this->registerComponent( 'footer' );
128    }
129}