Skip to content

Commit 8c2342b

Browse files
committed
DRAFT: Update ConfigurationUrlParser to use Uri\Rfc3986\Uri
1 parent 3b5aba9 commit 8c2342b

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

src/Illuminate/Support/ConfigurationUrlParser.php

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Support;
44

55
use InvalidArgumentException;
6+
use Uri\Rfc3986\Uri;
67

78
class ConfigurationUrlParser
89
{
@@ -39,46 +40,44 @@ public function parseConfiguration($config)
3940
return $config;
4041
}
4142

42-
$rawComponents = $this->parseUrl($url);
43-
44-
$decodedComponents = $this->parseStringsToNativeTypes(
45-
array_map(rawurldecode(...), $rawComponents)
46-
);
43+
$parsedUrl = $this->parseUrl($url);
4744

4845
return array_merge(
4946
$config,
50-
$this->getPrimaryOptions($decodedComponents),
51-
$this->getQueryOptions($rawComponents)
47+
$this->getPrimaryOptions($parsedUrl),
48+
$this->getQueryOptions($parsedUrl)
5249
);
5350
}
5451

5552
/**
5653
* Get the primary database connection options.
5754
*
58-
* @param array $url
55+
* @param Uri $url
5956
* @return array
6057
*/
6158
protected function getPrimaryOptions($url)
6259
{
63-
return array_filter([
60+
$options = array_filter([
6461
'driver' => $this->getDriver($url),
6562
'database' => $this->getDatabase($url),
66-
'host' => $url['host'] ?? null,
67-
'port' => $url['port'] ?? null,
68-
'username' => $url['user'] ?? null,
69-
'password' => $url['pass'] ?? null,
70-
], fn ($value) => ! is_null($value));
63+
'host' => ($host = $url->getHost()) === 'null' ? null : $host,
64+
'port' => $url->getPort(),
65+
'username' => $url->getUsername(),
66+
'password' => $url->getPassword(),
67+
], fn ($value) => $value !== null && $value !== '');
68+
69+
return array_map(rawurldecode(...), $options);
7170
}
7271

7372
/**
7473
* Get the database driver from the URL.
7574
*
76-
* @param array $url
75+
* @param Uri $url
7776
* @return string|null
7877
*/
7978
protected function getDriver($url)
8079
{
81-
$alias = $url['scheme'] ?? null;
80+
$alias = $url->getScheme();
8281

8382
if (! $alias) {
8483
return;
@@ -90,25 +89,25 @@ protected function getDriver($url)
9089
/**
9190
* Get the database name from the URL.
9291
*
93-
* @param array $url
92+
* @param Uri $url
9493
* @return string|null
9594
*/
9695
protected function getDatabase($url)
9796
{
98-
$path = $url['path'] ?? null;
97+
$path = $url->getPath();
9998

10099
return $path && $path !== '/' ? substr($path, 1) : null;
101100
}
102101

103102
/**
104103
* Get all of the additional database options from the query string.
105104
*
106-
* @param array $url
105+
* @param Uri $url
107106
* @return array
108107
*/
109108
protected function getQueryOptions($url)
110109
{
111-
$queryString = $url['query'] ?? null;
110+
$queryString = $url->getQuery();
112111

113112
if (! $queryString) {
114113
return [];
@@ -125,17 +124,17 @@ protected function getQueryOptions($url)
125124
* Parse the string URL to an array of components.
126125
*
127126
* @param string $url
128-
* @return array
127+
* @return Uri
129128
*
130129
* @throws \InvalidArgumentException
131130
*/
132131
protected function parseUrl($url)
133132
{
134133
$url = preg_replace('#^(sqlite3?):///#', '$1://null/', $url);
135134

136-
$parsedUrl = parse_url($url);
135+
$parsedUrl = Uri::parse($url);
137136

138-
if ($parsedUrl === false) {
137+
if ($parsedUrl === null) {
139138
throw new InvalidArgumentException('The database configuration URL is malformed.');
140139
}
141140

0 commit comments

Comments
 (0)