ResourceLoaderOOUIFileModule.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. */
  20. /**
  21. * ResourceLoaderFileModule which magically loads the right skinScripts and skinStyles for every
  22. * skin, using the specified OOUI theme for each.
  23. *
  24. * @since 1.30
  25. */
  26. class ResourceLoaderOOUIFileModule extends ResourceLoaderFileModule {
  27. use ResourceLoaderOOUIModule;
  28. public function __construct( $options = [] ) {
  29. if ( isset( $options[ 'themeScripts' ] ) ) {
  30. $skinScripts = $this->getSkinSpecific( $options[ 'themeScripts' ], 'scripts' );
  31. if ( !isset( $options['skinScripts'] ) ) {
  32. $options['skinScripts'] = [];
  33. }
  34. $this->extendSkinSpecific( $options['skinScripts'], $skinScripts );
  35. }
  36. if ( isset( $options[ 'themeStyles' ] ) ) {
  37. $skinStyles = $this->getSkinSpecific( $options[ 'themeStyles' ], 'styles' );
  38. if ( !isset( $options['skinStyles'] ) ) {
  39. $options['skinStyles'] = [];
  40. }
  41. $this->extendSkinSpecific( $options['skinStyles'], $skinStyles );
  42. }
  43. parent::__construct( $options );
  44. }
  45. /**
  46. * Helper function to generate values for 'skinStyles' and 'skinScripts'.
  47. *
  48. * @param string $module Module to generate skinStyles/skinScripts for:
  49. * 'core', 'widgets', 'toolbars', 'windows'
  50. * @param string $which 'scripts' or 'styles'
  51. * @return array
  52. */
  53. private function getSkinSpecific( $module, $which ) {
  54. $themes = self::getSkinThemeMap();
  55. return array_combine(
  56. array_keys( $themes ),
  57. array_map( function ( $theme ) use ( $module, $which ) {
  58. if ( $which === 'scripts' ) {
  59. return $this->getThemeScriptsPath( $theme, $module );
  60. } else {
  61. return $this->getThemeStylesPath( $theme, $module );
  62. }
  63. }, array_values( $themes ) )
  64. );
  65. }
  66. /**
  67. * Prepend the $extraSkinSpecific assoc. array to the $skinSpecific assoc. array.
  68. * Both of them represent a 'skinScripts' or 'skinStyles' definition.
  69. *
  70. * @param array &$skinSpecific
  71. * @param array $extraSkinSpecific
  72. */
  73. private function extendSkinSpecific( &$skinSpecific, $extraSkinSpecific ) {
  74. // For each skin where skinStyles/skinScripts are defined, add our ones at the beginning
  75. foreach ( $skinSpecific as $skin => $files ) {
  76. if ( !is_array( $files ) ) {
  77. $files = [ $files ];
  78. }
  79. if ( isset( $extraSkinSpecific[$skin] ) ) {
  80. $skinSpecific[$skin] = array_merge( [ $extraSkinSpecific[$skin] ], $files );
  81. } elseif ( isset( $extraSkinSpecific['default'] ) ) {
  82. $skinSpecific[$skin] = array_merge( [ $extraSkinSpecific['default'] ], $files );
  83. }
  84. }
  85. // Add our remaining skinStyles/skinScripts for skins that did not have them defined
  86. foreach ( $extraSkinSpecific as $skin => $file ) {
  87. if ( !isset( $skinSpecific[$skin] ) ) {
  88. $skinSpecific[$skin] = $file;
  89. }
  90. }
  91. }
  92. }