X-Git-Url: https://scripts.mit.edu/gitweb/autoinstallsdev/mediawiki.git/blobdiff_plain/19e297c21b10b1b8a3acad5e73fc71dcb35db44a..6932310fd58ebef145fa01eb76edf7150284d8ea:/includes/skins/SkinFactory.php diff --git a/includes/skins/SkinFactory.php b/includes/skins/SkinFactory.php new file mode 100644 index 00000000..cc993aaf --- /dev/null +++ b/includes/skins/SkinFactory.php @@ -0,0 +1,103 @@ + callback + * @var array + */ + private $factoryFunctions = []; + /** + * Map of name => fallback human-readable name, used when the 'skinname-' message is not + * available + * + * @var array + */ + private $displayNames = []; + + /** + * @deprecated in 1.27 + * @return SkinFactory + */ + public static function getDefaultInstance() { + return MediaWikiServices::getInstance()->getSkinFactory(); + } + + /** + * Register a new Skin factory function. + * + * Will override if it's already registered. + * + * @param string $name Internal skin name. Should be all-lowercase (technically doesn't have + * to be, but doing so would change the case of i18n message keys). + * @param string $displayName For backwards-compatibility with old skin loading system. This is + * the text used as skin's human-readable name when the 'skinname-' message is not + * available. It should be the same as the skin name provided in $wgExtensionCredits. + * @param callable $callback Callback that takes the skin name as an argument + * @throws InvalidArgumentException If an invalid callback is provided + */ + public function register( $name, $displayName, $callback ) { + if ( !is_callable( $callback ) ) { + throw new InvalidArgumentException( 'Invalid callback provided' ); + } + $this->factoryFunctions[$name] = $callback; + $this->displayNames[$name] = $displayName; + } + + /** + * Returns an associative array of: + * skin name => human readable name + * + * @return array + */ + public function getSkinNames() { + return $this->displayNames; + } + + /** + * Create a given Skin using the registered callback for $name. + * @param string $name Name of the skin you want + * @throws SkinException If a factory function isn't registered for $name + * @throws UnexpectedValueException If the factory function returns a non-Skin object + * @return Skin + */ + public function makeSkin( $name ) { + if ( !isset( $this->factoryFunctions[$name] ) ) { + throw new SkinException( "No registered builder available for $name." ); + } + $skin = call_user_func( $this->factoryFunctions[$name], $name ); + if ( $skin instanceof Skin ) { + return $skin; + } else { + throw new UnexpectedValueException( "The builder for $name returned a non-Skin object." ); + } + } +}