Skip to content

Commit

Permalink
Improve uri DX and public API
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Jun 28, 2023
1 parent 63fdb3d commit e876453
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 30 deletions.
15 changes: 8 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ All Notable changes to `League\Uri` will be documented in this file

### Added

- `League\Uri\UriTemplate\Template` is now part of the public API
- `League\Uri\Uri::new`
- `League\Uri\Uri::fromComponents`
- `League\Uri\Uri::fromServer`
- `League\Uri\Uri::fromWindowsPath`
- `League\Uri\Uri::fromUnixPath`
- `League\Uri\Uri::fromDataPath`
- `League\Uri\Uri::fromBaseUri`
- `League\Uri\Uri::fromFileContents`
- `League\Uri\Uri::fromClient`
- `League\Uri\Uri::fromTemplate`
- `League\Uri\Http::new`
- `League\Uri\Http::fromComponents`
Expand All @@ -22,6 +21,8 @@ All Notable changes to `League\Uri` will be documented in this file
- `League\Uri\Http::fromTemplate`
- `League\Uri\UriTemplate::expandOrFail`
- `League\Uri\UriTemplate\Template::expandOrFail`
- `League\Uri\UriString::parseAuthority`
- `League\Uri\UriString::buildAuthority`

### Fixed

Expand All @@ -34,17 +35,17 @@ All Notable changes to `League\Uri` will be documented in this file
- `League\Uri\Uri::createFromString` use `League\Uri\Uri::new`
- `League\Uri\Uri::createFromUri` use `League\Uri\Uri::new`
- `League\Uri\Uri::createFromComponents` use `League\Uri\Uri::fromComponents`
- `League\Uri\Uri::createFromBaseUri` use `League\Uri\Uri::fromBaseUri`
- `League\Uri\Uri::createFromBaseUri` use `League\Uri\Uri::fromClient`
- `League\Uri\Uri::createFromServer` use `League\Uri\Uri::fromServer`
- `League\Uri\Uri::createFromWindowsPath` use `League\Uri\Uri::fromWindowsPath`
- `League\Uri\Uri::createFromUnixPath` use `League\Uri\Uri::fromUnixPath`
- `League\Uri\Uri::createFromDataPath` use `League\Uri\Uri::fromDataPath`
- `League\Uri\Uri::createFromDataPath` use `League\Uri\Uri::fromFileContents`
- `League\Uri\Http::createFromString` use `League\Uri\Http::new`
- `League\Uri\Http::createFromUri` use `League\Uri\Http::new`
- `League\Uri\Http::createFromComponents` use `League\Uri\Http::fromComponents`
- `League\Uri\Http::createFromBaseUri` use `League\Uri\Http::fromBaseUri`
- `League\Uri\Http::createFromBaseUri` use `League\Uri\Http::fromClient`
- `League\Uri\Http::createFromServer` use `League\Uri\Http::fromServer`
- `League\Uri\UriTemplate\Template::createFromString` use `League\Uri\UriTemplate\Template::fromString`
- `League\Uri\UriTemplate\Template::createFromString` use `League\Uri\UriTemplate\Template::new`

### Remove

Expand Down
14 changes: 7 additions & 7 deletions FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class FactoryTest extends TestCase
public function testCreateFromPathFailed(string $path): void
{
self::expectException(SyntaxError::class);
Uri::fromDataPath($path);
Uri::fromFileContents($path);
}

public static function invalidDataPath(): array
Expand All @@ -48,7 +48,7 @@ public function testCreateFromPath(string $path, string $expected): void
],
]);

$uri = Uri::fromDataPath(dirname(__DIR__).'/test_files/'.$path, $context);
$uri = Uri::fromFileContents(dirname(__DIR__).'/test_files/'.$path, $context);
self::assertStringContainsString($expected, $uri->getPath());
}

Expand Down Expand Up @@ -339,7 +339,7 @@ public function testFailCreateFromServerWithoutInvalidUserInfo(): void
*/
public function testCreateFromBaseUri(string $base_uri, string $uri, string $expected): void
{
self::assertSame($expected, Uri::fromBaseUri($uri, $base_uri)->toString());
self::assertSame($expected, Uri::fromClient($uri, $base_uri)->toString());
}

public static function createProvider(): array
Expand Down Expand Up @@ -392,28 +392,28 @@ public static function createProvider(): array
public function testCreateThrowExceptionWithBaseUriNotAbsolute(): void
{
self::expectException(SyntaxError::class);
Uri::fromBaseUri('/path/to/you', '//example.com');
Uri::fromClient('/path/to/you', '//example.com');
}

public function testCreateThrowExceptionWithUriNotAbsolute(): void
{
self::expectException(SyntaxError::class);
Uri::fromBaseUri('/path/to/you');
Uri::fromClient('/path/to/you');
}

public function testCreateWithUriWithoutAuthority(): void
{
self::assertSame(
'data:text/plain;charset=us-ascii,',
Uri::fromBaseUri('data:text/plain;charset=us-ascii,')->toString()
Uri::fromClient('data:text/plain;charset=us-ascii,')->toString()
);
}

