|
| 1 | +<?php declare( strict_types = 1 ); |
| 2 | + |
| 3 | +use WP_MySQL_Proxy\MySQL_Proxy; |
| 4 | +use WP_MySQL_Proxy\Adapter\SQLite_Adapter; |
| 5 | + |
| 6 | +require_once __DIR__ . '/../vendor/autoload.php'; |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +define( 'WP_SQLITE_AST_DRIVER', true ); |
| 11 | + |
| 12 | +// Process CLI arguments: |
| 13 | +$shortopts = 'h:d:p'; |
| 14 | +$longopts = array( 'help', 'database:', 'port:' ); |
| 15 | +$opts = getopt( $shortopts, $longopts ); |
| 16 | + |
| 17 | +$help = <<<USAGE |
| 18 | +Usage: php mysql-proxy.php --database <path/to/db.sqlite> [--port <port>] |
| 19 | +
|
| 20 | +Options: |
| 21 | + -h, --help Show this help message and exit. |
| 22 | + -d, --database=<path> The path to the SQLite database file. |
| 23 | + -p, --port=<port> The port to listen on. |
| 24 | +
|
| 25 | +USAGE; |
| 26 | + |
| 27 | +if ( isset( $opts['h'] ) || isset( $opts['help'] ) ) { |
| 28 | + fwrite( STDERR, $help ); |
| 29 | + exit( 0 ); |
| 30 | +} |
| 31 | + |
| 32 | +$db_path = $opts['d'] ?? $opts['database'] ?? null; |
| 33 | +if ( null === $db_path || '' === $db_path ) { |
| 34 | + fwrite( STDERR, "Error: --database <path/to/db.sqlite> is required. Use --help for usage.\n" ); |
| 35 | + exit( 1 ); |
| 36 | +} |
| 37 | + |
| 38 | +$port = (int) ( $opts['p'] ?? $opts['port'] ?? 3306 ); |
| 39 | +if ( $port < 1 || $port > 65535 ) { |
| 40 | + fwrite( STDERR, "Error: --port must be an integer between 1 and 65535.\n" ); |
| 41 | + exit( 1 ); |
| 42 | +} |
| 43 | + |
| 44 | +// Start the MySQL proxy. |
| 45 | +$proxy = new MySQL_Proxy( |
| 46 | + new SQLite_Adapter( $db_path ), |
| 47 | + array( 'port' => $port ) |
| 48 | +); |
| 49 | +$proxy->start(); |
0 commit comments