Skip to content

Commit 1357613

Browse files
committed
Deprecated Uri::fromBaseUri use instead Uri::parse
1 parent a9b8264 commit 1357613

File tree

7 files changed

+109
-59
lines changed

7 files changed

+109
-59
lines changed

components/ModifierTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,8 @@ public static function removeQueryParameterIndicesProvider(): array
264264
#[DataProvider('removeEmptyPairsProvider')]
265265
public function testRemoveEmptyPairs(string $uri, ?string $expected): void
266266
{
267-
self::assertSame($expected, Modifier::wrap(Uri::fromBaseUri($uri))->removeEmptyQueryPairs()->toString());
268-
self::assertSame($expected, Modifier::wrap(Http::fromBaseUri($uri))->removeEmptyQueryPairs()->toString());
267+
self::assertSame($expected, Modifier::wrap(Uri::new($uri))->removeEmptyQueryPairs()->toString());
268+
self::assertSame($expected, Modifier::wrap(Http::new($uri))->removeEmptyQueryPairs()->toString());
269269
}
270270

271271
public static function removeEmptyPairsProvider(): iterable

docs/uri/7.0/rfc3986.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ header is taken into account for security reasons.</p>
5454
You can also return a URI based on standard specifications:
5555

5656
~~~php
57-
$uri = Uri::fromBaseUri("./p#~toto", "http://www.example.com/path/to/the/sky/");
57+
$uri = Uri::parse("./p#~toto", "http://www.example.com/path/to/the/sky/");
5858
echo $uri; //displays "http://www.example.com/path/to/the/sky/p#~toto"
5959

6060
$template = 'https://example.com/hotels/{hotel}/bookings/{booking}';
@@ -70,9 +70,14 @@ $uri = Uri::fromData('Héllo World!', 'text/plain', 'charset=utf8');
7070
echo $uri; // returns data:text/plain;charset=utf8,H%C3%A9llo%20World%21
7171
~~~
7272

