Passed
Push — master ( fbd49c...4b461a )
by Stephan
01:44
created

ExtensionRegistryHelper.php (4 issues)

1
<?php
2
/**
3
 * @copyright 2018, 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
	public function __construct() {
27
		if ( version_compare( $GLOBALS[ 'wgVersion' ], '1.27', '<' ) ) {
28
			throw new \Exception( 'ExtensionRegistryHelper requires MediaWiki 1.27 or above.' );
29
		}
30
	}
31
32
33
	/**
34
	 * @return ExtensionRegistryHelper
35
	 * @throws \Exception
36
	 */
37
	public static function singleton() {
38
		if ( !self::$singleton ) {
39
			self::$singleton = new self();
40
		}
41
42
		return self::$singleton;
43
	}
44
45
	/**
46
	 * @param $extensionName
47
	 * @param null $path
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $path is correct as it would always require null to be passed?
Loading history...
48
	 *
49
	 * @throws \Exception
50
	 */
51
	public function loadExtensionRecursive( $extensionName, $path = null ) {
52
		$path = $path ?: $GLOBALS[ 'wgExtensionDirectory' ] . '/' . $extensionName . '/extension.json';
0 ignored issues
show
$path is of type null, thus it always evaluated to false.
Loading history...
53
		$this->loadModuleRecursive( $extensionName, $path );
54
	}
55
56
	/**
57
	 * @param $skinName
58
	 * @param null $path
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $path is correct as it would always require null to be passed?
Loading history...
59
	 *
60
	 * @throws \Exception
61
	 */
62
	public function loadSkinRecursive( $skinName, $path = null ) {
63
		$path = $path ?: $GLOBALS[ 'wgStyleDirectory' ] . '/' . $skinName . '/skin.json';
0 ignored issues
show
$path is of type null, thus it always evaluated to false.
Loading history...
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
			$subregistry->load( $path );
80
81
			try {
82
				$loadedRefl = new \ReflectionProperty( \ExtensionRegistry::class, 'loaded' );
83
				$loadedRefl->setAccessible( true );
84
				$loadedRefl->setValue( $extensionRegistry, $extensionRegistry->getAllThings() + $subregistry->getAllThings() );
85
			} catch ( \Exception $e ) {
86
				throw new \Exception( 'This version of MediaWiki does not support ExtensionRegistryHelper.', 0, $e );
87
			}
88
89
		}
90
	}
91
92
}