PHPSessionHandler: Implement SessionHandlerInterface
[mediawiki.git] / includes / skins / SkinFactory.php
blobffbe6293f8ea61230dad3832acc261835795eae2
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 /**
25 * Factory class to create Skin objects
27 * @since 1.24
29 class SkinFactory {
31 /**
32 * Map of name => callback
33 * @var array
35 private $factoryFunctions = array();
36 /**
37 * Map of name => fallback human-readable name, used when the 'skinname-<skin>' message is not
38 * available
40 * @var array
42 private $displayNames = array();
44 /**
45 * @var SkinFactory
47 private static $self;
49 public static function getDefaultInstance() {
50 if ( !self::$self ) {
51 self::$self = new self;
54 return self::$self;
57 /**
58 * Register a new Skin factory function.
60 * Will override if it's already registered.
62 * @param string $name Internal skin name. Should be all-lowercase (technically doesn't have
63 * to be, but doing so would change the case of i18n message keys).
64 * @param string $displayName For backwards-compatibility with old skin loading system. This is
65 * the text used as skin's human-readable name when the 'skinname-<skin>' message is not
66 * available. It should be the same as the skin name provided in $wgExtensionCredits.
67 * @param callable $callback Callback that takes the skin name as an argument
68 * @throws InvalidArgumentException If an invalid callback is provided
70 public function register( $name, $displayName, $callback ) {
71 if ( !is_callable( $callback ) ) {
72 throw new InvalidArgumentException( 'Invalid callback provided' );
74 $this->factoryFunctions[$name] = $callback;
75 $this->displayNames[$name] = $displayName;
78 /**
79 * Returns an associative array of:
80 * skin name => human readable name
82 * @return array
84 public function getSkinNames() {
85 return $this->displayNames;
88 /**
89 * Create a given Skin using the registered callback for $name.
90 * @param string $name Name of the skin you want
91 * @throws SkinException If a factory function isn't registered for $name
92 * @throws UnexpectedValueException If the factory function returns a non-Skin object
93 * @return Skin
95 public function makeSkin( $name ) {
96 if ( !isset( $this->factoryFunctions[$name] ) ) {
97 throw new SkinException( "No registered builder available for $name." );
99 $skin = call_user_func( $this->factoryFunctions[$name], $name );
100 if ( $skin instanceof Skin ) {
101 return $skin;
102 } else {
103 throw new UnexpectedValueException( "The builder for $name returned a non-Skin object." );