Skip to content

Commit

Permalink
Add format-suffix handling (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes authored Sep 11, 2023
1 parent be76fed commit 8ecc5e7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
4 changes: 4 additions & 0 deletions Controller/WebsiteRedirectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ public function redirect(Request $request, RedirectRouteInterface $redirectRoute

$queryString = http_build_query($request->query->all());

$requestFormat = $request->getRequestFormat(null);
$formatSuffix = $requestFormat ? ('.' . $requestFormat) : '';

$url = [
$redirectRoute->getTarget(),
$formatSuffix,
false === strpos($redirectRoute->getTarget(), '?') ? '?' : '&',
$queryString,
];
Expand Down
20 changes: 8 additions & 12 deletions GoneSubscriber/GoneEntitySubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Sulu\Bundle\RedirectBundle\Entity\RedirectRoute;
use Sulu\Bundle\RedirectBundle\Exception\RedirectRouteNotUniqueException;
use Sulu\Bundle\RedirectBundle\Manager\RedirectRouteManager;
use Sulu\Bundle\RouteBundle\Entity\RouteRepositoryInterface;
use Sulu\Bundle\RouteBundle\Model\RouteInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
Expand All @@ -29,14 +28,6 @@ class GoneEntitySubscriber implements EventSubscriber, ContainerAwareInterface
{
use ContainerAwareTrait;

/**
* @return RouteRepositoryInterface
*/
public function getRouteRepository()
{
return $this->container->get('sulu.repository.route');
}

/**
* {@inheritdoc}
*/
Expand All @@ -51,7 +42,8 @@ public function preRemove(LifecycleEventArgs $event): void
{
$route = $event->getObject();

if (!$route instanceof RouteInterface) {
$routeManager = $this->getRedirectRouteManager();
if (!$route instanceof RouteInterface || null === $routeManager) {
return;
}

Expand All @@ -61,17 +53,21 @@ public function preRemove(LifecycleEventArgs $event): void
$redirectRoute->setSource($route->getPath());

try {
$this->getRedirectRouteManager()->save($redirectRoute);
$routeManager->save($redirectRoute);
} catch (RedirectRouteNotUniqueException $exception) {
// do nothing when there already exists a redirect route
}
}

/**
* @return RedirectRouteManager
* @return RedirectRouteManager|null
*/
private function getRedirectRouteManager()
{
if (null === $this->container) {
return null;
}

return $this->container->get('sulu_redirect.redirect_route_manager');
}
}
3 changes: 2 additions & 1 deletion Routing/RedirectRouteProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ public function getRouteCollectionForRequest(Request $request): RouteCollection
// server encodes the url and symfony does not encode it
// symfony decodes this data here https://github.com/symfony/symfony/blob/v5.2.3/src/Symfony/Component/Routing/Matcher/UrlMatcher.php#L88
$pathInfo = rawurldecode($request->getPathInfo());
$path = \str_replace('.' . $request->getRequestFormat(), '', $pathInfo);
$host = $request->getHost();

$routeCollection = new RouteCollection();
if (!$redirectRoute = $this->redirectRouteRepository->findEnabledBySource($pathInfo, $host)) {
if (!$redirectRoute = $this->redirectRouteRepository->findEnabledBySource($path, $host)) {
return $routeCollection;
}

Expand Down
15 changes: 14 additions & 1 deletion Tests/Functional/Routing/RedirectRouteProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function testRoute(
string $source,
int $statusCode,
string $target = '',
string $targetUrl = '',
?string $sourceHost = null
) {
// setup models
Expand All @@ -61,7 +62,7 @@ public function testRoute(

if ($target) {
$this->assertInstanceOf(RedirectResponse::class, $response);
$this->assertSame($target, $response->getTargetUrl());
$this->assertSame($targetUrl, $response->getTargetUrl());
}
}

Expand All @@ -72,13 +73,15 @@ public function routeDataProvider(): \Generator
'/test-301',
301,
'/test2',
'/test2',
];

yield [
'/test-302',
'/test-302',
302,
'/test2',
'/test2',
];

yield [
Expand All @@ -92,6 +95,7 @@ public function routeDataProvider(): \Generator
'/test-domain-redirect',
301,
'/',
'/',
'with-domain.com',
];

Expand All @@ -100,6 +104,15 @@ public function routeDataProvider(): \Generator
'/test-emoticon-🎉',
301,
'/',
'/',
];

yield [
'/source.json', // browsers will encode the url and be provided this way to symfony getPathInfo
'/source',
301,
'/target',
'/target.json',
];
}
}

0 comments on commit 8ecc5e7

Please sign in to comment.