Factories
2 years ago
Migrations
2 years ago
Seeds
2 years ago
DbCommand.php
2 years ago
DumpCommand.php
2 years ago
PruneCommand.php
2 years ago
WipeCommand.php
2 years ago
DbCommand.php
167 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP_SCOPED\Illuminate\Database\Console; |
| 4 | |
| 5 | use IAWP_SCOPED\Illuminate\Console\Command; |
| 6 | use IAWP_SCOPED\Illuminate\Support\ConfigurationUrlParser; |
| 7 | use IAWP_SCOPED\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 | (new Process(\array_merge([$this->getCommand($connection)], $this->commandArguments($connection)), null, $this->commandEnvironment($connection)))->setTimeout(null)->setTty(\true)->mustRun(function ($type, $buffer) { |
| 35 | $this->output->write($buffer); |
| 36 | }); |
| 37 | return 0; |
| 38 | } |
| 39 | /** |
| 40 | * Get the database connection configuration. |
| 41 | * |
| 42 | * @return array |
| 43 | * |
| 44 | * @throws \UnexpectedValueException |
| 45 | */ |
| 46 | public function getConnection() |
| 47 | { |
| 48 | $connection = $this->laravel['config']['database.connections.' . (($db = $this->argument('connection')) ?? $this->laravel['config']['database.default'])]; |
| 49 | if (empty($connection)) { |
| 50 | throw new UnexpectedValueException("Invalid database connection [{$db}]."); |
| 51 | } |
| 52 | if (!empty($connection['url'])) { |
| 53 | $connection = (new ConfigurationUrlParser())->parseConfiguration($connection); |
| 54 | } |
| 55 | if ($this->option('read')) { |
| 56 | if (\is_array($connection['read']['host'])) { |
| 57 | $connection['read']['host'] = $connection['read']['host'][0]; |
| 58 | } |
| 59 | $connection = \array_merge($connection, $connection['read']); |
| 60 | } elseif ($this->option('write')) { |
| 61 | if (\is_array($connection['write']['host'])) { |
| 62 | $connection['write']['host'] = $connection['write']['host'][0]; |
| 63 | } |
| 64 | $connection = \array_merge($connection, $connection['write']); |
| 65 | } |
| 66 | return $connection; |
| 67 | } |
| 68 | /** |
| 69 | * Get the arguments for the database client command. |
| 70 | * |
| 71 | * @param array $connection |
| 72 | * @return array |
| 73 | */ |
| 74 | public function commandArguments(array $connection) |
| 75 | { |
| 76 | $driver = \ucfirst($connection['driver']); |
| 77 | return $this->{"get{$driver}Arguments"}($connection); |
| 78 | } |
| 79 | /** |
| 80 | * Get the environment variables for the database client command. |
| 81 | * |
| 82 | * @param array $connection |
| 83 | * @return array|null |
| 84 | */ |
| 85 | public function commandEnvironment(array $connection) |
| 86 | { |
| 87 | $driver = \ucfirst($connection['driver']); |
| 88 | if (\method_exists($this, "get{$driver}Environment")) { |
| 89 | return $this->{"get{$driver}Environment"}($connection); |
| 90 | } |
| 91 | return null; |
| 92 | } |
| 93 | /** |
| 94 | * Get the database client command to run. |
| 95 | * |
| 96 | * @param array $connection |
| 97 | * @return string |
| 98 | */ |
| 99 | public function getCommand(array $connection) |
| 100 | { |
| 101 | return ['mysql' => 'mysql', 'pgsql' => 'psql', 'sqlite' => 'sqlite3', 'sqlsrv' => 'sqlcmd'][$connection['driver']]; |
| 102 | } |
| 103 | /** |
| 104 | * Get the arguments for the MySQL CLI. |
| 105 | * |
| 106 | * @param array $connection |
| 107 | * @return array |
| 108 | */ |
| 109 | protected function getMysqlArguments(array $connection) |
| 110 | { |
| 111 | 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']]); |
| 112 | } |
| 113 | /** |
| 114 | * Get the arguments for the Postgres CLI. |
| 115 | * |
| 116 | * @param array $connection |
| 117 | * @return array |
| 118 | */ |
| 119 | protected function getPgsqlArguments(array $connection) |
| 120 | { |
| 121 | return [$connection['database']]; |
| 122 | } |
| 123 | /** |
| 124 | * Get the arguments for the SQLite CLI. |
| 125 | * |
| 126 | * @param array $connection |
| 127 | * @return array |
| 128 | */ |
| 129 | protected function getSqliteArguments(array $connection) |
| 130 | { |
| 131 | return [$connection['database']]; |
| 132 | } |
| 133 | /** |
| 134 | * Get the arguments for the SQL Server CLI. |
| 135 | * |
| 136 | * @param array $connection |
| 137 | * @return array |
| 138 | */ |
| 139 | protected function getSqlsrvArguments(array $connection) |
| 140 | { |
| 141 | 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)); |
| 142 | } |
| 143 | /** |
| 144 | * Get the environment variables for the Postgres CLI. |
| 145 | * |
| 146 | * @param array $connection |
| 147 | * @return array|null |
| 148 | */ |
| 149 | protected function getPgsqlEnvironment(array $connection) |
| 150 | { |
| 151 | return \array_merge(...$this->getOptionalArguments(['username' => ['PGUSER' => $connection['username']], 'host' => ['PGHOST' => $connection['host']], 'port' => ['PGPORT' => $connection['port']], 'password' => ['PGPASSWORD' => $connection['password']]], $connection)); |
| 152 | } |
| 153 | /** |
| 154 | * Get the optional arguments based on the connection configuration. |
| 155 | * |
| 156 | * @param array $args |
| 157 | * @param array $connection |
| 158 | * @return array |
| 159 | */ |
| 160 | protected function getOptionalArguments(array $args, array $connection) |
| 161 | { |
| 162 | return \array_values(\array_filter($args, function ($key) use($connection) { |
| 163 | return !empty($connection[$key]); |
| 164 | }, \ARRAY_FILTER_USE_KEY)); |
| 165 | } |
| 166 | } |
| 167 |