Completed
Push — master ( 7a9ce5...aad2c9 )
by Gregory
01:53
created

Config::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 2
eloc 2
nc 2
nop 1
crap 6
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
		'env'          => '',
37
		'dir_dump'     => 'dumps',
38
		'dir_web_root' => 'public',
39
		'dump_ext'     => 'sql',
40
		'dump_prefix'  => 'wpdb',
41
		'cmd_ls'       => 'ls -o --time-style long-iso',
42
		'cmd_tail'     => 'tail -1',
43
		'cmd_xz'       => 'xz -v',
44
		'ext_xz'       => 'xz',
45
		'dump_ignore'  => '',
46
	);
47
48
	/**
49
	 * @param $key
50
	 *
51
	 * @return string
52
	 */
53
	public function get( $key ) {
54
		return isset( $this->config[ $key ] ) ? $this->config[ $key ] : '';
55
	}
56
57
	/**
58
	 * @param string $key
59
	 * @param string $value
60
	 */
61
	public function set( $key, $value = '' ) {
62
		$this->config[ $key ] = $value;
63
	}
64
65
66
	/**
67
	 * Config constructor.
68
	 */
69
	public function __construct() {
70
		$this->path_project_root = getcwd();
71
72
		$this->config = self::$config_defaults;
73
		$this->loadConfig();
74
		$this->loadWPConfig();
75
76
		$this->set( 'mysql_authorization', '--login-path=' . $this->get( 'DB_NAME' ) );
77
78
79
//		print_r( $this->config );
80
	}
81
82
	/**
83
	 * Load configuration parameters from JSON file, if exists.
84
	 */
85
	protected function loadConfig() {
86
87
		$file_config = implode( '/', array(
88
			$this->path_project_root,
89
			self::CONFIG_FILE_NAME,
90
		) );
91
92
		if ( is_file( $file_config ) && is_readable( $file_config ) ) {
93
			/** @var string[] $config_overwrite */
94
			$config_overwrite = json_decode( file_get_contents( $file_config ), JSON_OBJECT_AS_ARRAY );
95
			if ( is_array( $config_overwrite ) ) {
96
				$this->config = array_merge( $this->config, $config_overwrite );
97
			}
98
		}
99
	}
100
101
	/**
102
	 * Read defines from WP config.
103
	 */
104
	protected function loadWPConfig() {
105
106
		$file_wp_config = implode( '/', array(
107
			$this->path_project_root,
108
			$this->get( 'dir_web_root' ),
109
			self::WP_CONFIG_FILE_NAME,
110
		) );
111
112
		if ( is_file( $file_wp_config ) && is_readable( $file_wp_config ) ) {
113
			$lines          = file( $file_wp_config );
114
			$regex_template = '/define\s*\(\s*["\'](DB_NAME|DB_USER|DB_HOST)["\']\s*,\s*["\'](.+)["\']\s*\)\s*;/';
115
			foreach ( $lines as $line ) {
116
				if ( preg_match( $regex_template, $line, $matches ) ) {
117
					$this->set( $matches[1], $matches[2] );
118
				}
119
			}
120
		}
121
	}
122
123
}
124