Skip to content

Commit 51233af

Browse files
[URI] Enhance URI class (#53796)
* enhance URI class * Update Uri.php --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 7b92398 commit 51233af

File tree

5 files changed

+20
-31
lines changed

5 files changed

+20
-31
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"league/commonmark": "^2.2.1",
3737
"league/flysystem": "^3.25.1",
3838
"league/flysystem-local": "^3.25.1",
39-
"league/uri": "^7.4",
39+
"league/uri": "^7.5.1",
4040
"monolog/monolog": "^3.0",
4141
"nesbot/carbon": "^2.72.2|^3.4",
4242
"nunomaduro/termwind": "^2.0",

src/Illuminate/Support/Uri.php

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ class Uri implements Htmlable, Responsable
1818
{
1919
use Conditionable, Tappable;
2020

21+
/**
22+
* The URI instance.
23+
*/
24+
protected UriInterface $uri;
25+
2126
/**
2227
* The URL generator resolver.
2328
*/
@@ -75,29 +80,17 @@ public function scheme(): ?string
7580
*/
7681
public function user(bool $withPassword = false): ?string
7782
{
78-
if ($withPassword) {
79-
return $this->uri->getUserInfo();
80-
}
81-
82-
$userInfo = $this->uri->getUserInfo();
83-
84-
if (is_null($userInfo)) {
85-
return null;
86-
}
87-
88-
return str_contains($userInfo, ':')
89-
? Str::before($userInfo, ':')
90-
: $userInfo;
83+
return $withPassword
84+
? $this->uri->getUserInfo()
85+
: $this->uri->getUsername();
9186
}
9287

9388
/**
9489
* Get the password from the URI.
9590
*/
9691
public function password(): ?string
9792
{
98-
$userInfo = $this->uri->getUserInfo();
99-
100-
return ! is_null($userInfo) ? Str::after($userInfo, ':') : null;
93+
return $this->uri->getPassword();
10194
}
10295

10396
/**
@@ -272,7 +265,7 @@ public function withFragment(string $fragment): static
272265
*/
273266
public function redirect(int $status = 302, array $headers = []): RedirectResponse
274267
{
275-
return new RedirectResponse((string) $this, $status, $headers);
268+
return new RedirectResponse($this->value(), $status, $headers);
276269
}
277270

278271
/**
@@ -283,7 +276,7 @@ public function redirect(int $status = 302, array $headers = []): RedirectRespon
283276
*/
284277
public function toResponse($request)
285278
{
286-
return new RedirectResponse((string) $this);
279+
return new RedirectResponse($this->value());
287280
}
288281

289282
/**
@@ -293,7 +286,7 @@ public function toResponse($request)
293286
*/
294287
public function toHtml()
295288
{
296-
return (string) $this;
289+
return $this->value();
297290
}
298291

299292
/**
@@ -309,7 +302,7 @@ public function value(): string
309302
*/
310303
public function isEmpty(): bool
311304
{
312-
return trim((string) $this) === '';
305+
return trim($this->value()) === '';
313306
}
314307

315308
/**

src/Illuminate/Support/UriQueryString.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Contracts\Support\Arrayable;
66
use Illuminate\Support\Traits\InteractsWithData;
7+
use League\Uri\QueryString;
78

89
class UriQueryString implements Arrayable
910
{
@@ -81,13 +82,7 @@ public function value(): string
8182
*/
8283
public function toArray()
8384
{
84-
if (is_null($query = $this->uri->getUri()->getQuery())) {
85-
return [];
86-
}
87-
88-
parse_str($query, $currentQuery);
89-
90-
return $currentQuery;
85+
return QueryString::extract($this->value());
9186
}
9287

9388
/**

src/Illuminate/Support/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"illuminate/filesystem": "Required to use the Composer class (^11.0).",
5151
"laravel/serializable-closure": "Required to use the once function (^1.3).",
5252
"league/commonmark": "Required to use Str::markdown() and Stringable::markdown() (^2.0.2).",
53-
"league/uri": "Required to use the Uri class (^7.4).",
53+
"league/uri": "Required to use the Uri class (^7.5.1).",
5454
"ramsey/uuid": "Required to use Str::uuid() (^4.7).",
5555
"symfony/process": "Required to use the Composer class (^7.0).",
5656
"symfony/uid": "Required to use Str::ulid() (^7.0).",

tests/Support/SupportUriTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function test_basic_uri_interactions()
3434

3535
public function test_complicated_query_string_parsing()
3636
{
37-
$uri = Uri::of('https://example.com/users?key_1=value&key_2[sub_field]=value&key_3[]=value&key_4[9]=value&key_5[][][foo][9]=bar&flag_value');
37+
$uri = Uri::of('https://example.com/users?key_1=value&key_2[sub_field]=value&key_3[]=value&key_4[9]=value&key_5[][][foo][9]=bar&key.6=value&flag_value');
3838

3939
$this->assertEquals([
4040
'key_1' => 'value',
@@ -56,10 +56,11 @@ public function test_complicated_query_string_parsing()
5656
],
5757
],
5858
],
59+
'key.6' => 'value',
5960
'flag_value' => '',
6061
], $uri->query()->all());
6162

62-
$this->assertEquals('key_1=value&key_2[sub_field]=value&key_3[]=value&key_4[9]=value&key_5[][][foo][9]=bar&flag_value', $uri->query()->decode());
63+
$this->assertEquals('key_1=value&key_2[sub_field]=value&key_3[]=value&key_4[9]=value&key_5[][][foo][9]=bar&key.6=value&flag_value', $uri->query()->decode());
6364
}
6465

6566
public function test_uri_building()

0 commit comments

Comments
 (0)