MediaWiki master
SkinFactory.php
Go to the documentation of this file.
1<?php
2
24use Wikimedia\ObjectFactory\ObjectFactory;
25
32 private const SKIP_BY_SITECONFIG = 1;
33 private const SKIP_BY_REGISTER = 2;
34
40 private $factoryFunctions = [];
47 private $displayNames = [];
51 private $objectFactory;
52
59 private $skipSkins;
60
67 public function __construct( ObjectFactory $objectFactory, array $skipSkins ) {
68 $this->objectFactory = $objectFactory;
69 $this->skipSkins = array_fill_keys( $skipSkins, self::SKIP_BY_SITECONFIG );
70 }
71
87 public function register( $name, $displayName, $spec, ?bool $skippable = null ) {
88 if ( !is_callable( $spec ) ) {
89 if ( is_array( $spec ) ) {
90 if ( !isset( $spec['args'] ) ) {
91 // make sure name option is set:
92 $spec['args'] = [
93 [ 'name' => $name ]
94 ];
95 }
96 } else {
97 throw new InvalidArgumentException( 'Invalid callback provided' );
98 }
99 }
100 $this->factoryFunctions[$name] = $spec;
101 $this->displayNames[$name] = $displayName;
102
103 // If skipped by site config, leave as-is.
104 if ( ( $this->skipSkins[$name] ?? null ) !== self::SKIP_BY_SITECONFIG ) {
105 if ( $skippable === true ) {
106 $this->skipSkins[$name] = self::SKIP_BY_REGISTER;
107 } else {
108 // Make sure the register() call is unaffected by previous calls.
109 unset( $this->skipSkins[$name] );
110 }
111 }
112 }
113
120 public function getSkinNames() {
121 wfDeprecated( __METHOD__, '1.37' );
122 return $this->displayNames;
123 }
124
132 public function makeSkin( $name ) {
133 if ( !isset( $this->factoryFunctions[$name] ) ) {
134 throw new SkinException( "No registered builder available for $name." );
135 }
136
137 return $this->objectFactory->createObject(
138 $this->factoryFunctions[$name],
139 [
140 'allowCallable' => true,
141 'assertClass' => Skin::class,
142 ]
143 );
144 }
145
156 public function getAllowedSkins() {
157 $skins = $this->getInstalledSkins();
158
159 foreach ( $this->skipSkins as $name => $_ ) {
160 unset( $skins[$name] );
161 }
162
163 return $skins;
164 }
165
174 public function getInstalledSkins() {
175 return $this->displayNames;
176 }
177
188 public function getSkinOptions( string $name ): array {
189 $skin = $this->makeSkin( $name );
190 $options = $skin->getOptions();
191 return $options;
192 }
193}
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.
makeSkin( $name)
Create a given Skin using the registered callback for $name.
getInstalledSkins()
Get the list of installed skins.
getSkinNames()
Return an associative array of skin name => human readable name.
getSkinOptions(string $name)
Return options provided for a given skin name.
getAllowedSkins()
Get the list of user-selectable skins.
__construct(ObjectFactory $objectFactory, array $skipSkins)