SkinFactory.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. /**
  23. * Factory class to create Skin objects
  24. *
  25. * @since 1.24
  26. */
  27. class SkinFactory {
  28. /**
  29. * Map of name => callback
  30. * @var array
  31. */
  32. private $factoryFunctions = [];
  33. /**
  34. * Map of name => fallback human-readable name, used when the 'skinname-<skin>' message is not
  35. * available
  36. *
  37. * @var array
  38. */
  39. private $displayNames = [];
  40. /**
  41. * Register a new Skin factory function.
  42. *
  43. * Will override if it's already registered.
  44. *
  45. * @param string $name Internal skin name. Should be all-lowercase (technically doesn't have
  46. * to be, but doing so would change the case of i18n message keys).
  47. * @param string $displayName For backwards-compatibility with old skin loading system. This is
  48. * the text used as skin's human-readable name when the 'skinname-<skin>' message is not
  49. * available. It should be the same as the skin name provided in $wgExtensionCredits.
  50. * @param callable $callback Callback that takes the skin name as an argument
  51. * @throws InvalidArgumentException If an invalid callback is provided
  52. */
  53. public function register( $name, $displayName, $callback ) {
  54. if ( !is_callable( $callback ) ) {
  55. throw new InvalidArgumentException( 'Invalid callback provided' );
  56. }
  57. $this->factoryFunctions[$name] = $callback;
  58. $this->displayNames[$name] = $displayName;
  59. }
  60. /**
  61. * Returns an associative array of:
  62. * skin name => human readable name
  63. *
  64. * @return array
  65. */
  66. public function getSkinNames() {
  67. return $this->displayNames;
  68. }
  69. /**
  70. * Create a given Skin using the registered callback for $name.
  71. * @param string $name Name of the skin you want
  72. * @throws SkinException If a factory function isn't registered for $name
  73. * @throws UnexpectedValueException If the factory function returns a non-Skin object
  74. * @return Skin
  75. */
  76. public function makeSkin( $name ) {
  77. if ( !isset( $this->factoryFunctions[$name] ) ) {
  78. throw new SkinException( "No registered builder available for $name." );
  79. }
  80. $skin = call_user_func( $this->factoryFunctions[$name], $name );
  81. if ( $skin instanceof Skin ) {
  82. return $skin;
  83. } else {
  84. throw new UnexpectedValueException( "The builder for $name returned a non-Skin object." );
  85. }
  86. }
  87. }