Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add League URI #14194

Draft
wants to merge 1 commit into
base: 5.x
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add League URI
timkelty committed Jan 24, 2024
commit a54c6145f706f97ba3520c4ca0efd4cb62cd9753
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -44,6 +44,8 @@
"enshrined/svg-sanitize": "~0.16.0",
"guzzlehttp/guzzle": "^7.2.0",
"illuminate/collections": "^9.1.0",
"league/uri": "^7.4",
"league/uri-components": "^7.4",
"mikehaertl/php-shellcommand": "^1.6.3",
"moneyphp/money": "^4.0",
"monolog/monolog": "^3.0",
260 changes: 258 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 19 additions & 33 deletions src/helpers/UrlHelper.php
Original file line number Diff line number Diff line change
@@ -9,6 +9,10 @@

use Craft;
use craft\errors\SiteNotFoundException;
use League\Uri\BaseUri;
use League\Uri\Components\Query;
use League\Uri\Http;
use League\Uri\Uri;
use yii\base\Exception;

/**
@@ -27,7 +31,7 @@ class UrlHelper
*/
public static function isAbsoluteUrl(string $url): bool
{
return (str_starts_with($url, 'http://') || str_starts_with($url, 'https://'));
return BaseUri::from($url)->isAbsolute();
}

/**
@@ -38,7 +42,7 @@ public static function isAbsoluteUrl(string $url): bool
*/
public static function isProtocolRelativeUrl(string $url): bool
{
return (str_starts_with($url, '//'));
return BaseUri::from($url)->isNetworkPath();
}

/**
@@ -49,7 +53,7 @@ public static function isProtocolRelativeUrl(string $url): bool
*/
public static function isRootRelativeUrl(string $url): bool
{
return (str_starts_with($url, '/') && !static::isProtocolRelativeUrl($url));
return BaseUri::from($url)->isAbsolutePath();
}

/**
@@ -134,20 +138,9 @@ public static function urlWithParams(string $url, array|string $params): string
*/
public static function removeParam(string $url, string $param): string
{
// Extract any params/fragment from the base URL
[$url, $params, $fragment] = self::_extractParams($url);

// Remove the param
unset($params[$param]);

// Rebuild
if (($query = static::buildQuery($params)) !== '') {
$url .= '?' . $query;
}
if ($fragment !== null) {
$url .= '#' . $fragment;
}
return $url;
return Uri::new($url)->withQuery(
Query::fromUri($url)->withoutParameters($param)
)->toString();
}

/**
@@ -182,16 +175,13 @@ public static function urlWithScheme(string $url, string $scheme): string
return $url;
}

if (static::isProtocolRelativeUrl($url)) {
return $scheme . ':' . $url;
}
$uri = Uri::new($url);

if (static::isRootRelativeUrl($url)) {
// Prepend the current request’s scheme and hostname
$url = static::siteHost() . $url;
$uri = $uri->withHost(static::host());
}

return preg_replace('/^https?:/', $scheme . ':', $url);
return $uri->withScheme($scheme)->toString();
}

/**
@@ -216,16 +206,12 @@ public static function encodeParams(string $url): string
*/
public static function rootRelativeUrl(string $url): string
{
$url = static::urlWithScheme($url, 'http');
if (strlen($url) > 7 && ($slash = strpos($url, '/', 7)) !== false) {
return substr($url, $slash);
}
// Is this a host without a URI?
if (str_contains($url, '//')) {
return '/';
}
// Must just be a URI, then
return '/' . $url;
return Uri::new($url)
->withScheme(null)
->withHost(null)
->withPort(null)
->withUserInfo(null)
->toString();
}

/**