MediaWiki REL1_35
SkinFactory.php
Go to the documentation of this file.
1<?php
2
24use Wikimedia\ObjectFactory;
25
32
38 private $factoryFunctions = [];
45 private $displayNames = [];
50
56 public function __construct( ObjectFactory $objectFactory ) {
57 $this->objectFactory = $objectFactory;
58 }
59
74 public function register( $name, $displayName, $spec ) {
75 if ( !is_callable( $spec ) ) {
76 if ( is_array( $spec ) ) {
77 if ( !isset( $spec['args'] ) ) {
78 // make sure name option is set:
79 $spec['args'] = [
80 [ 'name' => $name ]
81 ];
82 }
83 } else {
84 throw new InvalidArgumentException( 'Invalid callback provided' );
85 }
86 }
87 $this->factoryFunctions[$name] = $spec;
88 $this->displayNames[$name] = $displayName;
89 }
90
97 public function getSkinNames() {
98 return $this->displayNames;
99 }
100
107 public function makeSkin( $name ) {
108 if ( !isset( $this->factoryFunctions[$name] ) ) {
109 throw new SkinException( "No registered builder available for $name." );
110 }
111
112 return $this->objectFactory->createObject(
113 $this->factoryFunctions[$name],
114 [
115 'allowCallable' => true,
116 'assertClass' => Skin::class,
117 ]
118 );
119 }
120}
Exceptions for skin-related failures.
Factory class to create Skin objects.
array< string, array|callable > $factoryFunctions
Map of skin name to object factory spec or factory function.
makeSkin( $name)
Create a given Skin using the registered callback for $name.
__construct(ObjectFactory $objectFactory)
array $displayNames
Map of name => fallback human-readable name, used when the 'skinname-<skin>' message is not available...
ObjectFactory $objectFactory
getSkinNames()
Returns an associative array of: skin name => human readable name.