MediaWiki  master
SkinFactory.php
Go to the documentation of this file.
1 <?php
2 
24 use Wikimedia\ObjectFactory;
25 
31 class SkinFactory {
32 
38  private $factoryFunctions = [];
45  private $displayNames = [];
49  private $objectFactory;
50 
57  private $skipSkins;
58 
65  public function __construct( ObjectFactory $objectFactory, array $skipSkins ) {
66  $this->objectFactory = $objectFactory;
67  $this->skipSkins = $skipSkins;
68  }
69 
86  public function register( $name, $displayName, $spec, $skippable = false ) {
87  if ( !is_callable( $spec ) ) {
88  if ( is_array( $spec ) ) {
89  if ( !isset( $spec['args'] ) ) {
90  // make sure name option is set:
91  $spec['args'] = [
92  [ 'name' => $name ]
93  ];
94  }
95  } else {
96  throw new InvalidArgumentException( 'Invalid callback provided' );
97  }
98  }
99  $this->factoryFunctions[$name] = $spec;
100  $this->displayNames[$name] = $displayName;
101  // Register as hidden if necessary.
102  if ( $skippable ) {
103  $this->skipSkins[] = $name;
104  }
105  }
106 
113  public function getSkinNames() {
114  return $this->displayNames;
115  }
116 
123  public function makeSkin( $name ) {
124  if ( !isset( $this->factoryFunctions[$name] ) ) {
125  throw new SkinException( "No registered builder available for $name." );
126  }
127 
128  return $this->objectFactory->createObject(
129  $this->factoryFunctions[$name],
130  [
131  'allowCallable' => true,
132  'assertClass' => Skin::class,
133  ]
134  );
135  }
136 
145  public function getAllowedSkins() {
146  $skins = $this->getInstalledSkins();
147 
148  foreach ( $this->skipSkins as $skip ) {
149  unset( $skins[$skip] );
150  }
151 
152  return $skins;
153  }
154 
163  public function getInstalledSkins() {
164  return $this->displayNames;
165  }
166 
174  public function getSkinOptions( string $name ): array {
175  $skin = $this->makeSkin( $name );
176  $options = $skin->getOptions();
177  return $options;
178  }
179 }
SkinFactory\$factoryFunctions
array< string, array|callable > $factoryFunctions
Map of skin name to object factory spec or factory function.
Definition: SkinFactory.php:38
SkinFactory\$displayNames
array $displayNames
Map of name => fallback human-readable name, used when the 'skinname-<skin>' message is not available...
Definition: SkinFactory.php:45
SkinFactory\getSkinOptions
getSkinOptions(string $name)
Return options provided for a given skin name.
Definition: SkinFactory.php:174
SkinFactory\makeSkin
makeSkin( $name)
Create a given Skin using the registered callback for $name.
Definition: SkinFactory.php:123
SkinFactory\getSkinNames
getSkinNames()
Returns an associative array of: skin name => human readable name.
Definition: SkinFactory.php:113
SkinFactory\__construct
__construct(ObjectFactory $objectFactory, array $skipSkins)
Definition: SkinFactory.php:65
SkinFactory\getInstalledSkins
getInstalledSkins()
Fetch the list of all installed skins.
Definition: SkinFactory.php:163
SkinFactory\$skipSkins
string[] $skipSkins
Array of skins that should not be presented in the list of available skins in user preferences,...
Definition: SkinFactory.php:57
SkinFactory
Factory class to create Skin objects.
Definition: SkinFactory.php:31
SkinFactory\getAllowedSkins
getAllowedSkins()
Fetch the list of user-selectable skins in regards to $wgSkipSkins.
Definition: SkinFactory.php:145
SkinFactory\$objectFactory
ObjectFactory $objectFactory
Definition: SkinFactory.php:49
SkinException
Exceptions for skin-related failures.
Definition: SkinException.php:29