summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/StoreFactory.php
blob: e625118502beaf6d0edc1cd6a793ad917638025f (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
<?php

namespace SMW;

use RuntimeException;
use SMW\Exception\StoreNotFoundException;
use Onoi\MessageReporter\NullMessageReporter;
use Psr\Log\NullLogger;

/**
 * Factory method that returns an instance of the default store, or an
 * alternative store instance.
 *
 * @license GNU GPL v2+
 * @since 1.9
 *
 * @author mwjames
 */
class StoreFactory {

	/**
	 * @var array
	 */
	private static $instance = [];

	/**
	 * @since 1.9
	 *
	 * @param string|null $class
	 *
	 * @return Store
	 * @throws RuntimeException
	 * @throws StoreNotFoundException
	 */
	public static function getStore( $class = null ) {

		if ( $class === null ) {
			$class = $GLOBALS['smwgDefaultStore'];
		}

		if ( !isset( self::$instance[$class] ) ) {
			self::$instance[$class] = self::newFromClass( $class );
		}

		return self::$instance[$class];
	}

	/**
	 * @since 1.9
	 */
	public static function clear() {
		self::$instance = [];
	}

	private static function newFromClass( $class ) {

		if ( !class_exists( $class ) ) {
			throw new RuntimeException( "{$class} was not found!" );
		}

		$instance = new $class;

		if ( !( $instance instanceof Store ) ) {
			throw new StoreNotFoundException( "{$class} cannot be used as a store instance!" );
		}

		$instance->setMessageReporter( new NullMessageReporter() );
		$instance->setLogger( new NullLogger() );

		return $instance;
	}

}