Passed
Branch master (0c09a6)
by Stephan
01:45
created

BootstrapManager::addExternalModule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
/**
3
 * File holding the BootstrapManager class
4
 *
5
 * @copyright (C) 2013-2018, 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 Bootstrap;
27
28
use Bootstrap\Definition\V4ModuleDefinition;
29
use 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
	/**
117
	 * @param string       $filetype 'styles'|'scripts'
118
	 * @param mixed[]      $description
119
	 */
120
	protected function addFilesToGlobalResourceModules ( $filetype, $description ) {
121
122
		if ( isset( $description[ $filetype ] ) ) {
123
124
			$this->adjustArrayElementOfResourceModuleDescription( $filetype, $description[ $filetype ], $filetype );
125
126
		}
127
	}
128
129
	/**
130
	 * Adds core bootstrap modules
131
	 *
132
	 * @since  2.0
133
	 */
134
	public function addCoreBootstrapModules() {
135
		$this->addBootstrapModule( $this->moduleDefinition->get( 'core' ) );
136
	}
137
138
	/**
139
	 * Adds all bootstrap modules
140
	 *
141
	 * @since  1.0
142
	 */
143
	public function addAllBootstrapModules() {
144
		$this->addBootstrapModule( $this->moduleDefinition->get( 'optional' ) );
145
	}
146
147
	/**
148
	 * @since  1.0
149
	 *
150
	 * @param string $path
151
	 * @param string $position
152
	 *
153
	 * @internal param string $path
154
	 */
155
	public function addStyleFile( $path, $position = 'main' ) {
156
		$this->adjustArrayElementOfResourceModuleDescription( 'styles', [ $path => [ 'position' => $position ] ] );
157
	}
158
159
	/**
160
	 * @since  2.0
161
	 *
162
	 * @param string $key   the SCSS variable name
163
	 * @param string $value the value to assign to the variable
164
	 */
165
	public function setScssVariable( $key, $value ) {
166
		$this->setScssVariables( [ $key => $value ] );
167
	}
168
169
	/**
170
	 * @since  2.0
171
	 *
172
	 * @param mixed[] $variables
173
	 */
174
	public function setScssVariables( $variables ) {
175
		$this->adjustArrayElementOfResourceModuleDescription( 'variables', $variables );
176
	}
177
178
	/**
179
	 * @since 1.1
180
	 * @param string|string[] $files
181
	 */
182
	public function addCacheTriggerFile( $files ){
183
		$this->adjustArrayElementOfResourceModuleDescription( 'cacheTriggers', $files );
184
	}
185
186
	/**
187
	 * @param string $key
188
	 * @param mixed  $value
189
	 * @param string $filetype 'styles'|'scripts'
190
	 */
191
	protected function adjustArrayElementOfResourceModuleDescription( $key, $value, $filetype = 'styles' ) {
192
193
		if (!isset($GLOBALS[ 'wgResourceModules' ][ 'ext.bootstrap.' . $filetype ][ $key ])) {
194
			$GLOBALS[ 'wgResourceModules' ][ 'ext.bootstrap.' . $filetype ][ $key ] = [];
195
		}
196
197
		$GLOBALS[ 'wgResourceModules' ][ 'ext.bootstrap.' . $filetype ][ $key ] =
198
			array_merge(
199
				$GLOBALS[ 'wgResourceModules' ][ 'ext.bootstrap.' . $filetype ][ $key ],
200
				(array) $value
201
			);
202
	}
203
}
204