Skip to content

Commit

Permalink
using ::createFromServer to reduce BC break
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed May 30, 2019
1 parent 5ec0a7c commit 495dccd
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 34 deletions.
4 changes: 2 additions & 2 deletions docs/uri/6.0/info.md
Expand Up @@ -19,7 +19,7 @@ use League\Uri\Http;
use League\Uri\Uri;
use League\Uri\UriInfo;

UriInfo::isAbsolute(Uri::createFromEnvironment($_SERVER)); //returns true
UriInfo::isAbsolute(Uri::createFromServer($_SERVER)); //returns true
UriInfo::isAbsolute(Http::createFromString("/🍣🍺")); //returns false
~~~

Expand All @@ -28,7 +28,7 @@ UriInfo::isAbsolute(Http::createFromString("/🍣🍺")); //returns false
This public static method tells whether the given URI object represents an absolute URI path.

~~~php
UriInfo::isAbsolutePath(Uri::createFromEnvironment($_SERVER)); //returns false
UriInfo::isAbsolutePath(Uri::createFromServer($_SERVER)); //returns false
UriInfo::isAbsolutePath(Http::createFromString("/🍣🍺")); //returns true
~~~

Expand Down
2 changes: 1 addition & 1 deletion docs/uri/6.0/psr7.md
Expand Up @@ -24,7 +24,7 @@ The `League\Uri\Http` class comes with the following named constructor to ease i
use League\Uri\Http;

//don't forget to provide the $_SERVER array
$uri = Http::createFromEnvironment($_SERVER);
$uri = Http::createFromServer($_SERVER);
~~~

<p class="message-warning">The method only relies on the server's safe parameters to determine the current URI. If you are using the library behind a proxy the result may differ from your expectation as no <code>$_SERVER['HTTP_X_*']</code> header is taken into account for security reasons.</p>
Expand Down
12 changes: 7 additions & 5 deletions docs/uri/6.0/rfc3986.md
Expand Up @@ -13,16 +13,18 @@ Creating new URI objects

### URI instantiation

To instantiate a new URI object you can use multiple named constructors which all returns an URI object from:

~~~php
<?php
public static Uri::createFromString(string $uri = ''): Uri
public static Uri::create($uri, $base_uri = null): Uri
public static Uri::createFromComponents(array $components): Uri
public static Uri::createFromDataPath(string $path, resource $context = null): Uri
public static Uri::createFromPsr7(UriInterface $psr7Uri): Uri
public static Uri::createFromServer(array $environment): Uri
public static Uri::createFromString(string $uri = ''): Uri
public static Uri::createFromUnixPath(string $path): Uri
public static Uri::createFromWindowsPath(string $path): Uri
public static Uri::createFromPsr7(UriInterface $psr7Uri): Uri
public static Uri::createFromEnvironment(array $environment): Uri
public static Uri::create($uri, $base_uri = null): Uri
~~~

To instantiate a new URI object you can use multiple named constructors which all returns an URI object from:
Expand All @@ -33,7 +35,7 @@ To instantiate a new URI object you can use multiple named constructors which al
- a Unix file path with `Uri::createFromUnixPath`.
- a Windows file path with `Uri::createFromWindowsPath`.
- a PSR-7 `UriInterface` with `Uri::createFromPsr7`.
- the environment array (typically the `$_SERVER` variable) with `Uri::createFromEnvironment`.
- the environment array (typically the `$_SERVER` variable) with `Uri::createFromServer`.
- a URI and its base URI with `Uri::create`.

<p class="message-warning">The default constructor is private and can not be accessed to instantiate a new object.</p>
Expand Down
8 changes: 8 additions & 0 deletions src/Http.php
Expand Up @@ -86,6 +86,14 @@ public static function createFromComponents(array $components): self
return new self(Uri::createFromComponents($components));
}

/**
* Create a new instance from the environment.
*/
public static function createFromServer(array $server): self
{
return new self(Uri::createFromServer($server));
}

/**
* Create a new instance from a URI and a Base URI.
*
Expand Down
44 changes: 22 additions & 22 deletions src/Uri.php
Expand Up @@ -688,49 +688,49 @@ public static function createFromWindowsPath(string $uri = ''): self
*/
public static function createFromPsr7(Psr7UriInterface $uri): self
{
$components = [
'scheme' => null,
'user' => null,
'pass' => null,
'host' => null,
'port' => $uri->getPort(),
'path' => $uri->getPath(),
'query' => null,
'fragment' => null,
];

$scheme = $uri->getScheme();
if ('' !== $scheme) {
$components['scheme'] = $scheme;
if ('' === $scheme) {
$scheme = null;
}

$fragment = $uri->getFragment();
if ('' !== $fragment) {
$components['fragment'] = $fragment;
if ('' === $fragment) {
$fragment = null;
}

$query = $uri->getQuery();
if ('' !== $query) {
$components['query'] = $query;
if ('' === $query) {
$query = null;
}

$host = $uri->getHost();
if ('' !== $host) {
$components['host'] = $host;
if ('' === $host) {
$host = null;
}

$user_info = $uri->getUserInfo();
$user = null;
$pass = null;
if ('' !== $user_info) {
[$components['user'], $components['pass']] = explode(':', $user_info, 2) + [1 => null];
[$user, $pass] = explode(':', $user_info, 2) + [1 => null];
}

return Uri::createFromComponents($components);
return new self(
$scheme,
$user,
$pass,
$host,
$uri->getPort(),
$uri->getPath(),
$query,
$fragment
);
}

/**
* Create a new instance from the environment.
*/
public static function createFromEnvironment(array $server): self
public static function createFromServer(array $server): self
{
[$user, $pass] = self::fetchUserInfo($server);
[$host, $port] = self::fetchHostname($server);
Expand Down
13 changes: 9 additions & 4 deletions tests/FactoryTest.php
Expand Up @@ -159,10 +159,14 @@ public function testCreateUri(): void

$uri = Uri::createFromPsr7($psr7);
self::assertSame((string) $psr7, (string) $uri);

$uribis = Http::createFromString();
self::assertSame((string) $uribis, Uri::createFromPsr7($uribis)->__toString());
}

/**
* @covers ::createFromEnvironment
* @covers ::createFromServer
* @covers League\Uri\Http::createFromServer
* @covers ::fetchScheme
* @covers ::fetchUserInfo
* @covers ::fetchHostname
Expand All @@ -172,7 +176,8 @@ public function testCreateUri(): void
*/
public function testCreateFromServer(string $expected, array $input): void
{
self::assertSame($expected, (string) Uri::createFromEnvironment($input));
self::assertSame($expected, (string) Uri::createFromServer($input));
self::assertSame($expected, (string) Http::createFromServer($input));
}

public function validServerArray(): array
Expand Down Expand Up @@ -326,7 +331,7 @@ public function validServerArray(): array
public function testFailCreateFromServerWithoutHost(): void
{
self::expectException(SyntaxError::class);
Uri::createFromEnvironment([
Uri::createFromServer([
'PHP_SELF' => '',
'REQUEST_URI' => '',
'HTTPS' => 'on',
Expand All @@ -340,7 +345,7 @@ public function testFailCreateFromServerWithoutHost(): void
public function testFailCreateFromServerWithoutInvalidUserInfo(): void
{
self::expectException(SyntaxError::class);
Uri::createFromEnvironment([
Uri::createFromServer([
'PHP_SELF' => '/toto',
'SERVER_ADDR' => '127.0.0.1',
'HTTPS' => 'on',
Expand Down

0 comments on commit 495dccd

Please sign in to comment.