Skip to content

Commit 0213f86

Browse files
committed
Add twig finalize_path() function
1 parent e55a2a2 commit 0213f86

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. This projec
44
to [Semantic Versioning] (http://semver.org/). For change log format,
55
use [Keep a Changelog] (http://keepachangelog.com/).
66

7+
## [2.4.0] - 2024-12-04
8+
9+
### Added
10+
11+
- Twig `finalize_path()` function
12+
713
## [2.3.1] - 2024-12-04
814

915
### Fixed

src/Extension/RouterExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public function getFunctions(): array
2929
return [
3030
new TwigFunction('path', [RouterRuntimeExtension::class, 'functionPath']),
3131
new TwigFunction('path_exists', [RouterRuntimeExtension::class, 'functionPathExists']),
32+
new TwigFunction('finalize_path', [RouterRuntimeExtension::class, 'functionFinalizePath']),
3233
];
3334
}
3435
}

src/Extension/RouterRuntimeExtension.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function functionPath(string $name, array|RouteAttributes $parameters = [
4141
{
4242
try {
4343
return $this->router->generate($name, $parameters);
44-
} catch (NotFoundException | RoutingException $exception) {
44+
} catch (NotFoundException|RoutingException $exception) {
4545
throw new RuntimeError($exception->getMessage());
4646
} catch (Exception $exception) {
4747
throw new RuntimeError('Routing treatment error', previous: $exception);
@@ -63,10 +63,22 @@ public function functionPathExists(string $name, array $parameters = []): bool
6363
$this->router->generate($name, $parameters);
6464

6565
return true;
66-
} catch (NotFoundException | RoutingException) {
66+
} catch (NotFoundException|RoutingException) {
6767
return false;
6868
} catch (Exception $exception) {
6969
throw new RuntimeError('Routing treatment error', previous: $exception);
7070
}
7171
}
72+
73+
/**
74+
* Function to finalize a path.
75+
*
76+
* @param string $path
77+
*
78+
* @return string
79+
*/
80+
public function functionFinalizePath(string $path): string
81+
{
82+
return $this->router->finalizePath($path);
83+
}
7284
}

tests/Extension/RouterRuntimeExtensionTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class RouterRuntimeExtensionTest extends TestCase
2424

2525
protected function setUp(): void
2626
{
27+
unset($_SERVER['HTTP_X_FORWARDED_PREFIX']);
2728
$this->router = new Router();
2829
$this->router->addRoute(
2930
new Route('/', name: 'home'),
@@ -74,4 +75,15 @@ public function testFunctionPathExists()
7475
$this->assertFalse($extensionRuntime->functionPathExists('a-route'));
7576
$this->assertFalse($extensionRuntime->functionPathExists('unknown-route'));
7677
}
78+
79+
public function testFunctionFinalizePath()
80+
{
81+
$extensionRuntime = new RouterRuntimeExtension(new Router(['X-Forwarded-Prefix' => true]));
82+
83+
$this->assertEquals('/path', $extensionRuntime->functionFinalizePath('/path'));
84+
85+
$_SERVER['HTTP_X_FORWARDED_PREFIX'] = '/prefix';
86+
87+
$this->assertEquals('/prefix/path', $extensionRuntime->functionFinalizePath('/path'));
88+
}
7789
}

0 commit comments

Comments
 (0)