ResourceLoaderOOUIFileModule.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. * Module which magically loads the right skinScripts and skinStyles for every
  22. * skin, using the specified OOUI theme for each.
  23. *
  24. * @ingroup ResourceLoader
  25. * @internal
  26. */
  27. class ResourceLoaderOOUIFileModule extends ResourceLoaderFileModule {
  28. use ResourceLoaderOOUIModule;
  29. public function __construct( $options = [] ) {
  30. if ( isset( $options['themeScripts'] ) ) {
  31. $skinScripts = $this->getSkinSpecific( $options['themeScripts'], 'scripts' );
  32. if ( !isset( $options['skinScripts'] ) ) {
  33. $options['skinScripts'] = [];
  34. }
  35. $this->extendSkinSpecific( $options['skinScripts'], $skinScripts );
  36. }
  37. if ( isset( $options['themeStyles'] ) ) {
  38. $skinStyles = $this->getSkinSpecific( $options['themeStyles'], 'styles' );
  39. if ( !isset( $options['skinStyles'] ) ) {
  40. $options['skinStyles'] = [];
  41. }
  42. $this->extendSkinSpecific( $options['skinStyles'], $skinStyles );
  43. }
  44. parent::__construct( $options );
  45. }
  46. /**
  47. * Helper function to generate values for 'skinStyles' and 'skinScripts'.
  48. *
  49. * @param string $module Module to generate skinStyles/skinScripts for:
  50. * 'core', 'widgets', 'toolbars', 'windows'
  51. * @param string $which 'scripts' or 'styles'
  52. * @return array
  53. */
  54. private function getSkinSpecific( $module, $which ) {
  55. $themes = self::getSkinThemeMap();
  56. return array_combine(
  57. array_keys( $themes ),
  58. array_map( function ( $theme ) use ( $module, $which ) {
  59. if ( $which === 'scripts' ) {
  60. return $this->getThemeScriptsPath( $theme, $module );
  61. } else {
  62. return $this->getThemeStylesPath( $theme, $module );
  63. }
  64. }, array_values( $themes ) )
  65. );
  66. }
  67. /**
  68. * Prepend the $extraSkinSpecific assoc. array to the $skinSpecific assoc. array.
  69. * Both of them represent a 'skinScripts' or 'skinStyles' definition.
  70. *
  71. * @param array &$skinSpecific
  72. * @param array $extraSkinSpecific
  73. */
  74. private function extendSkinSpecific( &$skinSpecific, $extraSkinSpecific ) {
  75. // For each skin where skinStyles/skinScripts are defined, add our ones at the beginning
  76. foreach ( $skinSpecific as $skin => $files ) {
  77. if ( !is_array( $files ) ) {
  78. $files = [ $files ];
  79. }
  80. if ( isset( $extraSkinSpecific[$skin] ) ) {
  81. $skinSpecific[$skin] = array_merge( [ $extraSkinSpecific[$skin] ], $files );
  82. } elseif ( isset( $extraSkinSpecific['default'] ) ) {
  83. $skinSpecific[$skin] = array_merge( [ $extraSkinSpecific['default'] ], $files );
  84. }
  85. }
  86. // Add our remaining skinStyles/skinScripts for skins that did not have them defined
  87. foreach ( $extraSkinSpecific as $skin => $file ) {
  88. if ( !isset( $skinSpecific[$skin] ) ) {
  89. $skinSpecific[$skin] = $file;
  90. }
  91. }
  92. }
  93. }