public function testCreateWithAbsoluteUriWithoutBaseUri(): void
{
self::assertSame(
'scheme://host/sky?q#f',
Uri::fromBaseUri('scheme://host/path/../sky?q#f')->toString()
Uri::fromClient('scheme://host/path/../sky?q#f')->toString()
);
}

Expand Down
8 changes: 4 additions & 4 deletions Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ public static function fromServer(array $server): self
*
* The returned URI must be absolute.
*/
public static function fromBaseUri(Stringable|String $uri, Stringable|String|null $baseUri = null): self
public static function fromClient(Stringable|String $uri, Stringable|String|null $baseUri = null): self
{
return new self(Uri::fromBaseUri($uri, $baseUri));
return new self(Uri::fromClient($uri, $baseUri));
}

public static function fromTemplate(Stringable|string $template, iterable $variables = []): self
Expand Down Expand Up @@ -259,14 +259,14 @@ public static function createFromUri(Stringable|string $uri): self
*
* @deprecated Since version 7.0.0
* @codeCoverageIgnore
* @see Http::fromBaseUri()
* @see Http::fromClient()
*
* Create a new instance from a URI and a Base URI.
*
* The returned URI must be absolute.
*/
public static function createFromBaseUri(Stringable|String $uri, Stringable|String|null $baseUri = null): self
{
return self::fromBaseUri($uri, $baseUri);
return self::fromClient($uri, $baseUri);
}
}
2 changes: 1 addition & 1 deletion HttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function testCreateFromBaseUri(): void
{
self::assertEquals(
Http::new('http://0:0@0/0?0#0'),
Http::fromBaseUri('0?0#0', 'http://0:0@0/')
Http::fromClient('0?0#0', 'http://0:0@0/')
);
}

Expand Down
22 changes: 11 additions & 11 deletions Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ public static function new(Stringable|string $uri = ''): self
*
* The returned URI must be absolute.
*/
public static function fromBaseUri(Stringable|String $uri, Stringable|String|null $baseUri = null): UriInterface
public static function fromClient(Stringable|String $uri, Stringable|String|null $baseUri = null): UriInterface
{
if (!$uri instanceof UriInterface) {
$uri = self::new($uri);
Expand Down Expand Up @@ -513,7 +513,7 @@ public static function fromComponents(array $components = []): self
* @throws FileinfoSupportMissing If ext/fileinfo is not installed
* @throws SyntaxError If the file does not exist or is not readable
*/
public static function fromDataPath(Stringable|string $path, $context = null): self
public static function fromFileContents(Stringable|string $path, $context = null): self
{
static $finfoSupport = null;
$finfoSupport = $finfoSupport ?? class_exists(finfo::class);
Expand Down Expand Up @@ -551,7 +551,7 @@ public static function fromDataPath(Stringable|string $path, $context = null): s
/**
* Create a new instance from a Unix path string.
*/
public static function fromUnixPath(Stringable|string $path = ''): self
public static function fromUnixPath(Stringable|string $path): self
{
$path = implode('/', array_map(rawurlencode(...), explode('/', (string) $path)));
if ('/' !== ($path[0] ?? '')) {
Expand All @@ -564,7 +564,7 @@ public static function fromUnixPath(Stringable|string $path = ''): self
/**
* Create a new instance from a local Windows path string.
*/
public static function fromWindowsPath(Stringable|string $path = ''): self
public static function fromWindowsPath(Stringable|string $path): self
{
$path = (string) $path;
$root = '';
Expand Down Expand Up @@ -1276,26 +1276,26 @@ public static function createFromComponents(array $components = []): self
/**
* DEPRECATION WARNING! This method will be removed in the next major point release.
*
* @deprecated Since version 9.9.0
* @codeCoverageIgnore
* @see Uri::fromDataPath()
*
* @param resource|null $context
*
* @throws FileinfoSupportMissing If ext/fileinfo is not installed
* @throws SyntaxError If the file does not exist or is not readable
*@see Uri::fromFileContents()
*
* @deprecated Since version 9.9.0
* @codeCoverageIgnore
*/
public static function createFromDataPath(string $path, $context = null): self
{
return self::fromDataPath($path, $context);
return self::fromFileContents($path, $context);
}

/**
* DEPRECATION WARNING! This method will be removed in the next major point release.
*
* @deprecated Since version 7.0.0
* @codeCoverageIgnore
* @see Uri::fromBaseUri()
* @see Uri::fromClient()
*
* Creates a new instance from a URI and a Base URI.
*
Expand All @@ -1305,7 +1305,7 @@ public static function createFromBaseUri(
Stringable|UriInterface|String $uri,
Stringable|UriInterface|String|null $baseUri = null
): UriInterface {
return self::fromBaseUri($uri, $baseUri);
return self::fromClient($uri, $baseUri);
}

/**
Expand Down

0 comments on commit e876453

Please sign in to comment.