73-
The `fromBaseUri` method resolves URI using the same logic behind URL construction
73+
The `parse` method resolves URI using the same logic behind URL construction
7474
in a browser and [is inline with how the Javascript](https://developer.mozilla.org/en-US/docs/Web/API/URL/URL) `URL` object constructor works.
75-
If no base URI is provided, the URI to resolve **MUST** be absolute. Otherwise, the base URI **MUST** be absolute.
75+
If a new instance cannot be returned, the method returns `null`.
76+
77+
~~~php
78+
$uri = Uri::parse("invalid uri", "http://www.example.com/path/to/the/sky/");
79+
var_dump($uri); // return null
80+
~~~
7681

7782
The `fromTemplate` method resolves a URI using the rules and variable from the
7883
[URITemplate specification RFC6570](http://tools.ietf.org/html/rfc6570):
@@ -104,6 +109,7 @@ echo $uri = //returns 'file:///etc/fstab'
104109
~~~
105110

106111
<p class="message-info"><code>fromRfc8089</code> is added since version <code>7.4.0</code></p>
112+
<p class="message-warning"><code>fromBaseUri</code> is deprecated since version <code>7.6.0</code> use <code>parse</code> instead</p>
107113

108114
## URI string representation
109115

uri/FactoryTest.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,10 @@ public function testFailCreateFromServerWithoutInvalidUserInfo(): void
445445
#[DataProvider('createProvider')]
446446
public function testCreateFromBaseUri(Stringable|string $baseUri, Stringable|string $uri, string $expected): void
447447
{
448-
self::assertSame($expected, Uri::fromBaseUri($uri, $baseUri)->toString());
448+
$newUri = Uri::parse($uri, $baseUri);
449+
450+
self::assertInstanceOf(Uri::class, $newUri);
451+
self::assertSame($expected, $newUri->toString());
449452
}
450453

451454
public static function createProvider(): array
@@ -500,30 +503,27 @@ public static function createProvider(): array
500503

501504
public function testCreateThrowExceptionWithBaseUriNotAbsolute(): void
502505
{
503-
self::expectException(SyntaxError::class);
504-
Uri::fromBaseUri('/path/to/you', '//example.com');
506+
self::assertNull(Uri::parse('/path/to/you', '//example.com'));
505507
}
506508

507-
public function testCreateThrowExceptionWithUriNotAbsolute(): void
509+
#[TestWith(['data:text/plain;charset=us-ascii,'])]
510+
#[TestWith(['/path/to/you'])]
511+
public function testCreateWithUriWithoutAuthority(string $expected): void
508512
{
509-
self::expectException(SyntaxError::class);
510-
Uri::fromBaseUri('/path/to/you');
511-
}
513+
$uri = Uri::parse($expected);
512514

513-
public function testCreateWithUriWithoutAuthority(): void
514-
{
515-
self::assertSame(
516-
'data:text/plain;charset=us-ascii,',
517-
Uri::fromBaseUri('data:text/plain;charset=us-ascii,')->toString()
518-
);
515+
self::assertInstanceOf(Uri::class, $uri);
516+
self::assertSame($expected, $uri->toString());
519517
}
520518

521519
public function testCreateWithAbsoluteUriWithoutBaseUri(): void
522520
{
523-
self::assertSame(
524-
'scheme://host/sky?q#f',
525-
Uri::fromBaseUri('scheme://host/path/../sky?q#f')->toString()
526-
);
521+
$expected = 'scheme://host/sky?q#f';
522+
$uri = Uri::parse('scheme://host/path/../sky?q#f');
523+
524+
self::assertInstanceOf(Uri::class, $uri);
525+
self::assertNotSame($expected, $uri->toString());
526+
self::assertSame($expected, $uri->normalize()->toString());
527527
}
528528

529529
public function testCreateFromComponentsWithNullPath(): void

uri/Http.php

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,24 +139,24 @@ public static function fromServer(array $server): self
139139
}
140140

141141
/**
142-
* Create a new instance from a URI and a Base URI.
142+
* Creates a new instance from a template.
143143
*
144-
* The returned URI must be absolute.
144+
* @throws TemplateCanNotBeExpanded if the variables are invalid or missing
145+
* @throws UriException if the variables are invalid or missing
145146
*/
146-
public static function fromBaseUri(Rfc3986Uri|WhatwgUrl|Stringable|string $uri, Rfc3986Uri|WhatwgUrl|Stringable|string|null $baseUri = null): self
147+
public static function fromTemplate(Stringable|string $template, iterable $variables = []): self
147148
{
148-
return new self(Uri::fromBaseUri($uri, $baseUri));
149+
return new self(Uri::fromTemplate($template, $variables));
149150
}
150151

151152
/**
152-
* Creates a new instance from a template.
153+
* Returns a new instance from a URI and a Base URI.or null on failure.
153154
*
154-
* @throws TemplateCanNotBeExpanded if the variables are invalid or missing
155-
* @throws UriException if the variables are invalid or missing
155+
* The returned URI must be absolute if a base URI is provided
156156
*/
157-
public static function fromTemplate(Stringable|string $template, iterable $variables = []): self
157+
public static function parse(WhatWgUrl|Rfc3986Uri|Stringable|string $uri, WhatWgUrl|Rfc3986Uri|Stringable|string|null $baseUri = null): ?self
158158
{
159-
return new self(Uri::fromTemplate($template, $variables));
159+
return null !== ($uri = Uri::parse($uri, $baseUri)) ? new self($uri) : null;
160160
}
161161

162162
public function getScheme(): string
@@ -281,6 +281,23 @@ public function withFragment(string $fragment): self
281281
return $this->newInstance($this->uri->withFragment($this->filterInput($fragment)));
282282
}
283283

