Move remaining LoadBalancer classes to Rdbms
[mediawiki.git] / includes / skins / SkinFactory.php
blobcc993aaf9b50eebd09f2677ad67d90c175f1228d
1 <?php
3 /**
4 * Copyright 2014
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
21 * @file
24 use MediaWiki\MediaWikiServices;
26 /**
27 * Factory class to create Skin objects
29 * @since 1.24
31 class SkinFactory {
33 /**
34 * Map of name => callback
35 * @var array
37 private $factoryFunctions = [];
38 /**
39 * Map of name => fallback human-readable name, used when the 'skinname-<skin>' message is not
40 * available
42 * @var array
44 private $displayNames = [];
46 /**
47 * @deprecated in 1.27
48 * @return SkinFactory
50 public static function getDefaultInstance() {
51 return MediaWikiServices::getInstance()->getSkinFactory();
54 /**
55 * Register a new Skin factory function.
57 * Will override if it's already registered.
59 * @param string $name Internal skin name. Should be all-lowercase (technically doesn't have
60 * to be, but doing so would change the case of i18n message keys).
61 * @param string $displayName For backwards-compatibility with old skin loading system. This is
62 * the text used as skin's human-readable name when the 'skinname-<skin>' message is not
63 * available. It should be the same as the skin name provided in $wgExtensionCredits.
64 * @param callable $callback Callback that takes the skin name as an argument
65 * @throws InvalidArgumentException If an invalid callback is provided
67 public function register( $name, $displayName, $callback ) {
68 if ( !is_callable( $callback ) ) {
69 throw new InvalidArgumentException( 'Invalid callback provided' );
71 $this->factoryFunctions[$name] = $callback;
72 $this->displayNames[$name] = $displayName;
75 /**
76 * Returns an associative array of:
77 * skin name => human readable name
79 * @return array
81 public function getSkinNames() {
82 return $this->displayNames;
85 /**
86 * Create a given Skin using the registered callback for $name.
87 * @param string $name Name of the skin you want
88 * @throws SkinException If a factory function isn't registered for $name
89 * @throws UnexpectedValueException If the factory function returns a non-Skin object
90 * @return Skin
92 public function makeSkin( $name ) {
93 if ( !isset( $this->factoryFunctions[$name] ) ) {
94 throw new SkinException( "No registered builder available for $name." );
96 $skin = call_user_func( $this->factoryFunctions[$name], $name );
97 if ( $skin instanceof Skin ) {
98 return $skin;
99 } else {
100 throw new UnexpectedValueException( "The builder for $name returned a non-Skin object." );