Skip to content

Commit

Permalink
Add twig finalize_path() function
Browse files Browse the repository at this point in the history
  • Loading branch information
ElGigi committed Dec 4, 2024
1 parent e55a2a2 commit 0213f86
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/Extension/RouterExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']),
];
}
}
16 changes: 14 additions & 2 deletions src/Extension/RouterRuntimeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}
12 changes: 12 additions & 0 deletions tests/Extension/RouterRuntimeExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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'));
}
}

0 comments on commit 0213f86

Please sign in to comment.