summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Connection/ConnectionProviderRef.php
blob: 471f3a358ec7a23e48ab1ded7b34d7db02d3e935 (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
<?php

namespace SMW\Connection;

use RuntimeException;

/**
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class ConnectionProviderRef {

	/**
	 * @var array
	 */
	private $connectionProviders = [];

	/**
	 * @since 3.0
	 *
	 * @param array $connectionProviders
	 */
	public function __construct( array $connectionProviders ) {
		$this->connectionProviders = $connectionProviders;
	}

	/**
	 * @since 3.0
	 *
	 * @param string $key
	 *
	 * @return boolean
	 */
	public function hasConnection( $key ) {
		return isset( $this->connectionProviders[$key] );
	}

	/**
	 * @since 3.0
	 *
	 * @param string $key
	 *
	 * @return ConnectionProvider
	 * @throws RuntimeException
	 */
	public function getConnection( $key ) {

		if ( isset( $this->connectionProviders[$key] ) && $this->connectionProviders[$key] instanceof ConnectionProvider ) {
			return $this->connectionProviders[$key]->getConnection();
		}

		throw new RuntimeException( "$key is unknown" );
	}

	/**
	 * @since 3.0
	 */
	public function releaseConnection() {
		foreach ( $this->connectionProviders as $connectionProvider ) {
			$connectionProvider->releaseConnection();
		}
	}

}