MediaWiki master
SkinFactory.php
Go to the documentation of this file.
1<?php
2
24namespace MediaWiki\Skin;
25
26use InvalidArgumentException;
27use Wikimedia\ObjectFactory\ObjectFactory;
28
35 private const SKIP_BY_SITECONFIG = 1;
36 private const SKIP_BY_REGISTER = 2;
37
43 private $factoryFunctions = [];
50 private $displayNames = [];
54 private $objectFactory;
55
62 private $skipSkins;
63
70 public function __construct( ObjectFactory $objectFactory, array $skipSkins ) {
71 $this->objectFactory = $objectFactory;
72 $this->skipSkins = array_fill_keys( $skipSkins, self::SKIP_BY_SITECONFIG );
73 }
74
90 public function register( $name, $displayName, $spec, ?bool $skippable = null ) {
91 if ( !is_callable( $spec ) ) {
92 if ( is_array( $spec ) ) {
93 if ( !isset( $spec['args'] ) ) {
94 // make sure name option is set:
95 $spec['args'] = [
96 [ 'name' => $name ]
97 ];
98 }
99 } else {
100 throw new InvalidArgumentException( 'Invalid callback provided' );
101 }
102 }
103 $this->factoryFunctions[$name] = $spec;
104 $this->displayNames[$name] = $displayName;
105
106 // If skipped by site config, leave as-is.
107 if ( ( $this->skipSkins[$name] ?? null ) !== self::SKIP_BY_SITECONFIG ) {
108 if ( $skippable === true ) {
109 $this->skipSkins[$name] = self::SKIP_BY_REGISTER;
110 } else {
111 // Make sure the register() call is unaffected by previous calls.
112 unset( $this->skipSkins[$name] );
113 }
114 }
115 }
116
123 public function getSkinNames() {
124 wfDeprecated( __METHOD__, '1.37' );
125 return $this->displayNames;
126 }
127
135 public function makeSkin( $name ) {
136 if ( !isset( $this->factoryFunctions[$name] ) ) {
137 throw new SkinException( "No registered builder available for $name." );
138 }
139
140 return $this->objectFactory->createObject(
141 $this->factoryFunctions[$name],
142 [
143 'allowCallable' => true,
144 'assertClass' => Skin::class,
145 ]
146 );
147 }
148
159 public function getAllowedSkins() {
160 $skins = $this->getInstalledSkins();
161
162 foreach ( $this->skipSkins as $name => $_ ) {
163 unset( $skins[$name] );
164 }
165
166 return $skins;
167 }
168
177 public function getInstalledSkins() {
178 return $this->displayNames;
179 }
180
191 public function getSkinOptions( string $name ): array {
192 $skin = $this->makeSkin( $name );
193 $options = $skin->getOptions();
194 return $options;
195 }
196}
197
199class_alias( SkinFactory::class, 'SkinFactory' );
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
Exceptions for skin-related failures.
Factory class to create Skin objects.
getSkinOptions(string $name)
Return options provided for a given skin name.
__construct(ObjectFactory $objectFactory, array $skipSkins)
getInstalledSkins()
Get the list of installed skins.
getAllowedSkins()
Get the list of user-selectable skins.
makeSkin( $name)
Create a given Skin using the registered callback for $name.
getSkinNames()
Return an associative array of skin name => human readable name.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...