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 
56  public function __construct( ObjectFactory $objectFactory ) {
57  $this->objectFactory = $objectFactory;
58  }
59 
74  public function register( $name, $displayName, $spec ) {
75  if ( !is_array( $spec ) && !is_callable( $spec ) ) {
76  throw new InvalidArgumentException( 'Invalid callback provided' );
77  }
78  $this->factoryFunctions[$name] = $spec;
79  $this->displayNames[$name] = $displayName;
80  }
81 
88  public function getSkinNames() {
89  return $this->displayNames;
90  }
91 
98  public function makeSkin( $name ) {
99  if ( !isset( $this->factoryFunctions[$name] ) ) {
100  throw new SkinException( "No registered builder available for $name." );
101  }
102 
103  return $this->objectFactory->createObject(
104  $this->factoryFunctions[$name],
105  [
106  'allowCallable' => true,
107  'assertClass' => Skin::class,
108  'extraArgs' => [ $name ]
109  ]
110  );
111  }
112 }
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:98
SkinFactory\getSkinNames
getSkinNames()
Returns an associative array of: skin name => human readable name.
Definition: SkinFactory.php:88
SkinFactory
Factory class to create Skin objects.
Definition: SkinFactory.php:31
SkinFactory\$objectFactory
ObjectFactory $objectFactory
Definition: SkinFactory.php:49
SkinException
Exceptions for skin-related failures.
Definition: SkinException.php:28
SkinFactory\__construct
__construct(ObjectFactory $objectFactory)
Definition: SkinFactory.php:56