284+
/**
285+
* DEPRECATION WARNING! This method will be removed in the next major point release.
286+
*
287+
* @deprecated Since version 7.6.0
288+
* @codeCoverageIgnore
289+
* @see Http::parse()
290+
*
291+
* Create a new instance from a URI and a Base URI.
292+
*
293+
* The returned URI must be absolute.
294+
*/
295+
#[Deprecated(message:'use League\Uri\Http::parse() instead', since:'league/uri:7.6.0')]
296+
public static function fromBaseUri(Rfc3986Uri|WhatwgUrl|Stringable|string $uri, Rfc3986Uri|WhatwgUrl|Stringable|string|null $baseUri = null): self
297+
{
298+
return new self(Uri::fromBaseUri($uri, $baseUri));
299+
}
300+
284301
/**
285302
* DEPRECATION WARNING! This method will be removed in the next major point release.
286303
*

uri/HttpTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function testCreateFromBaseUri(): void
9696
{
9797
self::assertEquals(
9898
Http::new('http://0:0@0/0?0#0'),
99-
Http::fromBaseUri('0?0#0', 'http://0:0@0/')
99+
Http::parse('0?0#0', 'http://0:0@0/')
100100
);
101101
}
102102

uri/Uri.php

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -494,36 +494,30 @@ public static function new(WhatWgUrl|Rfc3986Uri|Stringable|string $uri = ''): se
494494
public static function parse(WhatWgUrl|Rfc3986Uri|Stringable|string $uri, WhatWgUrl|Rfc3986Uri|Stringable|string|null $baseUri = null): ?self
495495
{
496496
try {
497-
return null === $baseUri ? self::new($uri) : self::fromBaseUri($uri, $baseUri);
498-
} catch (Throwable) {
499-
return null;
500-
}
501-
}
497+
if (null === $baseUri) {
498+
return self::new($uri);
499+
}
502500

503-
/**
504-
* Creates a new instance from a URI and a Base URI.
505-
*
506-
* The returned URI must be absolute.
507-
*/
508-
public static function fromBaseUri(WhatWgUrl|Rfc3986Uri|Stringable|string $uri, WhatWgUrl|Rfc3986Uri|Stringable|string|null $baseUri = null): self
509-
{
510-
if ($uri instanceof Rfc3986Uri) {
511-
$uri = $uri->toRawString();
512-
}
501+
if ($uri instanceof Rfc3986Uri) {
502+
$uri = $uri->toRawString();
503+
}
513504

514-
if ($uri instanceof WhatWgUrl) {
515-
$uri = $uri->toAsciiString();
516-
}
505+
if ($uri instanceof WhatWgUrl) {
506+
$uri = $uri->toAsciiString();
507+
}
517508

518-
if ($baseUri instanceof Rfc3986Uri) {
519-
$baseUri = $baseUri->toRawString();
520-
}
509+
if ($baseUri instanceof Rfc3986Uri) {
510+
$baseUri = $baseUri->toRawString();
511+
}
521512

522-
if ($baseUri instanceof WhatWgUrl) {
523-
$baseUri = $baseUri->toAsciiString();
524-
}
513+
if ($baseUri instanceof WhatWgUrl) {
514+
$baseUri = $baseUri->toAsciiString();
515+
}
525516

526-
return self::new(UriString::resolve($uri, $baseUri));
517+
return self::new(UriString::resolve($uri, $baseUri));
518+
} catch (Throwable) {
519+
return null;
520+
}
527521
}
528522

529523
/**
@@ -1727,6 +1721,39 @@ public function __debugInfo(): array
17271721
return $this->toComponents();
17281722
}
17291723

1724+
/**
1725+
* DEPRECATION WARNING! This method will be removed in the next major point release.
1726+
*
1727+
* @deprecated Since version 7.6.0
1728+
* @codeCoverageIgnore
1729+
* @see Uri::parse()
1730+
*
1731+
* Creates a new instance from a URI and a Base URI.
1732+
*
1733+
* The returned URI must be absolute.
1734+
*/
1735+
#[Deprecated(message:'use League\Uri\Uri::parse() instead', since:'league/uri:7.6.0')]
1736+
public static function fromBaseUri(WhatWgUrl|Rfc3986Uri|Stringable|string $uri, WhatWgUrl|Rfc3986Uri|Stringable|string|null $baseUri = null): self
1737+
{
1738+
if ($uri instanceof Rfc3986Uri) {
1739+
$uri = $uri->toRawString();
1740+
}
1741+
1742+
if ($uri instanceof WhatWgUrl) {
1743+
$uri = $uri->toAsciiString();
1744+
}
1745+
1746+
if ($baseUri instanceof Rfc3986Uri) {
1747+
$baseUri = $baseUri->toRawString();
1748+
}
1749+
1750+
if ($baseUri instanceof WhatWgUrl) {
1751+
$baseUri = $baseUri->toAsciiString();
1752+
}
1753+
1754+
return self::new(UriString::resolve($uri, $baseUri));
1755+
}
1756+
17301757
/**
17311758
* DEPRECATION WARNING! This method will be removed in the next major point release.
17321759
*

uri/UriTemplate.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public function expand(iterable $variables = [], Rfc3986Uri|WhatWgUrl|Stringable
136136
{
137137
$expanded = $this->templateExpanded($variables);
138138

139-
return null === $baseUri ? Uri::new($expanded) : Uri::fromBaseUri($expanded, $baseUri);
139+
return null === $baseUri ? Uri::new($expanded) : (Uri::parse($expanded, $baseUri) ?? throw new SyntaxError('Unable to expand URI'));
140140
}
141141

142142
/**
@@ -195,7 +195,7 @@ public function expandOrFail(iterable $variables = [], Rfc3986Uri|WhatWgUrl|Stri
195195
{
196196
$expanded = $this->templateExpandedOrFail($variables);
197197

198-
return null === $baseUri ? Uri::new($expanded) : Uri::fromBaseUri($expanded, $baseUri);
198+
return null === $baseUri ? Uri::new($expanded) : (Uri::parse($expanded, $baseUri) ?? throw new SyntaxError('Unable to expand URI'));
199199
}
200200

201201
/**

0 commit comments

Comments
 (0)