SkinFactory.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Copyright 2014
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. use MediaWiki\MediaWikiServices;
  23. /**
  24. * Factory class to create Skin objects
  25. *
  26. * @since 1.24
  27. */
  28. class SkinFactory {
  29. /**
  30. * Map of name => callback
  31. * @var array
  32. */
  33. private $factoryFunctions = [];
  34. /**
  35. * Map of name => fallback human-readable name, used when the 'skinname-<skin>' message is not
  36. * available
  37. *
  38. * @var array
  39. */
  40. private $displayNames = [];
  41. /**
  42. * @deprecated in 1.27
  43. * @return SkinFactory
  44. */
  45. public static function getDefaultInstance() {
  46. return MediaWikiServices::getInstance()->getSkinFactory();
  47. }
  48. /**
  49. * Register a new Skin factory function.
  50. *
  51. * Will override if it's already registered.
  52. *
  53. * @param string $name Internal skin name. Should be all-lowercase (technically doesn't have
  54. * to be, but doing so would change the case of i18n message keys).
  55. * @param string $displayName For backwards-compatibility with old skin loading system. This is
  56. * the text used as skin's human-readable name when the 'skinname-<skin>' message is not
  57. * available. It should be the same as the skin name provided in $wgExtensionCredits.
  58. * @param callable $callback Callback that takes the skin name as an argument
  59. * @throws InvalidArgumentException If an invalid callback is provided
  60. */
  61. public function register( $name, $displayName, $callback ) {
  62. if ( !is_callable( $callback ) ) {
  63. throw new InvalidArgumentException( 'Invalid callback provided' );
  64. }
  65. $this->factoryFunctions[$name] = $callback;
  66. $this->displayNames[$name] = $displayName;
  67. }
  68. /**
  69. * Returns an associative array of:
  70. * skin name => human readable name
  71. *
  72. * @return array
  73. */
  74. public function getSkinNames() {
  75. return $this->displayNames;
  76. }
  77. /**
  78. * Create a given Skin using the registered callback for $name.
  79. * @param string $name Name of the skin you want
  80. * @throws SkinException If a factory function isn't registered for $name
  81. * @throws UnexpectedValueException If the factory function returns a non-Skin object
  82. * @return Skin
  83. */
  84. public function makeSkin( $name ) {
  85. if ( !isset( $this->factoryFunctions[$name] ) ) {
  86. throw new SkinException( "No registered builder available for $name." );
  87. }
  88. $skin = call_user_func( $this->factoryFunctions[$name], $name );
  89. if ( $skin instanceof Skin ) {
  90. return $skin;
  91. } else {
  92. throw new UnexpectedValueException( "The builder for $name returned a non-Skin object." );
  93. }
  94. }
  95. }