ExtensionRegistryHelper::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 0
crap 3
1
<?php
2
/**
3
 * @copyright 2018 - 2019, Stephan Gambke
4
 * @license   GNU General Public License, version 3 (or any later version)
5
 *
6
 * @file
7
 */
8
9
namespace ExtensionRegistryHelper;
10
11
12
/**
13
 * Class ExtensionRegistryHelper
14
 *
15
 * @package ExtensionRegistryHelper
16
 */
17
class ExtensionRegistryHelper {
18
19
	private static $singleton;
20
21
	/**
22
	 * ExtensionRegistryHelper constructor.
23
	 *
24
	 * @throws \Exception
25
	 */
26 2
	public function __construct() {
27 2
		if ( ! isset( $GLOBALS[ 'wgVersion' ] ) || version_compare( $GLOBALS[ 'wgVersion' ], '1.27', '<' ) ) {
28 1
			throw new \Exception( 'ExtensionRegistryHelper requires MediaWiki 1.27 or above.' );
29
		}
30 1
	}
31
32
33
	/**
34
	 * @return ExtensionRegistryHelper
35
	 * @throws \Exception
36
	 */
37 2
	public static function singleton() {
38 2
		if ( !self::$singleton ) {
39 2
			self::$singleton = new self();
40
		}
41
42 1
		return self::$singleton;
43
	}
44
45
	/**
46
	 * @param $extensionName
47
	 * @param string|null $path
48
	 *
49
	 * @throws \Exception
50
	 */
51
	public function loadExtensionRecursive( $extensionName, $path = null ) {
52
		$path = $path ?: $GLOBALS[ 'wgExtensionDirectory' ] . '/' . $extensionName . '/extension.json';
53
		$this->loadModuleRecursive( $extensionName, $path );
54
	}
55
56
	/**
57
	 * @param $skinName
58
	 * @param string|null $path
59
	 *
60
	 * @throws \Exception
61
	 */
62
	public function loadSkinRecursive( $skinName, $path = null ) {
63
		$path = $path ?: $GLOBALS[ 'wgStyleDirectory' ] . '/' . $skinName . '/skin.json';
64
		$this->loadModuleRecursive( $skinName, $path );
65
	}
66
67
	/**
68
	 * @param $moduleName
69
	 *
70
	 * @throws \Exception
71
	 */
72
	protected function loadModuleRecursive( $moduleName, $path ) {
73
74
		$extensionRegistry = \ExtensionRegistry::getInstance();
75
76
		if ( !$extensionRegistry->isLoaded( $moduleName ) && !array_key_exists( $path, $extensionRegistry->getQueue() ) ) {
77
78
			$subregistry = new \ExtensionRegistry();
79
80
			$subregistry->queue( $path );
81
			$subregistry->loadFromQueue();
82
83
			try {
84
				$loadedRefl = new \ReflectionProperty( \ExtensionRegistry::class, 'loaded' );
85
				$loadedRefl->setAccessible( true );
86
				$loadedRefl->setValue( $extensionRegistry, $extensionRegistry->getAllThings() + $subregistry->getAllThings() );
87
			} catch ( \Exception $e ) {
88
				throw new \Exception( 'This version of MediaWiki does not support ExtensionRegistryHelper.', 0, $e );
89
			}
90
91
		}
92
	}
93
94
}
95