Skip to content

Commit

Permalink
Adapt to router 2.0 (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
rustamwin committed Nov 12, 2022
1 parent 32979e4 commit 164ef9e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@

- Enh #105: Raise the minimum PHP version to 8.0 (@xepozz, @rustamwin)
- Bug #107: Keep query string when generating from current route (@rustamwin)
- Enh #108: Add `$queryParameters` parameter to `UrlGenerator::generateFromCurrent()` method (@rustamwin)

## 1.1.1 June 28, 2022

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -20,7 +20,7 @@
"php": "^8.0",
"nikic/fast-route": "^1.3",
"psr/simple-cache": "^2.0|^3.0",
"yiisoft/router": "^1.1",
"yiisoft/router": "^2.0",
"psr/http-message": "^1.0",
"yiisoft/http": "^1.2"
},
Expand Down
7 changes: 4 additions & 3 deletions src/UrlGenerator.php
Expand Up @@ -114,7 +114,7 @@ public function generateAbsolute(
/**
* {@inheritdoc}
*/
public function generateFromCurrent(array $replacedArguments, string $fallbackRouteName = null): string
public function generateFromCurrent(array $replacedArguments, array $queryParameters = [], string $fallbackRouteName = null): string
{
if ($this->currentRoute === null || $this->currentRoute->getName() === null) {
if ($fallbackRouteName !== null) {
Expand All @@ -128,9 +128,10 @@ public function generateFromCurrent(array $replacedArguments, string $fallbackRo
throw new RuntimeException('Current route is not detected.');
}

$queryParameters = [];
if ($this->currentRoute->getUri() !== null) {
parse_str($this->currentRoute->getUri()->getQuery(), $queryParameters);
$currentQueryParameters = [];
parse_str($this->currentRoute->getUri()->getQuery(), $currentQueryParameters);
$queryParameters = array_merge($currentQueryParameters, $queryParameters);
}

/** @psalm-suppress PossiblyNullArgument Checked route name on null above. */
Expand Down
20 changes: 15 additions & 5 deletions tests/UrlGeneratorTest.php
Expand Up @@ -706,6 +706,15 @@ public function currentRouteArgumentsProvider(): array
['_locale', 'uz'],
['_locale' => 'ru'],
],
[
'http://example.com/en/home/index?test=1',
'/ru/home/index?test=2',
Route::get('/{_locale}/home/index')->name('index'),
['_locale' => 'en'],
['_locale', 'uz'],
['_locale' => 'ru'],
['test' => 2],
],
];
}

Expand All @@ -718,7 +727,8 @@ public function testGenerateFromCurrentWithArguments(
Route $route,
array $routeArguments,
array $defaultArgument,
array $replacedArguments
array $replacedArguments,
array $queryParameters = []
): void {
$request = new ServerRequest('GET', $uri);

Expand All @@ -730,7 +740,7 @@ public function testGenerateFromCurrentWithArguments(
$urlGenerator->setDefaultArgument($defaultArgument[0], $defaultArgument[1]);
}

$url = $urlGenerator->generateFromCurrent($replacedArguments);
$url = $urlGenerator->generateFromCurrent($replacedArguments, $queryParameters);

$this->assertEquals($expectedUrl, $url);
}
Expand All @@ -745,7 +755,7 @@ public function testGenerateFromCurrentWithFallbackRoute(): void
$urlGenerator = $this->createUrlGenerator([$route], $currentRoute);
$urlGenerator->setDefaultArgument('_locale', 'uz');

$url = $urlGenerator->generateFromCurrent(['_locale' => 'ru'], 'index');
$url = $urlGenerator->generateFromCurrent(['_locale' => 'ru'], fallbackRouteName: 'index');

$this->assertEquals('/ru/home/index', $url);
}
Expand All @@ -757,7 +767,7 @@ public function testGenerateFromCurrentWithFallbackRouteWithoutCurrentRoute(): v
$urlGenerator = $this->createUrlGenerator([$route], null);
$urlGenerator->setDefaultArgument('_locale', 'uz');

$url = $urlGenerator->generateFromCurrent(['_locale' => 'ru'], 'index');
$url = $urlGenerator->generateFromCurrent(['_locale' => 'ru'], fallbackRouteName: 'index');

$this->assertEquals('/ru/home/index', $url);
}
Expand All @@ -770,7 +780,7 @@ public function testGenerateFromCurrentWithFallbackRouteWithEmptyCurrentRoute():
$urlGenerator = $this->createUrlGenerator([$route], $currentRoute);
$urlGenerator->setDefaultArgument('_locale', 'uz');

$url = $urlGenerator->generateFromCurrent(['_locale' => 'ru'], 'index');
$url = $urlGenerator->generateFromCurrent(['_locale' => 'ru'], fallbackRouteName: 'index');

$this->assertEquals('/ru/home/index', $url);
}
Expand Down

0 comments on commit 164ef9e

Please sign in to comment.