|
1
|
|
|
<?php |
|
2
|
|
|
namespace Tivnet\WPDB; |
|
3
|
|
|
|
|
4
|
|
|
use Symfony\Component\Console\Helper\FormatterHelper; |
|
5
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Configuration loader and storage. |
|
9
|
|
|
* |
|
10
|
|
|
* @package Tivnet\WPDB |
|
11
|
|
|
*/ |
|
12
|
|
|
class Config { |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* The Application title. |
|
16
|
|
|
* |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
const APPLICATION_TITLE = 'WPDB: WordPress DataBase Manager'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* WPDB configuration filename. |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
const CONFIG_FILE_NAME = '.wpdb.json'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* WordPress configuration filename. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
const WP_CONFIG_FILE_NAME = 'wp-config.php'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The full path to the project root. |
|
37
|
|
|
* |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $path_project_root = '.'; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Array of configuration variables. |
|
44
|
|
|
* |
|
45
|
|
|
* @var string[] |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $config = array(); |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Configuration defaults. |
|
51
|
|
|
* |
|
52
|
|
|
* @var string[] |
|
53
|
|
|
*/ |
|
54
|
|
|
protected static $config_defaults = array( |
|
55
|
|
|
'env' => 'tmp', |
|
56
|
|
|
'dir_dump' => '.wpdb', |
|
57
|
|
|
'dir_web_root' => 'public', |
|
58
|
|
|
'dump_ext' => 'sql', |
|
59
|
|
|
'dump_prefix' => 'wpdb', |
|
60
|
|
|
'cmd_tail' => 'tail -1', |
|
61
|
|
|
'cmd_xz' => 'xz -v', |
|
62
|
|
|
'ext_xz' => 'xz', |
|
63
|
|
|
'dump_ignore' => '', |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* This will be set to true if any error occurred. |
|
68
|
|
|
* |
|
69
|
|
|
* @var bool |
|
70
|
|
|
*/ |
|
71
|
|
|
public $is_error = false; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get a configuration variable value. |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $key The variable name. |
|
77
|
|
|
* |
|
78
|
|
|
* @return string The value. Empty string if not found. |
|
79
|
|
|
*/ |
|
80
|
|
|
public function get( $key ) { |
|
81
|
|
|
return isset( $this->config[ $key ] ) ? $this->config[ $key ] : ''; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Set a configuration variable value. |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $key The variable name. |
|
88
|
|
|
* @param string $value The value. Default is empty string. |
|
89
|
|
|
*/ |
|
90
|
|
|
public function set( $key, $value = '' ) { |
|
91
|
|
|
$this->config[ $key ] = $value; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Config constructor. |
|
97
|
|
|
*/ |
|
98
|
|
|
public function __construct() { |
|
99
|
|
|
$this->path_project_root = getcwd(); |
|
100
|
|
|
|
|
101
|
|
|
$this->config = self::$config_defaults; |
|
102
|
|
|
$this->loadConfig(); |
|
103
|
|
|
$this->loadWPConfig(); |
|
104
|
|
|
|
|
105
|
|
|
$this->set( 'mysql_authorization', '--login-path=' . $this->get( 'DB_NAME' ) ); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Load configuration parameters from JSON file, if exists. |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function loadConfig() { |
|
112
|
|
|
|
|
113
|
|
|
$file_config = implode( '/', array( |
|
114
|
|
|
$this->path_project_root, |
|
115
|
|
|
self::CONFIG_FILE_NAME, |
|
116
|
|
|
) ); |
|
117
|
|
|
|
|
118
|
|
|
if ( is_file( $file_config ) && is_readable( $file_config ) ) { |
|
119
|
|
|
/** @var string[] $config_overwrite */ |
|
120
|
|
|
$config_overwrite = json_decode( file_get_contents( $file_config ), JSON_OBJECT_AS_ARRAY ); |
|
121
|
|
|
if ( is_array( $config_overwrite ) ) { |
|
122
|
|
|
$this->config = array_merge( $this->config, $config_overwrite ); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Read defines from the WordPress configuration file. |
|
129
|
|
|
*/ |
|
130
|
|
|
protected function loadWPConfig() { |
|
131
|
|
|
|
|
132
|
|
|
$file_wp_config = implode( '/', array( |
|
133
|
|
|
$this->path_project_root, |
|
134
|
|
|
$this->get( 'dir_web_root' ), |
|
135
|
|
|
self::WP_CONFIG_FILE_NAME, |
|
136
|
|
|
) ); |
|
137
|
|
|
|
|
138
|
|
|
if ( is_file( $file_wp_config ) && is_readable( $file_wp_config ) ) { |
|
139
|
|
|
$lines = file( $file_wp_config ); |
|
140
|
|
|
$regex_template = '/define\s*\(\s*["\'](DB_NAME|DB_USER|DB_HOST)["\']\s*,\s*["\'](.+)["\']\s*\)\s*;/'; |
|
141
|
|
|
foreach ( $lines as $line ) { |
|
142
|
|
|
if ( preg_match( $regex_template, $line, $matches ) ) { |
|
143
|
|
|
$this->set( $matches[1], $matches[2] ); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
} else { |
|
147
|
|
|
|
|
148
|
|
|
$this->is_error = true; |
|
149
|
|
|
|
|
150
|
|
|
$msg = '[ERROR] WordPress config file not found: ' . $file_wp_config; |
|
151
|
|
|
|
|
152
|
|
|
$output = new ConsoleOutput(); |
|
153
|
|
|
$f = new FormatterHelper(); |
|
154
|
|
|
|
|
155
|
|
|
$output->writeln( array( |
|
156
|
|
|
$f->formatBlock( self::APPLICATION_TITLE, 'info' ), |
|
157
|
|
|
$f->formatBlock( str_repeat( '=', strlen( self::APPLICATION_TITLE ) ), 'info' ), |
|
158
|
|
|
$f->formatBlock( $msg, 'error', true ), |
|
159
|
|
|
) ); |
|
160
|
|
|
|
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|