Debian Codegraph @

includes/skins/components/SkinComponentRegistry.php

raw symbols SCIP
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
19 namespace MediaWiki\Skin;
20
21 use MediaWiki\SpecialPage\SpecialPage;
22 use RuntimeException;
23
24 /**
25 * @internal for use inside Skin and SkinTemplate classes only
26 * @unstable
27 */
28 class SkinComponentRegistry {
29 /** @var SkinComponent[]|null null if not initialized. */
30 private $components = null;
31
32 /** @var SkinComponentRegistryContext */
33 private $skinContext;
34
35 /**
36 * @param SkinComponentRegistryContext $skinContext
37 */
38 public function __construct( SkinComponentRegistryContext $skinContext ) {
39 $this->skinContext = $skinContext;
40 }
41
42 /**
43 * Get a component. This method has side effects in that
44 * if registered components have been not initialized they
45 * will be registered as part of this method.
46 *
47 * @param string $name
48 * @throws RuntimeException with unknown name
49 * @return SkinComponent
50 */
51 public function getComponent( string $name ): SkinComponent {
52 if ( $this->components === null ) {
53 $this->registerComponents();
54 }
55 $component = $this->components[$name] ?? null;
56 if ( !$component ) {
57 throw new RuntimeException( 'Unknown component: ' . $name );
58 }
59 return $component;
60 }
61
62 /**
63 * Return all registered components.
64 *
65 * @since 1.38
66 * @return SkinComponent[]
67 */
68 public function getComponents() {
69 if ( $this->components === null ) {
70 $this->registerComponents();
71 }
72 return $this->components;
73 }
74
75 /**
76 * Registers a component for use with the skin.
77 * Private for now, but in future we may consider making this a
78 * public method to allow skins to extend component definitions.
79 *
80 * @param string $name
81 * @throws RuntimeException if given an unknown name
82 */
83 private function registerComponent( string $name ) {
84 $skin = $this->skinContext;
85 switch ( $name ) {
86 case 'copyright':
87 $component = new SkinComponentCopyright(
88 $skin
89 );
90 break;
91 case 'logos':
92 $component = new SkinComponentLogo(
93 $skin->getConfig(),
94 $skin->getLanguage()
95 );
96 break;
97 case 'search-box':
98 $component = new SkinComponentSearch(
99 $skin->getConfig(),
100 $skin->getMessageLocalizer(),
101 SpecialPage::newSearchPage( $skin->getUser() )
102 );
103 break;
104 case 'toc':
105 $component = new SkinComponentTableOfContents( $skin->getOutput() );
106 break;
107 case 'last-modified':
108 $component = new SkinComponentLastModified(
109 $skin, $skin->getOutput()->getRevisionTimestamp()
110 );
111 break;
112 case 'footer':
113 $component = new SkinComponentFooter( $skin );
114 break;
115 default:
116 throw new RuntimeException( 'Unknown component: ' . $name );
117 }
118 $this->components[$name] = $component;
119 }
120
121 /**
122 * Registers components used by skin.
123 */
124 private function registerComponents() {
125 $this->registerComponent( 'copyright' );
126 $this->registerComponent( 'last-modified' );
127 $this->registerComponent( 'logos' );
128 $this->registerComponent( 'toc' );
129 $this->registerComponent( 'search-box' );
130 $this->registerComponent( 'footer' );
131 }
132 }