Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / includes / skins / SkinFactory.php
1 <?php
2
3 /**
4 * Copyright 2014
5 *
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.
10 *
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.
15 *
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
20 *
21 * @file
22 */
23
24 /**
25 * Factory class to create Skin objects
26 *
27 * @since 1.24
28 */
29 class SkinFactory {
30
31 /**
32 * Map of name => callback
33 * @var array
34 */
35 private $factoryFunctions = [];
36 /**
37 * Map of name => fallback human-readable name, used when the 'skinname-<skin>' message is not
38 * available
39 *
40 * @var array
41 */
42 private $displayNames = [];
43
44 /**
45 * Register a new Skin factory function.
46 *
47 * Will override if it's already registered.
48 *
49 * @param string $name Internal skin name. Should be all-lowercase (technically doesn't have
50 * to be, but doing so would change the case of i18n message keys).
51 * @param string $displayName For backwards-compatibility with old skin loading system. This is
52 * the text used as skin's human-readable name when the 'skinname-<skin>' message is not
53 * available. It should be the same as the skin name provided in $wgExtensionCredits.
54 * @param callable $callback Callback that takes the skin name as an argument
55 * @throws InvalidArgumentException If an invalid callback is provided
56 */
57 public function register( $name, $displayName, $callback ) {
58 if ( !is_callable( $callback ) ) {
59 throw new InvalidArgumentException( 'Invalid callback provided' );
60 }
61 $this->factoryFunctions[$name] = $callback;
62 $this->displayNames[$name] = $displayName;
63 }
64
65 /**
66 * Returns an associative array of:
67 * skin name => human readable name
68 *
69 * @return array
70 */
71 public function getSkinNames() {
72 return $this->displayNames;
73 }
74
75 /**
76 * Create a given Skin using the registered callback for $name.
77 * @param string $name Name of the skin you want
78 * @throws SkinException If a factory function isn't registered for $name
79 * @throws UnexpectedValueException If the factory function returns a non-Skin object
80 * @return Skin
81 */
82 public function makeSkin( $name ) {
83 if ( !isset( $this->factoryFunctions[$name] ) ) {
84 throw new SkinException( "No registered builder available for $name." );
85 }
86 $skin = call_user_func( $this->factoryFunctions[$name], $name );
87 if ( $skin instanceof Skin ) {
88 return $skin;
89 } else {
90 throw new UnexpectedValueException( "The builder for $name returned a non-Skin object." );
91 }
92 }
93 }