File tree Expand file tree Collapse file tree 3 files changed +56
-0
lines changed
Expand file tree Collapse file tree 3 files changed +56
-0
lines changed Original file line number Diff line number Diff line change @@ -158,4 +158,25 @@ public function getFlashes(string|array $types = null): array
158158
159159 return $ result ;
160160 }
161+
162+ public function getCurrent_Route (): ?string
163+ {
164+ if (!isset ($ this ->requestStack )) {
165+ throw new \RuntimeException ('The "app.current_route" variable is not available. ' );
166+ }
167+
168+ return $ this ->getRequest ()?->attributes->get ('_route ' );
169+ }
170+
171+ /**
172+ * @return array<string, mixed>
173+ */
174+ public function getCurrent_Route_Parameters (): array
175+ {
176+ if (!isset ($ this ->requestStack )) {
177+ throw new \RuntimeException ('The "app.current_route_parameters" variable is not available. ' );
178+ }
179+
180+ return $ this ->getRequest ()?->attributes->get ('_route_params ' ) ?? [];
181+ }
161182}
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ CHANGELOG
77 * Add ` form_label_content ` and ` form_help_content ` block to form themes
88 * Add ` #[Template()] ` to describe how to render arrays returned by controllers
99 * Add support for toggle buttons in Bootstrap 5 form theme
10+ * Add ` app.current_route ` and ` app.current_route_parameters ` variables
1011
11126.1
1213---
Original file line number Diff line number Diff line change @@ -228,6 +228,40 @@ public function testGetFlashes()
228228 );
229229 }
230230
231+ public function testGetCurrentRoute ()
232+ {
233+ $ this ->setRequestStack (new Request (attributes: ['_route ' => 'some_route ' ]));
234+
235+ $ this ->assertSame ('some_route ' , $ this ->appVariable ->getCurrent_Route ());
236+ }
237+
238+ public function testGetCurrentRouteWithRequestStackNotSet ()
239+ {
240+ $ this ->expectException (\RuntimeException::class);
241+ $ this ->appVariable ->getCurrent_Route ();
242+ }
243+
244+ public function testGetCurrentRouteParameters ()
245+ {
246+ $ routeParams = ['some_param ' => true ];
247+ $ this ->setRequestStack (new Request (attributes: ['_route_params ' => $ routeParams ]));
248+
249+ $ this ->assertSame ($ routeParams , $ this ->appVariable ->getCurrent_Route_Parameters ());
250+ }
251+
252+ public function testGetCurrentRouteParametersWithoutAttribute ()
253+ {
254+ $ this ->setRequestStack (new Request ());
255+
256+ $ this ->assertSame ([], $ this ->appVariable ->getCurrent_Route_Parameters ());
257+ }
258+
259+ public function testGetCurrentRouteParametersWithRequestStackNotSet ()
260+ {
261+ $ this ->expectException (\RuntimeException::class);
262+ $ this ->appVariable ->getCurrent_Route_Parameters ();
263+ }
264+
231265 protected function setRequestStack ($ request )
232266 {
233267 $ requestStackMock = $ this ->createMock (RequestStack::class);
You can’t perform that action at this time.
0 commit comments