Skip to content
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 146 additions & 1 deletion src/Illuminate/Database/Console/DbCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use Illuminate\Console\Command;
use Illuminate\Support\ConfigurationUrlParser;
use PDO;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\ExecutableFinder;
use Symfony\Component\Process\Process;
use UnexpectedValueException;

Expand All @@ -19,7 +21,8 @@ class DbCommand extends Command
*/
protected $signature = 'db {connection? : The database connection that should be used}
{--read : Connect to the read connection}
{--write : Connect to the write connection}';
{--write : Connect to the write connection}
{--open : Open the connection URL in a GUI client}';

/**
* The console command description.
Expand All @@ -45,6 +48,10 @@ public function handle()
return Command::FAILURE;
}

if ($this->option('open')) {
return $this->openDatabaseUrl($connection);
}

try {
(new Process(
array_merge([$command = $this->getCommand($connection)], $this->commandArguments($connection)),
Expand Down Expand Up @@ -254,4 +261,142 @@ protected function getOptionalArguments(array $args, array $connection)
return ! empty($connection[$key]);
}, ARRAY_FILTER_USE_KEY));
}

/**
* Open the database connection URL in a GUI client.
*
* @param array $connection
* @return int
*/
protected function openDatabaseUrl(array $connection)
{
$this->open($this->buildDatabaseUrl($connection));

return Command::SUCCESS;
}

/**
* Build the database connection URL.
*
* @param array $connection
* @return string
*/
protected function buildDatabaseUrl(array $connection)
{
$driver = $this->getDriverScheme($connection['driver']);

if ($connection['driver'] === 'sqlite') {
return $connection['database'];
}

$url = "{$driver}://";

if (! empty($connection['username'])) {
$url .= urlencode($connection['username']);

if (! empty($connection['password'])) {
$url .= ':'.urlencode($connection['password']);
}

$url .= '@';
}

$url .= $connection['host'];

if (! empty($connection['port'])) {
$url .= ":{$connection['port']}";
}

if (! empty($connection['database'])) {
$url .= '/'.urlencode($connection['database']);
}

$queryParams = $this->getQueryParameters($connection);
if (! empty($queryParams)) {
$url .= '?'.http_build_query($queryParams);
}

return $url;
}

/**
* Get the URL scheme for the database driver.
*
* @param string $driver
* @return string
*/
protected function getDriverScheme($driver)
{
return match ($driver) {
'mysql', 'mariadb' => 'mysql',
'pgsql' => 'postgresql',
'sqlite' => 'sqlite',
'sqlsrv' => 'sqlserver',
default => $driver,
};
}

/**
* Get additional query parameters for the URL.
*
* @param array $connection
* @return array
*/
protected function getQueryParameters(array $connection)
{
$params = [];

// Add SSL/TLS parameters if configured
if (! empty($connection['sslmode'])) {
$params['sslmode'] = $connection['sslmode'];
}

if (! empty($connection['options'])) {
// For PostgreSQL SSL mode
if (isset($connection['options'][PDO::MYSQL_ATTR_SSL_CA])) {
$params['ssl'] = 'true';
}
}

return $params;
}

/**
* Open the database URL.
*
* @param string $url
* @return void
*/
protected function open($url)
{
if (PHP_OS_FAMILY === 'Windows') {
$process = Process::fromShellCommandline(escapeshellcmd("start {$url}"));
$process->run();

if (! $process->isSuccessful()) {
throw new ProcessFailedException($process);
}

return;
}

$binary = collect(match (PHP_OS_FAMILY) {
'Darwin' => ['open'],
'Linux' => ['xdg-open', 'wslview'],
})->first(fn ($binary) => (new ExecutableFinder)->find($binary) !== null);

if ($binary === null) {
$this->components->warn('Unable to open the URL on your system. You will need to open it yourself.');
$this->components->info("Database URL: {$url}");

return;
}

$process = Process::fromShellCommandline(escapeshellcmd("{$binary} {$url}"));
$process->run();

if (! $process->isSuccessful()) {
throw new ProcessFailedException($process);
}
}
}
44 changes: 44 additions & 0 deletions src/Illuminate/Database/Console/DbOpenCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Illuminate\Database\Console;

use Illuminate\Console\Command;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'db:open')]
class DbOpenCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'db:open {connection? : The database connection that should be used}
{--read : Connect to the read connection}
{--write : Connect to the write connection}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Open the database connection in a GUI client';

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
// This is an alias for 'db --open'
$arguments = array_filter([
'connection' => $this->argument('connection'),
'--read' => $this->option('read'),
'--write' => $this->option('write'),
'--open' => true,
]);

return $this->call('db', $arguments);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Illuminate\Console\Signals;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Database\Console\DbCommand;
use Illuminate\Database\Console\DbOpenCommand;
use Illuminate\Database\Console\DumpCommand;
use Illuminate\Database\Console\Factories\FactoryMakeCommand;
use Illuminate\Database\Console\MonitorCommand as DatabaseMonitorCommand;
Expand Down Expand Up @@ -129,6 +130,7 @@ class ArtisanServiceProvider extends ServiceProvider implements DeferrableProvid
'ConfigClear' => ConfigClearCommand::class,
'ConfigShow' => ConfigShowCommand::class,
'Db' => DbCommand::class,
'DbOpen' => DbOpenCommand::class,
'DbMonitor' => DatabaseMonitorCommand::class,
'DbPrune' => PruneCommand::class,
'DbShow' => ShowCommand::class,
Expand Down
Loading
Loading