ConfigurationUrlParser.php
4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
namespace Illuminate\Support;
use InvalidArgumentException;
class ConfigurationUrlParser
{
/**
* The drivers aliases map.
*
* @var array
*/
protected static $driverAliases = [
'mssql' => 'sqlsrv',
'mysql2' => 'mysql', // RDS
'postgres' => 'pgsql',
'postgresql' => 'pgsql',
'sqlite3' => 'sqlite',
'redis' => 'tcp',
'rediss' => 'tls',
];
/**
* Parse the database configuration, hydrating options using a database configuration URL if possible.
*
* @param array|string $config
* @return array
*/
public function parseConfiguration($config)
{
if (is_string($config)) {
$config = ['url' => $config];
}
$url = Arr::pull($config, 'url');
if (! $url) {
return $config;
}
$rawComponents = $this->parseUrl($url);
$decodedComponents = $this->parseStringsToNativeTypes(
array_map('rawurldecode', $rawComponents)
);
return array_merge(
$config,
$this->getPrimaryOptions($decodedComponents),
$this->getQueryOptions($rawComponents)
);
}
/**
* Get the primary database connection options.
*
* @param array $url
* @return array
*/
protected function getPrimaryOptions($url)
{
return array_filter([
'driver' => $this->getDriver($url),
'database' => $this->getDatabase($url),
'host' => $url['host'] ?? null,
'port' => $url['port'] ?? null,
'username' => $url['user'] ?? null,
'password' => $url['pass'] ?? null,
], function ($value) {
return ! is_null($value);
});
}
/**
* Get the database driver from the URL.
*
* @param array $url
* @return string|null
*/
protected function getDriver($url)
{
$alias = $url['scheme'] ?? null;
if (! $alias) {
return;
}
return static::$driverAliases[$alias] ?? $alias;
}
/**
* Get the database name from the URL.
*
* @param array $url
* @return string|null
*/
protected function getDatabase($url)
{
$path = $url['path'] ?? null;
return $path && $path !== '/' ? substr($path, 1) : null;
}
/**
* Get all of the additional database options from the query string.
*
* @param array $url
* @return array
*/
protected function getQueryOptions($url)
{
$queryString = $url['query'] ?? null;
if (! $queryString) {
return [];
}
$query = [];
parse_str($queryString, $query);
return $this->parseStringsToNativeTypes($query);
}
/**
* Parse the string URL to an array of components.
*
* @param string $url
* @return array
*
* @throws \InvalidArgumentException
*/
protected function parseUrl($url)
{
$url = preg_replace('#^(sqlite3?):///#', '$1://null/', $url);
$parsedUrl = parse_url($url);
if ($parsedUrl === false) {
throw new InvalidArgumentException('The database configuration URL is malformed.');
}
return $parsedUrl;
}
/**
* Convert string casted values to their native types.
*
* @param mixed $value
* @return mixed
*/
protected function parseStringsToNativeTypes($value)
{
if (is_array($value)) {
return array_map([$this, 'parseStringsToNativeTypes'], $value);
}
if (! is_string($value)) {
return $value;
}
$parsedValue = json_decode($value, true);
if (json_last_error() === JSON_ERROR_NONE) {
return $parsedValue;
}
return $value;
}
/**
* Get all of the current drivers' aliases.
*
* @return array
*/
public static function getDriverAliases()
{
return static::$driverAliases;
}
/**
* Add the given driver alias to the driver aliases array.
*
* @param string $alias
* @param string $driver
* @return void
*/
public static function addDriverAlias($alias, $driver)
{
static::$driverAliases[$alias] = $driver;
}
}