BootstrapManager   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 18
eloc 35
c 4
b 0
f 1
dl 0
loc 162
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A addCacheTriggerFile() 0 2 1
A setScssVariables() 0 2 1
A addStyleFile() 0 3 1
A addBootstrapModule() 0 22 4
A adjustArrayElementOfResourceModuleDescription() 0 10 2
A setScssVariable() 0 2 1
A addCoreBootstrapModules() 0 2 1
A addFilesToGlobalResourceModules() 0 5 2
A getInstance() 0 6 2
A addAllBootstrapModules() 0 2 1
A clear() 0 2 1
1
<?php
2
/**
3
 * File holding the BootstrapManager class
4
 *
5
 * @copyright 2013 - 2019, Stephan Gambke
6
 * @license   GPL-3.0-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\ModuleDefinition;
29
use Bootstrap\Definition\V4ModuleDefinition;
30
31
/**
32
 * Class managing the Bootstrap framework.
33
 *
34
 * @since 1.0
35
 * @ingroup   Bootstrap
36
 */
37
class BootstrapManager {
38
39
	/** @var ModuleDefinition|null */
40
	protected $moduleDefinition = null;
41
42
	/** @var BootstrapManager|null */
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
		if ( self::$instance === null ) {
66
			self::$instance = new self( new V4ModuleDefinition );
67
		}
68
69
		return self::$instance;
70
	}
71
72
	/**
73
	 * @since  1.0
74
	 */
75
	public static function clear() {
76
		self::$instance = null;
77
	}
78
79
	/**
80
	 * Adds the given Bootstrap module or modules.
81
	 *
82
	 * @since  1.0
83
	 *
84
	 * @param string|string[] $modules
85
	 */
86
	public function addBootstrapModule( $modules ) {
87
		$modules = (array)$modules;
88
89
		foreach ( $modules as $module ) {
90
91
			// if the module is known
92
			if ( isset( $this->moduleDescriptions[ $module ] ) ) {
93
94
				$description = $this->moduleDescriptions[ $module ];
95
96
				// prevent adding this module again; this also prevents infinite recursion in case
97
				// of dependency resolution
98
				unset( $this->moduleDescriptions[ $module ] );
99
100
				// first add any dependencies recursively, so they are available when the styles and
101
				// scripts of $module are loaded
102
				if ( isset( $description[ 'dependencies' ] ) ) {
103
					$this->addBootstrapModule( $description[ 'dependencies' ] );
104
				}
105
106
				$this->addFilesToGlobalResourceModules( 'styles', $description );
107
				$this->addFilesToGlobalResourceModules( 'scripts', $description );
108
109
			}
110
		}
111
	}
112
113
	/**
114
	 * @param string $filetype 'styles'|'scripts'
115
	 * @param mixed[] $description
116
	 */
117
	protected function addFilesToGlobalResourceModules( $filetype, $description ) {
118
		if ( isset( $description[ $filetype ] ) ) {
119
120
			$this->adjustArrayElementOfResourceModuleDescription( $filetype,
121
				$description[ $filetype ], $filetype );
122
123
		}
124
	}
125
126
	/**
127
	 * Adds core bootstrap modules
128
	 *
129
	 * @since  4.0
130
	 */
131
	public function addCoreBootstrapModules() {
132
		$this->addBootstrapModule( $this->moduleDefinition->get( 'core' ) );
0 ignored issues
show
Bug introduced by
The method get() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

132
		$this->addBootstrapModule( $this->moduleDefinition->/** @scrutinizer ignore-call */ get( 'core' ) );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
133
	}
134
135
	/**
136
	 * Adds all bootstrap modules
137
	 *
138
	 * @since  1.0
139
	 */
140
	public function addAllBootstrapModules() {
141
		$this->addBootstrapModule( $this->moduleDefinition->get( 'optional' ) );
142
	}
143
144
	/**
145
	 * @since  1.0
146
	 *
147
	 * @param string $path
148
	 * @param string $position
149
	 *
150
	 * @internal param string $path
151
	 */
152
	public function addStyleFile( $path, $position = 'main' ) {
153
		$this->adjustArrayElementOfResourceModuleDescription( 'styles',
154
			[ $path => [ 'position' => $position ] ] );
155
	}
156
157
	/**
158
	 * @since  4.0
159
	 *
160
	 * @param string $key the SCSS variable name
161
	 * @param string $value the value to assign to the variable
162
	 */
163
	public function setScssVariable( $key, $value ) {
164
		$this->setScssVariables( [ $key => $value ] );
165
	}
166
167
	/**
168
	 * @since  4.0
169
	 *
170
	 * @param mixed[] $variables
171
	 */
172
	public function setScssVariables( $variables ) {
173
		$this->adjustArrayElementOfResourceModuleDescription( 'variables', $variables );
174
	}
175
176
	/**
177
	 * @since 1.1
178
	 * @param string|string[] $files
179
	 */
180
	public function addCacheTriggerFile( $files ) {
181
		$this->adjustArrayElementOfResourceModuleDescription( 'cacheTriggers', $files );
182
	}
183
184
	/**
185
	 * @param string $key
186
	 * @param mixed $value
187
	 * @param string $filetype 'styles'|'scripts'
188
	 */
189
	protected function adjustArrayElementOfResourceModuleDescription( $key, $value,
190
		$filetype = 'styles' ) {
191
		if ( !isset( $GLOBALS[ 'wgResourceModules' ][ 'ext.bootstrap.' . $filetype ][ $key ] ) ) {
192
			$GLOBALS[ 'wgResourceModules' ][ 'ext.bootstrap.' . $filetype ][ $key ] = [];
193
		}
194
195
		$GLOBALS[ 'wgResourceModules' ][ 'ext.bootstrap.' . $filetype ][ $key ] =
196
			array_merge(
197
				$GLOBALS[ 'wgResourceModules' ][ 'ext.bootstrap.' . $filetype ][ $key ],
198
				(array)$value
199
			);
200
	}
201
}
202