Completed
Push — master ( c54225...7a9ce5 )
by Gregory
01:43
created

Config::load_wp_config()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
ccs 0
cts 16
cp 0
rs 8.8571
cc 5
eloc 11
nc 4
nop 0
crap 30
1
<?php
2
namespace Tivnet\WPDB;
3
4
/**
5
 * Class Config
6
 * @package Tivnet\WPDB
7
 */
8
class Config {
9
10
	const APPLICATION_TITLE = 'WPDB: WordPress DataBase Manager';
11
12
	/**
13
	 * @var string
14
	 */
15
	const CONFIG_FILE_NAME = '.wpdb.json';
16
17
	/**
18
	 * @var string
19
	 */
20
	const WP_CONFIG_FILE_NAME = 'wp-config.php';
21
22
	/**
23
	 * @var string
24
	 */
25
	protected $path_project_root = '.';
26
27
	/**
28
	 * @var string[]
29
	 */
30
	protected $config = array();
31
32
	/**
33
	 * @var string[]
34
	 */
35
	protected static $config_defaults = array(
36
		'dir_dump'     => 'dbdump-data',
37
		'dir_web_root' => 'public',
38
		'dump_ext'     => 'sql',
39
		'cmd_ls'       => 'ls -o --time-style long-iso',
40
	);
41
42
	/**
43
	 * @param $key
44
	 *
45
	 * @return string
46
	 */
47
	public function get( $key ) {
48
		return isset( $this->config[ $key ] ) ? $this->config[ $key ] : '';
49
	}
50
51
52
	/**
53
	 * Config constructor.
54
	 */
55
	public function __construct() {
56
		$this->path_project_root = getcwd();
57
58
		$this->config = self::$config_defaults;
59
		$this->loadConfig();
60
		$this->loadWPConfig();
61
62
//		print_r( $this->config );
63
	}
64
65
	/**
66
	 * Load configuration parameters from JSON file, if exists.
67
	 */
68
	protected function loadConfig() {
69
70
		$file_config = implode( '/', array(
71
			$this->path_project_root,
72
			self::CONFIG_FILE_NAME,
73
		) );
74
75
		if ( is_file( $file_config ) && is_readable( $file_config ) ) {
76
			/** @var string[] $config_overwrite */
77
			$config_overwrite = json_decode( file_get_contents( $file_config ), JSON_OBJECT_AS_ARRAY );
78
			if ( is_array( $config_overwrite ) ) {
79
				$this->config = array_merge( $this->config, $config_overwrite );
1 ignored issue
show
Documentation Bug introduced by
It seems like array_merge($this->config, $config_overwrite) of type array is incompatible with the declared type array<integer,string> of property $config.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
80
			}
81
		}
82
	}
83
84
	/**
85
	 * Read defines from WP config.
86
	 */
87
	protected function loadWPConfig() {
88
89
		$file_wp_config = implode( '/', array(
90
			$this->path_project_root,
91
			$this->get( 'dir_web_root' ),
92
			self::WP_CONFIG_FILE_NAME,
93
		) );
94
95
		if ( is_file( $file_wp_config ) && is_readable( $file_wp_config ) ) {
96
			$lines          = file( $file_wp_config );
97
			$regex_template = '/define\s*\(\s*["\'](DB_NAME|DB_USER|DB_HOST)["\']\s*,\s*["\'](.+)["\']\s*\)\s*;/';
98
			foreach ( $lines as $line ) {
99
				if ( preg_match( $regex_template, $line, $matches ) ) {
100
					$this->config[ $matches[1] ] = $matches[2];
101
				}
102
			}
103
		}
104
	}
105
106
}
107