Remove dependency on league/uri

This commit is contained in:
Alejandro Celaya 2024-02-05 22:44:23 +01:00
parent aa242eba25
commit e014cfa72a
6 changed files with 28 additions and 30 deletions

View File

@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this
### Changed
* [#1935](https://github.com/shlinkio/shlink/issues/1935) Replace dependency on abandoned `php-middleware/request-id` with userland simple middleware.
* [#1988](https://github.com/shlinkio/shlink/issues/1988) Remove dependency on `league\uri` package.
### Deprecated
* *Nothing*

View File

@ -33,7 +33,6 @@
"laminas/laminas-inputfilter": "^2.27",
"laminas/laminas-servicemanager": "^3.21",
"laminas/laminas-stdlib": "^3.17",
"league/uri": "^6.8",
"matomo/matomo-php-tracker": "^3.2",
"mezzio/mezzio": "^3.17",
"mezzio/mezzio-fastroute": "^3.11",

View File

@ -4,8 +4,8 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Config;
use League\Uri\Exceptions\SyntaxError;
use League\Uri\Uri;
use Laminas\Diactoros\Exception\InvalidArgumentException;
use Laminas\Diactoros\Uri;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;
use Psr\Log\LoggerInterface;
@ -51,8 +51,8 @@ class NotFoundRedirectResolver implements NotFoundRedirectResolverInterface
private function resolvePlaceholders(UriInterface $currentUri, string $redirectUrl): string
{
try {
$redirectUri = Uri::createFromString($redirectUrl);
} catch (SyntaxError $e) {
$redirectUri = new Uri($redirectUrl);
} catch (InvalidArgumentException $e) {
$this->logger->warning('It was not possible to parse "{url}" as a valid URL: {e}', [
'e' => $e,
'url' => $redirectUrl,
@ -63,26 +63,22 @@ class NotFoundRedirectResolver implements NotFoundRedirectResolverInterface
$path = $currentUri->getPath();
$domain = $currentUri->getAuthority();
$replacePlaceholderForPattern = static fn (string $pattern, string $replace, ?string $value): string|null =>
$value === null ? null : str_replace($pattern, $replace, $value);
$replacePlaceholders = static function (
callable $modifier,
?string $value,
string $value,
) use (
$replacePlaceholderForPattern,
$path,
$domain,
): string|null {
$value = $replacePlaceholderForPattern($modifier(self::DOMAIN_PLACEHOLDER), $modifier($domain), $value);
return $replacePlaceholderForPattern($modifier(self::ORIGINAL_PATH_PLACEHOLDER), $modifier($path), $value);
): string {
$value = str_replace(urlencode(self::DOMAIN_PLACEHOLDER), $modifier($domain), $value);
return str_replace(urlencode(self::ORIGINAL_PATH_PLACEHOLDER), $modifier($path), $value);
};
$replacePlaceholdersInPath = static function (string $path) use ($replacePlaceholders): string {
$result = $replacePlaceholders(static fn (mixed $v) => $v, $path);
return str_replace('//', '/', $result ?? '');
return str_replace('//', '/', $result);
};
$replacePlaceholdersInQuery = static fn (?string $query): string|null => $replacePlaceholders(
$replacePlaceholdersInQuery = static fn (string $query): string => $replacePlaceholders(
urlencode(...),
$query,
);

View File

@ -36,9 +36,9 @@ class ShortUrlRedirectionBuilder implements ShortUrlRedirectionBuilderInterface
->__toString();
}
private function resolveQuery(Uri $uri, array $currentQuery): ?string
private function resolveQuery(Uri $uri, array $currentQuery): string
{
$hardcodedQuery = Query::parse($uri->getQuery() ?? '');
$hardcodedQuery = Query::parse($uri->getQuery());
$disableTrackParam = $this->trackingOptions->disableTrackParam;
if ($disableTrackParam !== null) {
@ -48,7 +48,7 @@ class ShortUrlRedirectionBuilder implements ShortUrlRedirectionBuilderInterface
// We want to merge preserving numeric keys, as some params might be numbers
$mergedQuery = ArrayUtils::merge($hardcodedQuery, $currentQuery, true);
return empty($mergedQuery) ? null : Query::build($mergedQuery);
return Query::build($mergedQuery);
}
private function resolvePath(Uri $uri, ?string $extraPath): string

View File

@ -18,6 +18,8 @@ class RedirectTest extends ApiTestCase
public function properRedirectHappensBasedOnUserAgent(?string $userAgent, string $expectedRedirect): void
{
$response = $this->callShortUrl('def456', $userAgent);
self::assertEquals(302, $response->getStatusCode());
self::assertEquals($expectedRedirect, $response->getHeaderLine('Location'));
}

View File

@ -57,8 +57,14 @@ class NotFoundRedirectResolverTest extends TestCase
yield 'base URL with trailing slash' => [
$uri = new Uri('/'),
self::notFoundType(ServerRequestFactory::fromGlobals()->withUri($uri)),
new NotFoundRedirectOptions(baseUrl: 'baseUrl'),
'baseUrl',
new NotFoundRedirectOptions(baseUrl: 'https://example.com/baseUrl'),
'https://example.com/baseUrl',
];
yield 'base URL without trailing slash' => [
$uri = new Uri(''),
self::notFoundType(ServerRequestFactory::fromGlobals()->withUri($uri)),
new NotFoundRedirectOptions(baseUrl: 'https://example.com/baseUrl'),
'https://example.com/baseUrl',
];
yield 'base URL with domain placeholder' => [
$uri = new Uri('https://s.test'),
@ -72,17 +78,11 @@ class NotFoundRedirectResolverTest extends TestCase
new NotFoundRedirectOptions(baseUrl: 'https://redirect-here.com/?domain={DOMAIN}'),
'https://redirect-here.com/?domain=s.test',
];
yield 'base URL without trailing slash' => [
$uri = new Uri(''),
self::notFoundType(ServerRequestFactory::fromGlobals()->withUri($uri)),
new NotFoundRedirectOptions(baseUrl: 'baseUrl'),
'baseUrl',
];
yield 'regular 404' => [
$uri = new Uri('/foo/bar'),
self::notFoundType(ServerRequestFactory::fromGlobals()->withUri($uri)),
new NotFoundRedirectOptions(regular404: 'regular404'),
'regular404',
new NotFoundRedirectOptions(regular404: 'https://example.com/regular404'),
'https://example.com/regular404',
];
yield 'regular 404 with path placeholder in query' => [
$uri = new Uri('/foo/bar'),
@ -101,8 +101,8 @@ class NotFoundRedirectResolverTest extends TestCase
yield 'invalid short URL' => [
new Uri('/foo'),
self::notFoundType(self::requestForRoute(RedirectAction::class)),
new NotFoundRedirectOptions(invalidShortUrl: 'invalidShortUrl'),
'invalidShortUrl',
new NotFoundRedirectOptions(invalidShortUrl: 'https://example.com/invalidShortUrl'),
'https://example.com/invalidShortUrl',
];
yield 'invalid short URL with path placeholder' => [
new Uri('/foo'),