|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony CMF package. |
| 5 | + * |
| 6 | + * (c) Symfony CMF |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Cmf\Bundle\RoutingBundle\Routing; |
| 13 | + |
| 14 | +use Symfony\Component\HttpFoundation\Request; |
| 15 | +use Symfony\Component\Routing\Exception\ExceptionInterface; |
| 16 | +use Symfony\Component\Routing\Exception\ResourceNotFoundException; |
| 17 | +use Symfony\Component\Routing\Matcher\RequestMatcherInterface; |
| 18 | +use Symfony\Component\Routing\RequestContext; |
| 19 | + |
| 20 | +/** |
| 21 | + * Reproduce the behaviour of the Symfony router to redirect paths with a trailing slash if necessary. |
| 22 | + * |
| 23 | + * The logic is taken from Symfony\Component\Routing\Matcher\RedirectableUrlMatcher and Symfony\Bundle\FrameworkBundle\Routing\RedirectableCompiledUrlMatcher |
| 24 | + * |
| 25 | + * @see https://symfony.com/doc/4.1/routing.html#routing-trailing-slash-redirection |
| 26 | + */ |
| 27 | +class RedirectableRequestMatcher implements RequestMatcherInterface |
| 28 | +{ |
| 29 | + private RequestMatcherInterface $decorated; |
| 30 | + |
| 31 | + private RequestContext $requestContext; |
| 32 | + |
| 33 | + public function __construct(RequestMatcherInterface $decorated, RequestContext $requestContext) |
| 34 | + { |
| 35 | + $this->decorated = $decorated; |
| 36 | + $this->requestContext = $requestContext; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * If the matcher can not find a match, |
| 41 | + * it will try to add or remove a trailing slash to the path and match again. |
| 42 | + */ |
| 43 | + public function matchRequest(Request $request) |
| 44 | + { |
| 45 | + try { |
| 46 | + return $this->decorated->matchRequest($request); |
| 47 | + } catch (ResourceNotFoundException $e) { |
| 48 | + if (!\in_array($request->getMethod(), ['HEAD', 'GET'], true)) { |
| 49 | + throw $e; |
| 50 | + } |
| 51 | + |
| 52 | + $pathinfo = $request->getPathInfo(); |
| 53 | + |
| 54 | + if ('/' === $trimmedPathinfo = rtrim($pathinfo, '/') ?: '/') { |
| 55 | + throw $e; |
| 56 | + } |
| 57 | + |
| 58 | + $pathinfo = $trimmedPathinfo === $pathinfo ? $pathinfo.'/' : $trimmedPathinfo; |
| 59 | + |
| 60 | + try { |
| 61 | + $parameters = $this->decorated->matchRequest($this->rebuildRequest($request, $pathinfo)); |
| 62 | + } catch (ExceptionInterface $e2) { |
| 63 | + throw $e; |
| 64 | + } |
| 65 | + |
| 66 | + return $this->redirect($pathinfo, $parameters['_route'] ?? null, $this->requestContext->getScheme()) + $parameters; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private function redirect(string $path, string $route, string $scheme = null): array |
| 71 | + { |
| 72 | + return [ |
| 73 | + '_controller' => 'Symfony\\Bundle\\FrameworkBundle\\Controller\\RedirectController::urlRedirectAction', |
| 74 | + 'path' => $path, |
| 75 | + 'permanent' => true, |
| 76 | + 'scheme' => $scheme, |
| 77 | + 'httpPort' => $this->requestContext->getHttpPort(), |
| 78 | + 'httpsPort' => $this->requestContext->getHttpsPort(), |
| 79 | + '_route' => $route, |
| 80 | + ]; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Return a duplicated version of $request with the new $newpath as request_uri. |
| 85 | + */ |
| 86 | + private function rebuildRequest(Request $request, string $newPath): Request |
| 87 | + { |
| 88 | + $server = $request->server->all(); |
| 89 | + $server['REQUEST_URI'] = $newPath; |
| 90 | + |
| 91 | + return $request->duplicate(null, null, null, null, null, $server); |
| 92 | + } |
| 93 | +} |
0 commit comments