Merge "Special:BlockList: Update remove/change block links"
[mediawiki.git] / includes / skins / components / SkinComponentRegistry.php
blob21ec8b6ab40db4400a5323c16333dcd96368468f
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.
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.
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
19 namespace MediaWiki\Skin;
21 use MediaWiki\SpecialPage\SpecialPage;
22 use RuntimeException;
24 /**
25 * @internal for use inside Skin and SkinTemplate classes only
26 * @unstable
28 class SkinComponentRegistry {
29 /** @var SkinComponent[]|null null if not initialized. */
30 private $components = null;
32 /** @var SkinComponentRegistryContext */
33 private $skinContext;
35 public function __construct( SkinComponentRegistryContext $skinContext ) {
36 $this->skinContext = $skinContext;
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.
44 * @param string $name
45 * @throws RuntimeException with unknown name
46 * @return SkinComponent
48 public function getComponent( string $name ): SkinComponent {
49 if ( $this->components === null ) {
50 $this->registerComponents();
52 $component = $this->components[$name] ?? null;
53 if ( !$component ) {
54 throw new RuntimeException( 'Unknown component: ' . $name );
56 return $component;
59 /**
60 * Return all registered components.
62 * @since 1.38
63 * @return SkinComponent[]
65 public function getComponents() {
66 if ( $this->components === null ) {
67 $this->registerComponents();
69 return $this->components;
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.
77 * @param string $name
78 * @throws RuntimeException if given an unknown name
80 private function registerComponent( string $name ) {
81 $skin = $this->skinContext;
82 switch ( $name ) {
83 case 'copyright':
84 $component = new SkinComponentCopyright(
85 $skin
87 break;
88 case 'logos':
89 $component = new SkinComponentLogo(
90 $skin->getConfig(),
91 $skin->getLanguage()
93 break;
94 case 'search-box':
95 $component = new SkinComponentSearch(
96 $skin->getConfig(),
97 $skin->getMessageLocalizer(),
98 SpecialPage::newSearchPage( $skin->getUser() )
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()
108 break;
109 case 'footer':
110 $component = new SkinComponentFooter( $skin );
111 break;
112 default:
113 throw new RuntimeException( 'Unknown component: ' . $name );
115 $this->components[$name] = $component;
119 * Registers components used by skin.
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' );