Skip to content

Commit

Permalink
Merge pull request #3336 from neos/task/3316-followup-merge-uri-helpers
Browse files Browse the repository at this point in the history
TASK: 3316 followup merge uri helpers
  • Loading branch information
kitsunet committed Mar 29, 2024
2 parents b2f093b + 112c3e3 commit 408854c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 89 deletions.
74 changes: 31 additions & 43 deletions Neos.Flow/Classes/Http/Helper/UriHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@
* source code.
*/

use Neos\Utility\Arrays;
use Psr\Http\Message\UriInterface;

/**
* Helper to extract information from Uris.
*/
abstract class UriHelper
final class UriHelper
{
// this class only has static helpers
private function __construct()
{
}

/**
* @var array
* @var array<string, int>
*/
private static $defaultPortsByScheme = [
'http' => 80,
Expand All @@ -35,43 +41,18 @@ abstract class UriHelper
'ldap' => 389,
];

/**
* Get the username component of the given Uri
*
* @param UriInterface $uri
* @return string If the URI had no username an empty string is returned.
*/
public static function getUsername(UriInterface $uri): string
{
$userInfo = explode(':', $uri->getUserInfo());
return (isset($userInfo[0]) ? $userInfo[0] : '');
}

/**
* Get the password component of the given Uri
*
* @param UriInterface $uri
* @return string If the URI had no password an empty string is returned.
*/
public static function getPassword(UriInterface $uri): string
{
$userInfo = explode(':', $uri->getUserInfo());

return (isset($userInfo[1]) ? $userInfo[1] : '');
}

/**
* Returns the path relative to the $baseUri
*
* @param UriInterface $baseUri The base URI to start from
* @param UriInterface $uri The URI in quesiton
* @param UriInterface $uri The URI in question
* @return string
*/
public static function getRelativePath(UriInterface $baseUri, UriInterface $uri): string
{
$baseUriString = (string)$baseUri;
$uriString = (string)$uri;
if (empty($baseUriString) || strpos($uriString, $baseUriString) !== 0) {
if (empty($baseUriString) || !str_starts_with($uriString, $baseUriString)) {
return '';
}

Expand All @@ -80,29 +61,36 @@ public static function getRelativePath(UriInterface $baseUri, UriInterface $uri)
}

/**
* Parses the URIs query string into an array of arguments
* Sets and replaces the query parameters.
*
* @param UriInterface $uri
* @return array
* @param array<string, mixed> $queryParameters
* @return UriInterface A new instance with the replaced query parameters.
*/
public static function parseQueryIntoArguments(UriInterface $uri): array
public static function uriWithQueryParameters(UriInterface $uri, array $queryParameters): UriInterface
{
$arguments = [];
parse_str($uri->getQuery(), $arguments);
return $arguments;
$query = http_build_query($queryParameters, '', '&');
return $uri->withQuery($query);
}

/**
* Returns an Uri object with the query string being generated from the array of arguments given
* Merges the additional query parameters recursively into the current query parameters.
*
* @param UriInterface $uri
* @param array $arguments
* @return UriInterface
* @param array<string, mixed> $queryParameters
* @return UriInterface A new instance with the additional query parameters.
*/
public static function uriWithArguments(UriInterface $uri, array $arguments): UriInterface
public static function uriWithAdditionalQueryParameters(UriInterface $uri, array $queryParameters): UriInterface
{
$query = http_build_query($arguments, '', '&', PHP_QUERY_RFC3986);
return $uri->withQuery($query);
if ($queryParameters === []) {
return $uri;
}
if ($uri->getQuery() === '') {
$mergedQueryParameters = $queryParameters;
} else {
$queryParametersFromUri = [];
parse_str($uri->getQuery(), $queryParametersFromUri);
$mergedQueryParameters = Arrays::arrayMergeRecursiveOverrule($queryParametersFromUri, $queryParameters);
}
return self::uriWithQueryParameters($uri, $mergedQueryParameters);
}

/**
Expand Down
37 changes: 0 additions & 37 deletions Neos.Flow/Classes/Http/UriHelper.php

This file was deleted.

9 changes: 5 additions & 4 deletions Neos.Flow/Classes/Mvc/Routing/Dto/UriConstraints.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Neos\Flow\Annotations as Flow;
use GuzzleHttp\Psr7\Uri;
use Neos\Flow\Http\Helper\UriHelper;
use Neos\Utility\Arrays;
use Psr\Http\Message\UriInterface;

/**
Expand Down Expand Up @@ -219,11 +218,13 @@ public function withQueryString(string $queryString): self
public function withAddedQueryValues(array $values): self
{
$newConstraints = $this->constraints;
$temporaryUriWithQuery = new Uri();
if (isset($this->constraints[self::CONSTRAINT_QUERY_STRING])) {
parse_str($this->constraints[self::CONSTRAINT_QUERY_STRING], $existingValues);
$values = Arrays::arrayMergeRecursiveOverrule($existingValues, $values);
$temporaryUriWithQuery = $temporaryUriWithQuery->withQuery($this->constraints[self::CONSTRAINT_QUERY_STRING]);
}
$newConstraints[self::CONSTRAINT_QUERY_STRING] = http_build_query($values, '', '&');
// temporary, otherwise empty, uri to satisfy and reuse helper
$temporaryUriWithQuery = UriHelper::uriWithAdditionalQueryParameters($temporaryUriWithQuery, $values);
$newConstraints[self::CONSTRAINT_QUERY_STRING] = $temporaryUriWithQuery->getQuery();
return new static($newConstraints);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Neos\Flow\Tests\Unit\Http;
namespace Neos\Flow\Tests\Unit\Http\Helper;

/*
* This file is part of the Neos.Flow package.
Expand All @@ -12,7 +12,7 @@
*/

use GuzzleHttp\Psr7\Uri;
use Neos\Flow\Http\UriHelper;
use Neos\Flow\Http\Helper\UriHelper;
use Neos\Flow\Tests\UnitTestCase;

class UriHelperTest extends UnitTestCase
Expand All @@ -22,7 +22,7 @@ public function specificationWithoutQueryParametersDontModifyTheUri()
{
self::assertEquals(
new Uri('http://localhost/index?param1=foo&param2[0]=bar'),
UriHelper::withAdditionalQueryParameters(new Uri('http://localhost/index?param1=foo&param2[0]=bar'), [])
UriHelper::uriWithAdditionalQueryParameters(new Uri('http://localhost/index?param1=foo&param2[0]=bar'), [])
);
}

Expand All @@ -31,7 +31,7 @@ public function queryParametersAddedToUriWithoutQueryParameters()
{
self::assertEquals(
new Uri('http://localhost/index?param=123'),
UriHelper::withAdditionalQueryParameters(new Uri('http://localhost/index'), ['param' => 123])
UriHelper::uriWithAdditionalQueryParameters(new Uri('http://localhost/index'), ['param' => 123])
);
}

Expand All @@ -40,7 +40,7 @@ public function nestedQueryParametersAreMergedCorrectly()
{
self::assertEquals(
new Uri('http://localhost/index?param1=foo&param2[a]=bar&param2[b]=huhu&param3=123'),
UriHelper::withAdditionalQueryParameters(
UriHelper::uriWithAdditionalQueryParameters(
new Uri('http://localhost/index?param1=foo&param2[a]=bar'),
[
'param2' => [
Expand All @@ -51,4 +51,13 @@ public function nestedQueryParametersAreMergedCorrectly()
)
);
}

/** @test */
public function additionalQueryParametersAreEncoded()
{
self::assertEquals(
new Uri('http://localhost/index?param=with+space&second=sp%C3%A4cial-chars%26stuff%3A'),
UriHelper::uriWithAdditionalQueryParameters(new Uri('http://localhost/index'), ['param' => 'with space', 'second' => 'späcial-chars&stuff:'])
);
}
}

0 comments on commit 408854c

Please sign in to comment.