|
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->load_config(); |
|
60
|
|
|
$this->load_wp_config(); |
|
61
|
|
|
|
|
62
|
|
|
// print_r( $this->config ); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Load configuration parameters from JSON file, if exists. |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function load_config() { |
|
|
|
|
|
|
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
|
|
|
$config_overwrite = json_decode( file_get_contents( $file_config ), JSON_OBJECT_AS_ARRAY ); |
|
77
|
|
|
if ( is_array( $config_overwrite ) ) { |
|
78
|
|
|
$this->config = array_merge( $this->config, $config_overwrite ); |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Read defines from WP config. |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function load_wp_config() { |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
$file_wp_config = implode( '/', array( |
|
89
|
|
|
$this->path_project_root, |
|
90
|
|
|
$this->get( 'dir_web_root' ), |
|
91
|
|
|
self::WP_CONFIG_FILE_NAME, |
|
92
|
|
|
) ); |
|
93
|
|
|
|
|
94
|
|
|
if ( is_file( $file_wp_config ) && is_readable( $file_wp_config ) ) { |
|
95
|
|
|
$lines = file( $file_wp_config ); |
|
96
|
|
|
$regex_template = '/define\s*\(\s*["\'](DB_NAME|DB_USER|DB_HOST)["\']\s*,\s*["\'](.+)["\']\s*\)\s*;/'; |
|
97
|
|
|
foreach ( $lines as $line ) { |
|
98
|
|
|
if ( preg_match( $regex_template, $line, $matches ) ) { |
|
99
|
|
|
$this->config[ $matches[1] ] = $matches[2]; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|