summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Bootstrap/src/BootstrapManager.php
blob: 7248320e2ad1c0ae021bf3949188feda25bc9513 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php

namespace Bootstrap;

use Bootstrap\Definition\V3ModuleDefinition;
use Bootstrap\Definition\ModuleDefinition;

/**
 * File holding the Bootstrap class
 *
 * @copyright (C) 2013, Stephan Gambke
 * @license   http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License, version 3 (or later)
 *
 * This file is part of the MediaWiki extension Bootstrap.
 * The Bootstrap extension is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * The Bootstrap extension is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @file
 * @ingroup   Bootstrap
 */

/**
 * Class managing the Bootstrap framework.
 */
class BootstrapManager {

	/** @var ModuleDefinition */
	protected $moduleDefinition = null;

	/** @var BootstrapManager */
	private static $instance = null;

	private $mModuleDescriptions;

	/**
	 * @since  1.0
	 *
	 * @param ModuleDefinition $moduleDefinition
	 */
	public function __construct( ModuleDefinition $moduleDefinition ) {
		$this->moduleDefinition = $moduleDefinition;
		$this->initCoreModules();
	}

	/**
	 * Returns the Bootstrap singleton.
	 *
	 * @since  1.0
	 *
	 * @return BootstrapManager
	 */
	public static function getInstance() {

		if ( self::$instance === null ) {
			self::$instance = new self( new V3ModuleDefinition );
		}

		return self::$instance;
	}

	/**
	 * @since  1.0
	 */
	public static function clear() {
		self::$instance = null;
	}

	/**
	 * Adds the given Bootstrap module or modules.
	 *
	 * @since  1.0
	 *
	 * @param string|string[] $modules
	 */
	public function addBootstrapModule( $modules ) {

		$modules = (array) $modules;

		foreach ( $modules as $module ) {

			// if the module is known
			if ( isset( $this->mModuleDescriptions[ $module ] ) ) {

				$description = $this->mModuleDescriptions[ $module ];

				// prevent adding this module again; this also prevents infinite recursion in case
				// of dependency resolution
				unset( $this->mModuleDescriptions[ $module ] );

				// first add any dependencies recursively, so they are available when the styles and
				// scripts of $module are loaded
				if ( isset( $description[ 'dependencies' ] ) ) {
					$this->addBootstrapModule( $description[ 'dependencies' ] );
				}

				$this->addFilesToGlobalResourceModules( 'styles', $description, '.less' );
				$this->addFilesToGlobalResourceModules( 'scripts', $description, '.js' );

			}
		}

	}

	/**
	 * @param string       $filetype 'styles'|'scripts'
	 * @param mixed[]      $description
	 * @param              $fileExt
	 */
	protected function addFilesToGlobalResourceModules ( $filetype, $description, $fileExt ) {

		if ( isset( $description[ $filetype ] ) ) {

			$files = array_map(
				function ( $filename ) use ( $fileExt ) {
					return $filename . $fileExt;
				},
				(array) $description[ $filetype ]
			);

			$this->adjustArrayElementOfResourceModuleDescription( $filetype, $files, $filetype );

		}
	}

	/**
	 * Adds all bootstrap modules
	 *
	 * @since  1.0
	 */
	public function addAllBootstrapModules() {
		$this->addBootstrapModule( $this->moduleDefinition->get( 'optional' ) );
	}

	/**
	 * @since  1.0
	 *
	 * @param string $file
	 * @param string $remotePath
	 *
	 * @internal param string $path
	 */
	public function addExternalModule( $file, $remotePath = '' ) {
		$this->adjustArrayElementOfResourceModuleDescription( 'external styles', array( $file => $remotePath ) );
	}

	/**
	 * @since  1.0
	 *
	 * @param string $key   the LESS variable name
	 * @param string $value the value to assign to the variable
	 */
	public function setLessVariable( $key, $value ) {
		$this->setLessVariables( array( $key => $value ) );
	}

	/**
	 * @since  1.0
	 *
	 * @param mixed[] $variables
	 */
	public function setLessVariables( $variables ) {
		$this->adjustArrayElementOfResourceModuleDescription( 'variables', $variables );
	}

	/**
	 * @since 1.1
	 * @param string|string[] $files
	 */
	public function addCacheTriggerFile( $files ){
		$this->adjustArrayElementOfResourceModuleDescription( 'cachetriggers', $files );
	}

	protected function initCoreModules() {
		$this->mModuleDescriptions = $this->moduleDefinition->get( 'descriptions' );
		$this->addBootstrapModule( $this->moduleDefinition->get( 'core' ) );
	}

	/**
	 * @param string $key
	 * @param mixed  $value
	 * @param string $filetype 'styles'|'scripts'
	 */
	protected function adjustArrayElementOfResourceModuleDescription( $key, $value, $filetype = 'styles' ) {

		if (!isset($GLOBALS[ 'wgResourceModules' ][ 'ext.bootstrap.' . $filetype ][ $key ])) {
			$GLOBALS[ 'wgResourceModules' ][ 'ext.bootstrap.' . $filetype ][ $key ] = $value;
		} else {
			$GLOBALS[ 'wgResourceModules' ][ 'ext.bootstrap.' . $filetype ][ $key ] =
				array_merge(
					$GLOBALS[ 'wgResourceModules' ][ 'ext.bootstrap.' . $filetype ][ $key ],
					(array) $value
				);
		}
	}
}