Skip to content
Merged
Show file tree
Hide file tree
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
29 changes: 20 additions & 9 deletions src/LiveComponent/src/Util/UrlFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,8 @@ public function createFromPreviousAndProps(
return null;
}

// Make sure to handle only path and query
$previousUrl = $parsed['path'] ?? '';
if (isset($parsed['query'])) {
$previousUrl .= '?'.$parsed['query'];
}

try {
$newUrl = $this->createPath($previousUrl, $pathMappedProps);
$newUrl = $this->createPath($parsed['path'] ?? '', $pathMappedProps);
} catch (ResourceNotFoundException|MethodNotAllowedException|MissingMandatoryParametersException) {
return null;
}
Expand All @@ -60,10 +54,27 @@ public function createFromPreviousAndProps(

private function createPath(string $previousUrl, array $props): string
{
return $this->router->generate(
$this->router->match($previousUrl)['_route'] ?? '',
$newPath = $this->router->generate(
$this->matchRoute($previousUrl),
$props
);

return $newPath;
}

private function matchRoute(string $previousUrl): string
{
$context = $this->router->getContext();
$tmpContext = clone $context;
$tmpContext->setMethod('GET');
$this->router->setContext($tmpContext);
try {
$match = $this->router->match($previousUrl);
} finally {
$this->router->setContext($context);
}

return $match['_route'] ?? '';
}

private function replaceQueryString($url, array $props): string
Expand Down
2 changes: 1 addition & 1 deletion src/LiveComponent/tests/Fixtures/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ protected function configureRoutes(RoutingConfigurator $routes): void
$routes->add('homepage', '/')->controller('kernel::index');
$routes->add('alternate_live_route', '/alt/{_live_component}/{_live_action}')->defaults(['_live_action' => 'get']);
$routes->add('localized_route', '/locale/{_locale}/{_live_component}/{_live_action}')->defaults(['_live_action' => 'get']);
$routes->add('route_with_prop', '/route_with_prop/{pathProp}');
$routes->add('route_with_prop', '/route_with_prop/{pathProp}')->methods(['GET']);
$routes->add('route_with_alias_prop', '/route_with_alias_prop/{pathAlias}');
}
}
17 changes: 14 additions & 3 deletions src/LiveComponent/tests/Unit/Util/UrlFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouterInterface;
use Symfony\UX\LiveComponent\Util\UrlFactory;

Expand All @@ -40,10 +41,10 @@ public static function provideTestCreate(): \Generator

yield 'keep_url_with_query_parameters' => [
'input' => ['previousUrl' => 'https://symfony.com/foo/bar?prop1=val1&prop2=val2'],
'/foo/bar?prop1=val1&prop2=val2',
'expectedUrl' => '/foo/bar?prop1=val1&prop2=val2',
'routerStubData' => [
'previousUrl' => '/foo/bar?prop1=val1&prop2=val2',
'newUrl' => '/foo/bar?prop1=val1&prop2=val2',
'previousUrl' => '/foo/bar',
'newUrl' => '/foo/bar',
],
];

Expand All @@ -61,6 +62,10 @@ public static function provideTestCreate(): \Generator
'queryMappedProps' => ['prop1' => 'val1', 'prop2' => 'val2'],
],
'expectedUrl' => '/foo/bar?prop1=val1&prop3=oldValue&prop2=val2',
'routerStubData' => [
'previousUrl' => '/foo/bar',
'newUrl' => '/foo/bar',
],
];

yield 'add_path_parameters' => [
Expand Down Expand Up @@ -178,7 +183,13 @@ private function createRouterStub(
array $props = [],
): RouterInterface {
$matchedRoute = 'default';
$context = $this->createMock(RequestContext::class);
$router = $this->createMock(RouterInterface::class);
$router->expects(self::once())
->method('getContext')
->willReturn($context);
$router->expects(self::exactly(2))
->method('setContext');
$router->expects(self::once())
->method('match')
->with($previousUrl)
Expand Down
Loading