diff --git a/CHANGELOG.md b/CHANGELOG.md index ee4e546..6b8f3eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. This projec to [Semantic Versioning] (http://semver.org/). For change log format, use [Keep a Changelog] (http://keepachangelog.com/). +## [2.4.0] - 2024-12-04 + +### Added + +- Twig `finalize_path()` function + ## [2.3.1] - 2024-12-04 ### Fixed diff --git a/src/Extension/RouterExtension.php b/src/Extension/RouterExtension.php index 10e5476..4f7591e 100644 --- a/src/Extension/RouterExtension.php +++ b/src/Extension/RouterExtension.php @@ -29,6 +29,7 @@ public function getFunctions(): array return [ new TwigFunction('path', [RouterRuntimeExtension::class, 'functionPath']), new TwigFunction('path_exists', [RouterRuntimeExtension::class, 'functionPathExists']), + new TwigFunction('finalize_path', [RouterRuntimeExtension::class, 'functionFinalizePath']), ]; } } \ No newline at end of file diff --git a/src/Extension/RouterRuntimeExtension.php b/src/Extension/RouterRuntimeExtension.php index c240a0b..acb8f21 100644 --- a/src/Extension/RouterRuntimeExtension.php +++ b/src/Extension/RouterRuntimeExtension.php @@ -41,7 +41,7 @@ public function functionPath(string $name, array|RouteAttributes $parameters = [ { try { return $this->router->generate($name, $parameters); - } catch (NotFoundException | RoutingException $exception) { + } catch (NotFoundException|RoutingException $exception) { throw new RuntimeError($exception->getMessage()); } catch (Exception $exception) { throw new RuntimeError('Routing treatment error', previous: $exception); @@ -63,10 +63,22 @@ public function functionPathExists(string $name, array $parameters = []): bool $this->router->generate($name, $parameters); return true; - } catch (NotFoundException | RoutingException) { + } catch (NotFoundException|RoutingException) { return false; } catch (Exception $exception) { throw new RuntimeError('Routing treatment error', previous: $exception); } } + + /** + * Function to finalize a path. + * + * @param string $path + * + * @return string + */ + public function functionFinalizePath(string $path): string + { + return $this->router->finalizePath($path); + } } \ No newline at end of file diff --git a/tests/Extension/RouterRuntimeExtensionTest.php b/tests/Extension/RouterRuntimeExtensionTest.php index a33c91d..6e10a5a 100644 --- a/tests/Extension/RouterRuntimeExtensionTest.php +++ b/tests/Extension/RouterRuntimeExtensionTest.php @@ -24,6 +24,7 @@ class RouterRuntimeExtensionTest extends TestCase protected function setUp(): void { + unset($_SERVER['HTTP_X_FORWARDED_PREFIX']); $this->router = new Router(); $this->router->addRoute( new Route('/', name: 'home'), @@ -74,4 +75,15 @@ public function testFunctionPathExists() $this->assertFalse($extensionRuntime->functionPathExists('a-route')); $this->assertFalse($extensionRuntime->functionPathExists('unknown-route')); } + + public function testFunctionFinalizePath() + { + $extensionRuntime = new RouterRuntimeExtension(new Router(['X-Forwarded-Prefix' => true])); + + $this->assertEquals('/path', $extensionRuntime->functionFinalizePath('/path')); + + $_SERVER['HTTP_X_FORWARDED_PREFIX'] = '/prefix'; + + $this->assertEquals('/prefix/path', $extensionRuntime->functionFinalizePath('/path')); + } }