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 
84  public function register( $name, $displayName, $spec ) {
85  if ( !is_callable( $spec ) ) {
86  if ( is_array( $spec ) ) {
87  if ( !isset( $spec['args'] ) ) {
88  // make sure name option is set:
89  $spec['args'] = [
90  [ 'name' => $name ]
91  ];
92  }
93  } else {
94  throw new InvalidArgumentException( 'Invalid callback provided' );
95  }
96  }
97  $this->factoryFunctions[$name] = $spec;
98  $this->displayNames[$name] = $displayName;
99  }
100 
107  public function getSkinNames() {
108  return $this->displayNames;
109  }
110 
117  public function makeSkin( $name ) {
118  if ( !isset( $this->factoryFunctions[$name] ) ) {
119  throw new SkinException( "No registered builder available for $name." );
120  }
121 
122  return $this->objectFactory->createObject(
123  $this->factoryFunctions[$name],
124  [
125  'allowCallable' => true,
126  'assertClass' => Skin::class,
127  ]
128  );
129  }
130 
139  public function getAllowedSkins() {
140  $allowedSkins = $this->getSkinNames();
141 
142  // Internal skins not intended for general use
143  unset( $allowedSkins['fallback'] );
144  unset( $allowedSkins['apioutput'] );
145 
146  foreach ( $this->skipSkins as $skip ) {
147  unset( $allowedSkins[$skip] );
148  }
149 
150  return $allowedSkins;
151  }
152 }
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\makeSkin
makeSkin( $name)
Create a given Skin using the registered callback for $name.
Definition: SkinFactory.php:117
SkinFactory\getSkinNames
getSkinNames()
Returns an associative array of: skin name => human readable name.
Definition: SkinFactory.php:107
SkinFactory\__construct
__construct(ObjectFactory $objectFactory, array $skipSkins)
Definition: SkinFactory.php:65
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:139
SkinFactory\$objectFactory
ObjectFactory $objectFactory
Definition: SkinFactory.php:49
SkinException
Exceptions for skin-related failures.
Definition: SkinException.php:29