Factories
2 weeks ago
Migrations
2 weeks ago
Seeds
2 weeks ago
DatabaseInspectionCommand.php
2 weeks ago
DbCommand.php
2 weeks ago
DumpCommand.php
2 weeks ago
MonitorCommand.php
2 weeks ago
PruneCommand.php
2 weeks ago
ShowCommand.php
2 weeks ago
TableCommand.php
2 weeks ago
WipeCommand.php
2 weeks ago
DbCommand.php
173 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWPSCOPED\Illuminate\Database\Console; |
| 4 | |
| 5 | use IAWPSCOPED\Illuminate\Console\Command; |
| 6 | use IAWPSCOPED\Illuminate\Support\ConfigurationUrlParser; |
| 7 | use IAWPSCOPED\Symfony\Component\Process\Process; |
| 8 | use UnexpectedValueException; |
| 9 | /** @internal */ |
| 10 | class DbCommand extends Command |
| 11 | { |
| 12 | /** |
| 13 | * The name and signature of the console command. |
| 14 | * |
| 15 | * @var string |
| 16 | */ |
| 17 | protected $signature = 'db {connection? : The database connection that should be used} |
| 18 | {--read : Connect to the read connection} |
| 19 | {--write : Connect to the write connection}'; |
| 20 | /** |
| 21 | * The console command description. |
| 22 | * |
| 23 | * @var string |
| 24 | */ |
| 25 | protected $description = 'Start a new database CLI session'; |
| 26 | /** |
| 27 | * Execute the console command. |
| 28 | * |
| 29 | * @return int |
| 30 | */ |
| 31 | public function handle() |
| 32 | { |
| 33 | $connection = $this->getConnection(); |
| 34 | if (!isset($connection['host']) && $connection['driver'] !== 'sqlite') { |
| 35 | $this->components->error('No host specified for this database connection.'); |
| 36 | $this->line(' Use the <options=bold>[--read]</> and <options=bold>[--write]</> options to specify a read or write connection.'); |
| 37 | $this->newLine(); |
| 38 | return Command::FAILURE; |
| 39 | } |
| 40 | (new Process(\array_merge([$this->getCommand($connection)], $this->commandArguments($connection)), null, $this->commandEnvironment($connection)))->setTimeout(null)->setTty(\true)->mustRun(function ($type, $buffer) { |
| 41 | $this->output->write($buffer); |
| 42 | }); |
| 43 | return 0; |
| 44 | } |
| 45 | /** |
| 46 | * Get the database connection configuration. |
| 47 | * |
| 48 | * @return array |
| 49 | * |
| 50 | * @throws \UnexpectedValueException |
| 51 | */ |
| 52 | public function getConnection() |
| 53 | { |
| 54 | $connection = $this->laravel['config']['database.connections.' . (($db = $this->argument('connection')) ?? $this->laravel['config']['database.default'])]; |
| 55 | if (empty($connection)) { |
| 56 | throw new UnexpectedValueException("Invalid database connection [{$db}]."); |
| 57 | } |
| 58 | if (!empty($connection['url'])) { |
| 59 | $connection = (new ConfigurationUrlParser())->parseConfiguration($connection); |
| 60 | } |
| 61 | if ($this->option('read')) { |
| 62 | if (\is_array($connection['read']['host'])) { |
| 63 | $connection['read']['host'] = $connection['read']['host'][0]; |
| 64 | } |
| 65 | $connection = \array_merge($connection, $connection['read']); |
| 66 | } elseif ($this->option('write')) { |
| 67 | if (\is_array($connection['write']['host'])) { |
| 68 | $connection['write']['host'] = $connection['write']['host'][0]; |
| 69 | } |
| 70 | $connection = \array_merge($connection, $connection['write']); |
| 71 | } |
| 72 | return $connection; |
| 73 | } |
| 74 | /** |
| 75 | * Get the arguments for the database client command. |
| 76 | * |
| 77 | * @param array $connection |
| 78 | * @return array |
| 79 | */ |
| 80 | public function commandArguments(array $connection) |
| 81 | { |
| 82 | $driver = \ucfirst($connection['driver']); |
| 83 | return $this->{"get{$driver}Arguments"}($connection); |
| 84 | } |
| 85 | /** |
| 86 | * Get the environment variables for the database client command. |
| 87 | * |
| 88 | * @param array $connection |
| 89 | * @return array|null |
| 90 | */ |
| 91 | public function commandEnvironment(array $connection) |
| 92 | { |
| 93 | $driver = \ucfirst($connection['driver']); |
| 94 | if (\method_exists($this, "get{$driver}Environment")) { |
| 95 | return $this->{"get{$driver}Environment"}($connection); |
| 96 | } |
| 97 | return null; |
| 98 | } |
| 99 | /** |
| 100 | * Get the database client command to run. |
| 101 | * |
| 102 | * @param array $connection |
| 103 | * @return string |
| 104 | */ |
| 105 | public function getCommand(array $connection) |
| 106 | { |
| 107 | return ['mysql' => 'mysql', 'pgsql' => 'psql', 'sqlite' => 'sqlite3', 'sqlsrv' => 'sqlcmd'][$connection['driver']]; |
| 108 | } |
| 109 | /** |
| 110 | * Get the arguments for the MySQL CLI. |
| 111 | * |
| 112 | * @param array $connection |
| 113 | * @return array |
| 114 | */ |
| 115 | protected function getMysqlArguments(array $connection) |
| 116 | { |
| 117 | return \array_merge(['--host=' . $connection['host'], '--port=' . $connection['port'], '--user=' . $connection['username']], $this->getOptionalArguments(['password' => '--password=' . $connection['password'], 'unix_socket' => '--socket=' . ($connection['unix_socket'] ?? ''), 'charset' => '--default-character-set=' . ($connection['charset'] ?? '')], $connection), [$connection['database']]); |
| 118 | } |
| 119 | /** |
| 120 | * Get the arguments for the Postgres CLI. |
| 121 | * |
| 122 | * @param array $connection |
| 123 | * @return array |
| 124 | */ |
| 125 | protected function getPgsqlArguments(array $connection) |
| 126 | { |
| 127 | return [$connection['database']]; |
| 128 | } |
| 129 | /** |
| 130 | * Get the arguments for the SQLite CLI. |
| 131 | * |
| 132 | * @param array $connection |
| 133 | * @return array |
| 134 | */ |
| 135 | protected function getSqliteArguments(array $connection) |
| 136 | { |
| 137 | return [$connection['database']]; |
| 138 | } |
| 139 | /** |
| 140 | * Get the arguments for the SQL Server CLI. |
| 141 | * |
| 142 | * @param array $connection |
| 143 | * @return array |
| 144 | */ |
| 145 | protected function getSqlsrvArguments(array $connection) |
| 146 | { |
| 147 | return \array_merge(...$this->getOptionalArguments(['database' => ['-d', $connection['database']], 'username' => ['-U', $connection['username']], 'password' => ['-P', $connection['password']], 'host' => ['-S', 'tcp:' . $connection['host'] . ($connection['port'] ? ',' . $connection['port'] : '')]], $connection)); |
| 148 | } |
| 149 | /** |
| 150 | * Get the environment variables for the Postgres CLI. |
| 151 | * |
| 152 | * @param array $connection |
| 153 | * @return array|null |
| 154 | */ |
| 155 | protected function getPgsqlEnvironment(array $connection) |
| 156 | { |
| 157 | return \array_merge(...$this->getOptionalArguments(['username' => ['PGUSER' => $connection['username']], 'host' => ['PGHOST' => $connection['host']], 'port' => ['PGPORT' => $connection['port']], 'password' => ['PGPASSWORD' => $connection['password']]], $connection)); |
| 158 | } |
| 159 | /** |
| 160 | * Get the optional arguments based on the connection configuration. |
| 161 | * |
| 162 | * @param array $args |
| 163 | * @param array $connection |
| 164 | * @return array |
| 165 | */ |
| 166 | protected function getOptionalArguments(array $args, array $connection) |
| 167 | { |
| 168 | return \array_values(\array_filter($args, function ($key) use($connection) { |
| 169 | return !empty($connection[$key]); |
| 170 | }, \ARRAY_FILTER_USE_KEY)); |
| 171 | } |
| 172 | } |
| 173 |