-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathwp-mysql-proxy.php
More file actions
56 lines (45 loc) · 1.62 KB
/
wp-mysql-proxy.php
File metadata and controls
56 lines (45 loc) · 1.62 KB
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
<?php declare( strict_types = 1 );
use WP_MySQL_Proxy\MySQL_Proxy;
use WP_MySQL_Proxy\Adapter\SQLite_Adapter;
use WP_MySQL_Proxy\Logger;
require_once __DIR__ . '/../vendor/autoload.php';
// Process CLI arguments:
$shortopts = 'h:d:p:l:';
$longopts = array( 'help', 'database:', 'port:', 'log-level:' );
$opts = getopt( $shortopts, $longopts );
$help = <<<USAGE
Usage: php bin/wp-mysql-proxy.php [--port <port>] [--database <path/to/db.sqlite>] [--log-level <log_level>]
Options:
-h, --help Show this help message and exit.
-p, --port=<port> The port to listen on. Default: 3306
-d, --database=<path> The path to the SQLite database file. Default: :memory:
-l, --log-level=<level> The log level to use. One of 'error', 'warning', 'info', 'debug'. Default: info
USAGE;
// Help.
if ( isset( $opts['h'] ) || isset( $opts['help'] ) ) {
fwrite( STDERR, $help );
exit( 0 );
}
// Database path.
$db_path = $opts['d'] ?? $opts['database'] ?? ':memory:';
// Port.
$port = (int) ( $opts['p'] ?? $opts['port'] ?? 3306 );
if ( $port < 1 || $port > 65535 ) {
fwrite( STDERR, "Error: --port must be an integer between 1 and 65535. Use --help for more information.\n" );
exit( 1 );
}
// Log level.
$log_level = $opts['l'] ?? $opts['log-level'] ?? 'info';
if ( ! in_array( $log_level, Logger::LEVELS, true ) ) {
fwrite( STDERR, 'Error: --log-level must be one of: ' . implode( ', ', Logger::LEVELS ) . ". Use --help for more information.\n" );
exit( 1 );
}
// Start the MySQL proxy.
$proxy = new MySQL_Proxy(
new SQLite_Adapter( $db_path ),
array(
'port' => $port,
'log_level' => $log_level,
)
);
$proxy->start();