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

namespace SMW\Elastic;

use SMW\Options;
use RuntimeException;

/**
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class Config extends Options {

	/**
	 * @since 3.0
	 *
	 * @param string $data
	 */
	public function loadFromJSON( $data ) {

		if ( $data === false ) {
			return;
		}

		$data = json_decode( $data, true );
		$merge = true;

		if ( ( $error = json_last_error() ) !== JSON_ERROR_NONE ) {
			throw new RuntimeException( 'JSON returned with a "' . json_last_error_msg() . '"' );
		}

		foreach ( $data as $key => $value ) {

			if ( $merge && isset( $this->options[$key] ) && is_array( $value ) && is_array( $this->options[$key] ) ) {
				$value = array_merge( $this->options[$key], $value );
			}

			$this->options[$key] = $value;
		}
	}

	/**
	 * @since 3.0
	 *
	 * @param string $file
	 *
	 * @return string|false
	 * @throws RuntimeException
	 */
	public function readFile( $file ) {

		if ( $file === false ) {
			return false;
		}

		$file = str_replace( [ '\\', '/' ], DIRECTORY_SEPARATOR, realpath( $file ) );

		if ( is_readable( $file ) ) {
			return file_get_contents( $file );
		}

		throw new RuntimeException( "$file is inaccessible!" );
	}

}