Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.8] Handle DATABASE_URL env variable for database connection #28096

Closed
wants to merge 1 commit into from
Closed
Changes from all 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
115 changes: 115 additions & 0 deletions src/Illuminate/Database/UrlParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace Illuminate\Database;

use function array_map;
use function parse_str;
use function parse_url;
use function array_merge;
use function preg_replace;
use function array_key_exists;

class UrlParser
{
private const DRIVER_ALIASES = [
'mssql' => 'sqlsrv',
'sqlsrv' => 'sqlsrv',
'mysql' => 'mysql',
'mysql2' => 'mysql', // Amazon RDS, for some weird reason
'postgres' => 'pgsql',
'postgresql' => 'pgsql',
'pgsql' => 'pgsql',
'sqlite' => 'sqlite',
'sqlite3' => 'sqlite',
];
/**
* @var array
*/
private $url;

public function __construct(?string $url)
{
$this->url = $this->getParsedUrl($url);
}

private function getParsedUrl(?string $url)
{
// sqlite3?:///... => sqlite3?://localhost/... or else the URL will be invalid
$url = preg_replace('#^(sqlite3?):///#', '$1://localhost/', $url);

$parsedUrl = parse_url($url);

if ($parsedUrl === false) {
throw new \InvalidArgumentException('Malformed parameter "url".');
}

return array_map('rawurldecode', $parsedUrl);
}

public static function parse(?string $url): array
{
return (new self($url))->parseDatabaseUrl();
}

public function parseDatabaseUrl(): array
{
return array_merge(
$this->getMainAttributes(),
$this->parseDatabaseUrlQuery()
);
}

private function getMainAttributes(): array
{
return [
'driver' => $this->getDriverFromAlias($this->getInUrl('scheme')),
'database' => $this->normalizeDatabaseUrlPath($this->getInUrl('path')),
'host' => $this->getInUrl('host'),
'port' => $this->getInUrl('port'),
'username' => $this->getInUrl('user'),
'password' => $this->getInUrl('pass'),
];
}

private function getDriverFromAlias(?string $alias): ?string
{
if (! $alias) {
return null;
}

if (! array_key_exists($alias, self::DRIVER_ALIASES)) {
throw new \InvalidArgumentException('No driver found with "'.$alias.'" scheme');
}

return self::DRIVER_ALIASES[$alias];
}

private function getInUrl(string $key): ?string
{
return $this->url[$key] ?? null;
}

private function normalizeDatabaseUrlPath(?string $urlPath): ?string
{
if (! $urlPath) {
return null;
}

return trim($urlPath, '/');
}

private function parseDatabaseUrlQuery(): array
{
$queryString = $this->getInUrl('query');

if (! $queryString) {
return [];
}

$query = [];

parse_str($queryString, $query);

return $query;
}
}