From 67aae53e9fd36e457017cd1a0408c06d89d1c856 Mon Sep 17 00:00:00 2001 From: Jules Pietri Date: Sat, 10 Sep 2022 10:12:35 +0200 Subject: [PATCH] [TwigBridge] Expose current route in `AppVariable` --- src/Symfony/Bridge/Twig/AppVariable.php | 21 ++++++++++++ src/Symfony/Bridge/Twig/CHANGELOG.md | 1 + .../Bridge/Twig/Tests/AppVariableTest.php | 34 +++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/src/Symfony/Bridge/Twig/AppVariable.php b/src/Symfony/Bridge/Twig/AppVariable.php index 43d6e3ffc9056..d342f9fbaf03f 100644 --- a/src/Symfony/Bridge/Twig/AppVariable.php +++ b/src/Symfony/Bridge/Twig/AppVariable.php @@ -158,4 +158,25 @@ public function getFlashes(string|array $types = null): array return $result; } + + public function getCurrent_Route(): ?string + { + if (!isset($this->requestStack)) { + throw new \RuntimeException('The "app.current_route" variable is not available.'); + } + + return $this->getRequest()?->attributes->get('_route'); + } + + /** + * @return array + */ + public function getCurrent_Route_Parameters(): array + { + if (!isset($this->requestStack)) { + throw new \RuntimeException('The "app.current_route_parameters" variable is not available.'); + } + + return $this->getRequest()?->attributes->get('_route_params') ?? []; + } } diff --git a/src/Symfony/Bridge/Twig/CHANGELOG.md b/src/Symfony/Bridge/Twig/CHANGELOG.md index 1a58330efa509..8edc9b80190b4 100644 --- a/src/Symfony/Bridge/Twig/CHANGELOG.md +++ b/src/Symfony/Bridge/Twig/CHANGELOG.md @@ -7,6 +7,7 @@ CHANGELOG * Add `form_label_content` and `form_help_content` block to form themes * Add `#[Template()]` to describe how to render arrays returned by controllers * Add support for toggle buttons in Bootstrap 5 form theme + * Add `app.current_route` and `app.current_route_parameters` variables 6.1 --- diff --git a/src/Symfony/Bridge/Twig/Tests/AppVariableTest.php b/src/Symfony/Bridge/Twig/Tests/AppVariableTest.php index 6ba4ca77e51c4..01db44528a0d4 100644 --- a/src/Symfony/Bridge/Twig/Tests/AppVariableTest.php +++ b/src/Symfony/Bridge/Twig/Tests/AppVariableTest.php @@ -228,6 +228,40 @@ public function testGetFlashes() ); } + public function testGetCurrentRoute() + { + $this->setRequestStack(new Request(attributes: ['_route' => 'some_route'])); + + $this->assertSame('some_route', $this->appVariable->getCurrent_Route()); + } + + public function testGetCurrentRouteWithRequestStackNotSet() + { + $this->expectException(\RuntimeException::class); + $this->appVariable->getCurrent_Route(); + } + + public function testGetCurrentRouteParameters() + { + $routeParams = ['some_param' => true]; + $this->setRequestStack(new Request(attributes: ['_route_params' => $routeParams])); + + $this->assertSame($routeParams, $this->appVariable->getCurrent_Route_Parameters()); + } + + public function testGetCurrentRouteParametersWithoutAttribute() + { + $this->setRequestStack(new Request()); + + $this->assertSame([], $this->appVariable->getCurrent_Route_Parameters()); + } + + public function testGetCurrentRouteParametersWithRequestStackNotSet() + { + $this->expectException(\RuntimeException::class); + $this->appVariable->getCurrent_Route_Parameters(); + } + protected function setRequestStack($request) { $requestStackMock = $this->createMock(RequestStack::class);