|
10 | 10 | */ |
11 | 11 | namespace WebFiori\Framework\Cli\Commands; |
12 | 12 |
|
| 13 | +use WebFiori\Cli\Argument; |
13 | 14 | use WebFiori\Cli\Command; |
14 | 15 | use WebFiori\Database\ConnectionInfo; |
15 | 16 | use WebFiori\Database\DatabaseException; |
|
24 | 25 | */ |
25 | 26 | class AddDbConnectionCommand extends Command { |
26 | 27 | public function __construct() { |
27 | | - parent::__construct('add:db-connection', [], 'Add a database connection.'); |
| 28 | + parent::__construct('add:db-connection', [ |
| 29 | + new Argument('--db-type', 'The type of the database server. Supported values: '.implode(', ', ConnectionInfo::SUPPORTED_DATABASES).'.', true), |
| 30 | + new Argument('--host', 'The address of the database host.', true), |
| 31 | + new Argument('--port', 'Port number of the database server.', true), |
| 32 | + new Argument('--user', 'The username to use when connecting to the database.', true), |
| 33 | + new Argument('--password', 'The password to use when connecting to the database.', true), |
| 34 | + new Argument('--database', 'The name of the database to connect to.', true), |
| 35 | + new Argument('--name', 'A friendly name to identify the connection.', true), |
| 36 | + new Argument('--extras', 'A JSON string of key-value pairs with extra connection information.', true), |
| 37 | + new Argument('--no-check', 'If provided, the connection will be added without the attempt to check if provided credentials are valid.', true), |
| 38 | + ], 'Add a database connection.'); |
28 | 39 | } |
29 | 40 | /** |
30 | 41 | * Execute the command. |
31 | 42 | * |
32 | 43 | * @return int |
33 | 44 | */ |
34 | 45 | public function exec() : int { |
35 | | - $dbType = $this->select('Select database type:', ConnectionInfo::SUPPORTED_DATABASES); |
| 46 | + $dbTypeArg = $this->getArgValue('--db-type'); |
| 47 | + $supportedDbs = ConnectionInfo::SUPPORTED_DATABASES; |
36 | 48 |
|
37 | | - $connInfoObj = new ConnectionInfo('mysql', 'root', 'pass', 'ok'); |
| 49 | + if ($dbTypeArg !== null && in_array($dbTypeArg, $supportedDbs)) { |
| 50 | + $dbType = $dbTypeArg; |
| 51 | + } else { |
| 52 | + if ($dbTypeArg !== null && !in_array($dbTypeArg, $supportedDbs)) { |
| 53 | + $this->warning("Database not supported: $dbTypeArg"); |
| 54 | + } |
| 55 | + $dbType = $this->select('Select database type:', $supportedDbs); |
| 56 | + } |
| 57 | + |
| 58 | + $connInfoObj = new ConnectionInfo($dbType, 'root', 'pass', 'ok'); |
| 59 | + |
| 60 | + $hostArg = $this->getArgValue('--host'); |
| 61 | + $connInfoObj->setHost($hostArg !== null ? $hostArg : $this->getInput('Database host:', '127.0.0.1')); |
| 62 | + |
| 63 | + $portArg = $this->getArgValue('--port'); |
| 64 | + $connInfoObj->setPort($portArg !== null ? (int) $portArg : $this->getInput('Port number:', 3306)); |
| 65 | + |
| 66 | + $userArg = $this->getArgValue('--user'); |
| 67 | + $connInfoObj->setUsername($userArg !== null ? $userArg : $this->getInput('Username:')); |
| 68 | + |
| 69 | + $passArg = $this->getArgValue('--password'); |
| 70 | + $connInfoObj->setPassword($passArg !== null ? $passArg : $this->getMaskedInput('Password:')); |
| 71 | + |
| 72 | + $dbArg = $this->getArgValue('--database'); |
| 73 | + $connInfoObj->setDBName($dbArg !== null ? $dbArg : $this->getInput('Database name:')); |
| 74 | + |
| 75 | + $defaultName = 'db-connection-'.(count(App::getConfig()->getDBConnections()) + 1); |
| 76 | + $nameArg = $this->getArgValue('--name'); |
| 77 | + $connInfoObj->setName($nameArg !== null ? $nameArg : $this->getInput('Give your connection a friendly name:', $defaultName)); |
38 | 78 |
|
39 | | - if ($dbType == 'mssql') { |
40 | | - $connInfoObj = new ConnectionInfo('mssql', 'root', 'pass', 'ok'); |
| 79 | + $extrasArg = $this->getArgValue('--extras'); |
| 80 | + |
| 81 | + if ($extrasArg !== null) { |
| 82 | + $decoded = json_decode($extrasArg, true); |
| 83 | + |
| 84 | + if (is_array($decoded)) { |
| 85 | + $connInfoObj->setExtras($decoded); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if ($this->isArgProvided('--no-check')) { |
| 90 | + App::getConfig()->addOrUpdateDBConnection($connInfoObj); |
| 91 | + $this->success('Connection information was stored in application configuration.'); |
| 92 | + |
| 93 | + return 0; |
41 | 94 | } |
42 | 95 |
|
43 | | - $connInfoObj->setHost($this->getInput('Database host:', '127.0.0.1')); |
44 | | - $connInfoObj->setPort($this->getInput('Port number:', 3306)); |
45 | | - $connInfoObj->setUsername($this->getInput('Username:')); |
46 | | - $connInfoObj->setPassword($this->getMaskedInput('Password:')); |
47 | | - $connInfoObj->setDBName($this->getInput('Database name:')); |
48 | | - $connInfoObj->setName($this->getInput('Give your connection a friendly name:', 'db-connection-'.(count(App::getConfig()->getDBConnections()) + 1))); |
49 | 96 | $this->println('Trying to connect to the database...'); |
50 | 97 |
|
51 | 98 | $addConnection = $this->tryConnect($connInfoObj); |
52 | 99 | $orgHost = $connInfoObj->getHost(); |
53 | 100 | $orgErr = $addConnection !== true ? $addConnection->getMessage() : ''; |
54 | | - |
| 101 | + |
55 | 102 | if ($addConnection !== true) { |
56 | 103 | if ($connInfoObj->getHost() == '127.0.0.1') { |
57 | 104 | $this->println("Trying with 'localhost'..."); |
|
0 commit comments