Skip to content

Commit

Permalink
Merge pull request #3316 from mhsdesign/feature/neosFlowPsrUriWrapper…
Browse files Browse the repository at this point in the history
…Utility

FEATURE: introduce `Neos\Flow\Http\UriHelper` to work with query parameters
  • Loading branch information
kitsunet authored Mar 28, 2024
2 parents 4c64d9e + 95537db commit 9034039
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 193 deletions.
2 changes: 1 addition & 1 deletion Neos.Flow/Classes/Http/BaseUriProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class BaseUriProvider
/**
* Get the configured framework base URI.
*
* @return Uri|null
* @return UriInterface|null
*/
private function getConfiguredBaseUri(): ?UriInterface
{
Expand Down
37 changes: 37 additions & 0 deletions Neos.Flow/Classes/Http/UriHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Neos\Flow\Http;

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

final class UriHelper
{
// this class only has static helpers
private function __construct()
{
}

/**
* Merges recursively into the current {@see UriInterface::getQuery} these additional query parameters.
*
* @param array $queryParameters
* @return UriInterface A new instance with the additional query.
*/
public static function withAdditionalQueryParameters(UriInterface $uri, array $queryParameters): UriInterface
{
if ($queryParameters === []) {
return $uri;
}
if ($uri->getQuery() === '') {
$mergedQuery = $queryParameters;
} else {
$queryParametersFromUri = [];
parse_str($uri->getQuery(), $queryParametersFromUri);
$mergedQuery = Arrays::arrayMergeRecursiveOverrule($queryParametersFromUri, $queryParameters);
}
return $uri->withQuery(http_build_query($mergedQuery, '', '&'));
}
}
54 changes: 54 additions & 0 deletions Neos.Flow/Tests/Unit/Http/UriHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
namespace Neos\Flow\Tests\Unit\Http;

/*
* This file is part of the Neos.Flow package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

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

class UriHelperTest extends UnitTestCase
{
/** @test */
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'), [])
);
}

/** @test */
public function queryParametersAddedToUriWithoutQueryParameters()
{
self::assertEquals(
new Uri('http://localhost/index?param=123'),
UriHelper::withAdditionalQueryParameters(new Uri('http://localhost/index'), ['param' => 123])
);
}

/** @test */
public function nestedQueryParametersAreMergedCorrectly()
{
self::assertEquals(
new Uri('http://localhost/index?param1=foo&param2[a]=bar&param2[b]=huhu&param3=123'),
UriHelper::withAdditionalQueryParameters(
new Uri('http://localhost/index?param1=foo&param2[a]=bar'),
[
'param2' => [
'b' => 'huhu'
],
'param3' => 123,
]
)
);
}
}
191 changes: 0 additions & 191 deletions Neos.Flow/Tests/Unit/Http/UriTest.php

This file was deleted.

5 changes: 5 additions & 0 deletions Neos.Http.Factories/Classes/PsrHttpFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
use Psr\Http\Message\UploadedFileFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;

/**
* A factory that implements all interfaces of PSR 17
*
* This factory can be used to simply create Requests, Uris and Streams without having to inject the traits yourself.
*/
class PsrHttpFactory implements ServerRequestFactoryInterface, RequestFactoryInterface, ResponseFactoryInterface,
UriFactoryInterface, StreamFactoryInterface, UploadedFileFactoryInterface
{
Expand Down
1 change: 0 additions & 1 deletion Neos.Http.Factories/Classes/UriFactoryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
namespace Neos\Http\Factories;

use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\UriFactoryInterface;
use Psr\Http\Message\UriInterface;

/**
Expand Down

0 comments on commit 9034039

Please sign in to comment.