MediaWiki master
SkinFactory.php
Go to the documentation of this file.
1<?php
2
10namespace MediaWiki\Skin;
11
12use InvalidArgumentException;
13use Wikimedia\ObjectFactory\ObjectFactory;
14
21 private const SKIP_BY_SITECONFIG = 1;
22 private const SKIP_BY_REGISTER = 2;
23
29 private $factoryFunctions = [];
36 private $displayNames = [];
40 private $objectFactory;
41
48 private $skipSkins;
49
56 public function __construct( ObjectFactory $objectFactory, array $skipSkins ) {
57 $this->objectFactory = $objectFactory;
58 $this->skipSkins = array_fill_keys( $skipSkins, self::SKIP_BY_SITECONFIG );
59 }
60
76 public function register( $name, $displayName, $spec, ?bool $skippable = null ) {
77 if ( !is_callable( $spec ) ) {
78 if ( is_array( $spec ) ) {
79 if ( !isset( $spec['args'] ) ) {
80 // make sure name option is set:
81 $spec['args'] = [
82 [ 'name' => $name ]
83 ];
84 }
85 } else {
86 throw new InvalidArgumentException( 'Invalid callback provided' );
87 }
88 }
89 $this->factoryFunctions[$name] = $spec;
90 $this->displayNames[$name] = $displayName;
91
92 // If skipped by site config, leave as-is.
93 if ( ( $this->skipSkins[$name] ?? null ) !== self::SKIP_BY_SITECONFIG ) {
94 if ( $skippable === true ) {
95 $this->skipSkins[$name] = self::SKIP_BY_REGISTER;
96 } else {
97 // Make sure the register() call is unaffected by previous calls.
98 unset( $this->skipSkins[$name] );
99 }
100 }
101 }
102
110 public function makeSkin( $name ) {
111 if ( !isset( $this->factoryFunctions[$name] ) ) {
112 throw new SkinException( "No registered builder available for $name." );
113 }
114
115 return $this->objectFactory->createObject(
116 $this->factoryFunctions[$name],
117 [
118 'allowCallable' => true,
119 'assertClass' => Skin::class,
120 ]
121 );
122 }
123
134 public function getAllowedSkins() {
135 $skins = $this->getInstalledSkins();
136
137 foreach ( $this->skipSkins as $name => $_ ) {
138 unset( $skins[$name] );
139 }
140
141 return $skins;
142 }
143
152 public function getInstalledSkins() {
153 return $this->displayNames;
154 }
155
166 public function getSkinOptions( string $name ): array {
167 $skin = $this->makeSkin( $name );
168 $options = $skin->getOptions();
169 return $options;
170 }
171}
172
174class_alias( SkinFactory::class, 'SkinFactory' );
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.