Debian Codegraph @

lib/3rdParty/composer/illuminate/support/ConfigurationUrlParser.php

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