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