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