Skip to content

Commit 05b029e

Browse files
committed
add files
1 parent 4560d28 commit 05b029e

File tree

1 file changed

+190
-0
lines changed

1 file changed

+190
-0
lines changed

Diff for: src/Illuminate/Database/ConfigurationUrlParser.php

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

0 commit comments

Comments
 (0)