Passed
Pull Request — master (#23)
by
unknown
08:31
created

BootstrapManager::addAllBootstrapModules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * File holding the BootstrapManager class
4
 *
5
 * @copyright 2013 - 2019, Stephan Gambke
6
 * @license   http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License, version 3 (or later)
7
 *
8
 * This file is part of the MediaWiki extension Bootstrap.
9
 * The Bootstrap extension is free software: you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * The Bootstrap extension is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 * @file
23
 * @ingroup   Bootstrap
24
 */
25
26
namespace MediaWiki\Extensions\Bootstrap;
27
28
use MediaWiki\Extensions\Bootstrap\Definition\V4ModuleDefinition;
29
use MediaWiki\Extensions\Bootstrap\Definition\ModuleDefinition;
30
31
/**
32
 * Class managing the Bootstrap framework.
33
 *
34
 * @since 1.0
35
 * @ingroup   Bootstrap
36
 */
37
class BootstrapManager {
38
39
	/** @var ModuleDefinition */
40
	protected $moduleDefinition = null;
41
42
	/** @var BootstrapManager */
43
	private static $instance = null;
44
45
	private $moduleDescriptions;
46
47
	/**
48
	 * @since  1.0
49
	 *
50
	 * @param ModuleDefinition $moduleDefinition
51
	 */
52
	public function __construct( ModuleDefinition $moduleDefinition ) {
53
		$this->moduleDefinition = $moduleDefinition;
54
		$this->moduleDescriptions = $this->moduleDefinition->get( 'descriptions' );
55
	}
56
57
	/**
58
	 * Returns the Bootstrap singleton.
59
	 *
60
	 * @since  1.0
61
	 *
62
	 * @return BootstrapManager
63
	 */
64
	public static function getInstance() {
65
66
		if ( self::$instance === null ) {
67
			self::$instance = new self( new V4ModuleDefinition );
68
		}
69
70
		return self::$instance;
71
	}
72
73
	/**
74
	 * @since  1.0
75
	 */
76
	public static function clear() {
77
		self::$instance = null;
78
	}
79
80
	/**
81
	 * Adds the given Bootstrap module or modules.
82
	 *
83
	 * @since  1.0
84
	 *
85
	 * @param string|string[] $modules
86
	 */
87
	public function addBootstrapModule( $modules ) {
88
89
		$modules = (array) $modules;
90
91
		foreach ( $modules as $module ) {
92
93
			// if the module is known
94
			if ( isset( $this->moduleDescriptions[ $module ] ) ) {
95
96
				$description = $this->moduleDescriptions[ $module ];
97
98
				// prevent adding this module again; this also prevents infinite recursion in case
99
				// of dependency resolution
100
				unset( $this->moduleDescriptions[ $module ] );
101
102
				// first add any dependencies recursively, so they are available when the styles and
103
				// scripts of $module are loaded
104
				if ( isset( $description[ 'dependencies' ] ) ) {
105
					$this->addBootstrapModule( $description[ 'dependencies' ] );
106
				}
107
108
				$this->addFilesToGlobalResourceModules( 'styles', $description );
109
				$this->addFilesToGlobalResourceModules( 'scripts', $description );
110
111
			}
112
		}
113
	}
114
115
	/**
116
	 * @param string       $filetype 'styles'|'scripts'
117
	 * @param mixed[]      $description
118
	 */
119
	protected function addFilesToGlobalResourceModules ( $filetype, $description ) {
120
121
		if ( isset( $description[ $filetype ] ) ) {
122
123
			$this->adjustArrayElementOfResourceModuleDescription( $filetype, $description[ $filetype ], $filetype );
124
125
		}
126
	}
127
128
	/**
129
	 * Adds core bootstrap modules
130
	 *
131
	 * @since  4.0
132
	 */
133
	public function addCoreBootstrapModules() {
134
		$this->addBootstrapModule( $this->moduleDefinition->get( 'core' ) );
135
	}
136
137
	/**
138
	 * Adds all bootstrap modules
139
	 *
140
	 * @since  1.0
141
	 */
142
	public function addAllBootstrapModules() {
143
		$this->addBootstrapModule( $this->moduleDefinition->get( 'optional' ) );
144
	}
145
146
	/**
147
	 * @since  1.0
148
	 *
149
	 * @param string $path
150
	 * @param string $position
151
	 *
152
	 * @internal param string $path
153
	 */
154
	public function addStyleFile( $path, $position = 'main' ) {
155
		$this->adjustArrayElementOfResourceModuleDescription( 'styles', [ $path => [ 'position' => $position ] ] );
156
	}
157
158
	/**
159
	 * @since  4.0
160
	 *
161
	 * @param string $key   the SCSS variable name
162
	 * @param string $value the value to assign to the variable
163
	 */
164
	public function setScssVariable( $key, $value ) {
165
		$this->setScssVariables( [ $key => $value ] );
166
	}
167
168
	/**
169
	 * @since  4.0
170
	 *
171
	 * @param mixed[] $variables
172
	 */
173
	public function setScssVariables( $variables ) {
174
		$this->adjustArrayElementOfResourceModuleDescription( 'variables', $variables );
175
	}
176
177
	/**
178
	 * @since 1.1
179
	 * @param string|string[] $files
180
	 */
181
	public function addCacheTriggerFile( $files ){
182
		$this->adjustArrayElementOfResourceModuleDescription( 'cacheTriggers', $files );
183
	}
184
185
	/**
186
	 * @param string $key
187
	 * @param mixed  $value
188
	 * @param string $filetype 'styles'|'scripts'
189
	 */
190
	protected function adjustArrayElementOfResourceModuleDescription( $key, $value, $filetype = 'styles' ) {
191
192
		if (!isset($GLOBALS[ 'wgResourceModules' ][ 'ext.bootstrap.' . $filetype ][ $key ])) {
193
			$GLOBALS[ 'wgResourceModules' ][ 'ext.bootstrap.' . $filetype ][ $key ] = [];
194
		}
195
196
		$GLOBALS[ 'wgResourceModules' ][ 'ext.bootstrap.' . $filetype ][ $key ] =
197
			array_merge(
198
				$GLOBALS[ 'wgResourceModules' ][ 'ext.bootstrap.' . $filetype ][ $key ],
199
				(array) $value
200
			);
201
	}
202
}
203