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 return $this->displayNames;
122 }
123
131 public function makeSkin( $name ) {
132 if ( !isset( $this->factoryFunctions[$name] ) ) {
133 throw new SkinException( "No registered builder available for $name." );
134 }
135
136 return $this->objectFactory->createObject(
137 $this->factoryFunctions[$name],
138 [
139 'allowCallable' => true,
140 'assertClass' => Skin::class,
141 ]
142 );
143 }
144
155 public function getAllowedSkins() {
156 $skins = $this->getInstalledSkins();
157
158 foreach ( $this->skipSkins as $name => $_ ) {
159 unset( $skins[$name] );
160 }
161
162 return $skins;
163 }
164
173 public function getInstalledSkins() {
174 return $this->displayNames;
175 }
176
187 public function getSkinOptions( string $name ): array {
188 $skin = $this->makeSkin( $name );
189 $options = $skin->getOptions();
190 return $options;
191 }
192}
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)