-
Notifications
You must be signed in to change notification settings - Fork 11.9k
[5.8] Handle database urls for database connections. #28308
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
Merged
+599
−12
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0283436
Add Database Url Parser with tests.
mathieutu c91d6a5
Parse Database Url when creating connection.
mathieutu ba8d1f4
Apply styleCI.
mathieutu f7dc3f4
Add some more tests.
mathieutu fb73271
Merge remote-tracking branch 'origin/5.8' into features/database_url
mathieutu 95d7e82
Apply @driesvints suggestions from code review.
driesvints 1b568d0
Add docblocs and change self to static.
mathieutu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| <?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 Illuminate\Support\Arr; | ||
| use InvalidArgumentException; | ||
|
|
||
| class UrlParser | ||
| { | ||
| /** | ||
| * The drivers aliases map. | ||
| * | ||
| * @var array | ||
| */ | ||
| protected static $driverAliases = [ | ||
| 'mssql' => 'sqlsrv', | ||
| 'mysql2' => 'mysql', // Amazon RDS, for some weird reason | ||
| 'postgres' => 'pgsql', | ||
| 'postgresql' => 'pgsql', | ||
| 'sqlite3' => 'sqlite', | ||
| ]; | ||
|
|
||
| /** | ||
| * The different components of parsed url. | ||
| * | ||
| * @var array | ||
| */ | ||
| protected $parsedUrl; | ||
|
|
||
| /** | ||
| * Get all of the current drivers aliases. | ||
| * | ||
| * @return array | ||
| */ | ||
| public static function getDriverAliases(): array | ||
| { | ||
| return static::$driverAliases; | ||
| } | ||
|
|
||
| /** | ||
| * Add the 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; | ||
| } | ||
|
|
||
| /** | ||
| * Transform the url string or config array with url key to a parsed classic config array. | ||
| * | ||
|
driesvints marked this conversation as resolved.
|
||
| * @param array|string $config | ||
| * @return array | ||
| */ | ||
| public function parseDatabaseConfigWithUrl($config): array | ||
| { | ||
| if (is_string($config)) { | ||
| $config = ['url' => $config]; | ||
| } | ||
|
|
||
| $url = $config['url'] ?? null; | ||
| $config = Arr::except($config, 'url'); | ||
|
|
||
| if (! $url) { | ||
| return $config; | ||
| } | ||
|
|
||
| $this->parsedUrl = $this->parseUrl($url); | ||
|
|
||
| return array_merge( | ||
| $config, | ||
| $this->getMainAttributes(), | ||
| $this->getOtherOptions() | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Decode the string url, to an array of all of its components. | ||
| * | ||
| * @param string $url | ||
| * @return array | ||
| */ | ||
| protected function parseUrl($url): array | ||
| { | ||
| // sqlite3?:///... => sqlite3?://null/... or else the URL will be invalid | ||
| $url = preg_replace('#^(sqlite3?):///#', '$1://null/', $url); | ||
|
|
||
| $parsedUrl = parse_url($url); | ||
|
|
||
| if ($parsedUrl === false) { | ||
| throw new InvalidArgumentException('Malformed parameter "url".'); | ||
| } | ||
|
|
||
| return $this->parseStringsToNativeTypes(array_map('rawurldecode', $parsedUrl)); | ||
| } | ||
|
|
||
| /** | ||
| * Convert string casted values to there native types. | ||
| * Ex: 'false' => false, '42' => 42, 'foo' => 'foo' | ||
| * | ||
| * @param string $url | ||
| * @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; | ||
| } | ||
|
|
||
| /** | ||
| * Return the main attributes of the database connection config from url. | ||
| * | ||
| * @return array | ||
| */ | ||
| protected function getMainAttributes(): array | ||
| { | ||
| return array_filter([ | ||
| 'driver' => $this->getDriver(), | ||
| 'database' => $this->getDatabase(), | ||
| 'host' => $this->getInUrl('host'), | ||
| 'port' => $this->getInUrl('port'), | ||
| 'username' => $this->getInUrl('user'), | ||
| 'password' => $this->getInUrl('pass'), | ||
| ], function ($value) { | ||
| return $value !== null; | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Find connection driver from url. | ||
| * | ||
| * @return string|null | ||
| */ | ||
| protected function getDriver() | ||
| { | ||
| $alias = $this->getInUrl('scheme'); | ||
|
|
||
| if (! $alias) { | ||
| return null; | ||
| } | ||
|
|
||
| return static::$driverAliases[$alias] ?? $alias; | ||
| } | ||
|
|
||
| /** | ||
| * Get a component of the parsed url. | ||
| * | ||
| * @param string $key | ||
| * @return string|null | ||
| */ | ||
| protected function getInUrl($key) | ||
| { | ||
| return $this->parsedUrl[$key] ?? null; | ||
| } | ||
|
|
||
| /** | ||
| * Find connection database from url. | ||
| * | ||
| * @return string|null | ||
| */ | ||
| protected function getDatabase() | ||
| { | ||
| $path = $this->getInUrl('path'); | ||
|
|
||
| if (! $path) { | ||
| return null; | ||
| } | ||
|
|
||
| return substr($path, 1); | ||
| } | ||
|
|
||
| /** | ||
| * Return all the options added to the url with query params. | ||
| * | ||
| * @return array | ||
| */ | ||
| protected function getOtherOptions(): array | ||
| { | ||
| $queryString = $this->getInUrl('query'); | ||
|
|
||
| if (! $queryString) { | ||
| return []; | ||
| } | ||
|
|
||
| $query = []; | ||
|
|
||
| parse_str($queryString, $query); | ||
|
|
||
| return $this->parseStringsToNativeTypes($query); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.