Skip to content

Commit b608ff0

Browse files
authored
De-Couple twig templates from session and request objects (kevinpapst#250)
1 parent 2318a08 commit b608ff0

10 files changed

Lines changed: 28 additions & 18 deletions

config/services.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ services:
33
KevinPapst\TablerBundle\Twig\RuntimeExtension:
44
class: KevinPapst\TablerBundle\Twig\RuntimeExtension
55
arguments:
6+
- '@request_stack'
67
- '@event_dispatcher'
78
- '@tabler_bundle.context_helper'
89
- '%tabler_bundle.routes%'

src/Twig/RuntimeExtension.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use KevinPapst\TablerBundle\Helper\ContextHelper;
1616
use KevinPapst\TablerBundle\Model\MenuItemInterface;
1717
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
18-
use Symfony\Component\HttpFoundation\Request;
18+
use Symfony\Component\HttpFoundation\RequestStack;
1919
use Twig\Extension\RuntimeExtensionInterface;
2020

2121
final class RuntimeExtension implements RuntimeExtensionInterface
@@ -25,6 +25,7 @@ final class RuntimeExtension implements RuntimeExtensionInterface
2525
* @param array<string, string> $icons
2626
*/
2727
public function __construct(
28+
private readonly RequestStack $requestStack,
2829
private readonly EventDispatcherInterface $eventDispatcher,
2930
private readonly ContextHelper $helper,
3031
private readonly array $routes,
@@ -66,16 +67,19 @@ public function containerClass(string $class = ''): string
6667
}
6768

6869
/**
69-
* @param Request $request
70-
*
7170
* @return MenuItemInterface[]|null
7271
*/
73-
public function getMenu(Request $request): ?array
72+
public function getMenu(): ?array
7473
{
7574
if (!$this->eventDispatcher->hasListeners(MenuEvent::class)) {
7675
return null;
7776
}
7877

78+
$request = $this->requestStack->getCurrentRequest();
79+
if ($request === null) {
80+
return null;
81+
}
82+
7983
/** @var MenuEvent $event */
8084
$event = $this->eventDispatcher->dispatch(new MenuEvent($request));
8185

templates/components/flash.html.twig

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
{% endmacro %}
1515

1616
{% macro session_flash(close, use_raw, class, domain) %}
17-
{% if app.session.flashbag.peekAll|length > 0 %}
18-
{% for type, messages in app.session.flashbag.all %}
19-
{% for message in messages %}
20-
{{ _self.flash(type, message, close, use_raw, class, domain) }}
21-
{% endfor %}
17+
{{ _self.flashes(app.flashes, close, use_raw, class, domain) }}
18+
{% endmacro %}
19+
20+
{% macro flashes(flashes, close, use_raw, class, domain) %}
21+
{% for type, messages in flashes %}
22+
{% for message in messages %}
23+
{{ _self.flash(type, message, close, use_raw, class, domain) }}
2224
{% endfor %}
23-
{% endif %}
25+
{% endfor %}
2426
{% endmacro %}

templates/fullpage.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!doctype html{% block html_start %}{% endblock %}>
2-
<html lang="{{ app.request.locale }}"{% if tabler_bundle.isRightToLeft() %} dir="rtl"{% endif %} data-bs-theme="{{ tabler_theme() }}" data-bs-theme-base="{{ tabler_bundle.getThemeBase() }}" data-bs-theme-radius="{{ tabler_bundle.getThemeRadius() }}" data-bs-theme-primary="{{ tabler_bundle.getThemePrimary() }}">
2+
<html lang="{{ app.locale }}"{% if tabler_bundle.isRightToLeft() %} dir="rtl"{% endif %} data-bs-theme="{{ tabler_theme() }}" data-bs-theme-base="{{ tabler_bundle.getThemeBase() }}" data-bs-theme-radius="{{ tabler_bundle.getThemeRadius() }}" data-bs-theme-primary="{{ tabler_bundle.getThemePrimary() }}">
33
<head>
44
{% block head %}
55
{% include '@Tabler/includes/html_head.html.twig' %}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{% import "@Tabler/components/flash.html.twig" as macro %}
22

3-
{% if app.session and app.session.started and app.session.flashbag.peekAll|length > 0 %}
3+
{% set flashes = app.flashes %}
4+
{% if flashes|length > 0 %}
45
<div class="row">
56
<div class="col-sm-12">
6-
{{ macro.session_flash(true, false, '', 'flashmessages') }}
7+
{{ macro.flashes(flashes, true, false, '', 'flashmessages') }}
78
</div>
89
</div>
910
{% endif %}

templates/includes/menu.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{% if menu is not defined %}
2-
{% set menu = tabler_menu(app.request) %}
2+
{% set menu = tabler_menu() %}
33
{% endif %}
44

55
{% if menu is defined and menu is not null %}

templates/layout_horizontal.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ by adding this code to your own base template:
77
Enjoy your theme!
88
#}
99
<!doctype html{% block html_start %}{% endblock %}>
10-
<html lang="{{ app.request.locale }}"{% if tabler_bundle.isRightToLeft() %} dir="rtl"{% endif %} data-bs-theme="{{ tabler_theme() }}" data-bs-theme-base="{{ tabler_bundle.getThemeBase() }}" data-bs-theme-radius="{{ tabler_bundle.getThemeRadius() }}" data-bs-theme-primary="{{ tabler_bundle.getThemePrimary() }}">
10+
<html lang="{{ app.locale }}"{% if tabler_bundle.isRightToLeft() %} dir="rtl"{% endif %} data-bs-theme="{{ tabler_theme() }}" data-bs-theme-base="{{ tabler_bundle.getThemeBase() }}" data-bs-theme-radius="{{ tabler_bundle.getThemeRadius() }}" data-bs-theme-primary="{{ tabler_bundle.getThemePrimary() }}">
1111
<head>
1212
{% block head %}
1313
{% include '@Tabler/includes/html_head.html.twig' %}

templates/layout_vertical.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ by adding this code to your own base template:
77
Enjoy your theme!
88
#}
99
<!doctype html{% block html_start %}{% endblock %}>
10-
<html lang="{{ app.request.locale }}"{% if tabler_bundle.isRightToLeft() %} dir="rtl"{% endif %} data-bs-theme="{{ tabler_theme() }}" data-bs-theme-base="{{ tabler_bundle.getThemeBase() }}" data-bs-theme-radius="{{ tabler_bundle.getThemeRadius() }}" data-bs-theme-primary="{{ tabler_bundle.getThemePrimary() }}">
10+
<html lang="{{ app.locale }}"{% if tabler_bundle.isRightToLeft() %} dir="rtl"{% endif %} data-bs-theme="{{ tabler_theme() }}" data-bs-theme-base="{{ tabler_bundle.getThemeBase() }}" data-bs-theme-radius="{{ tabler_bundle.getThemeRadius() }}" data-bs-theme-primary="{{ tabler_bundle.getThemePrimary() }}">
1111
<head>
1212
{% block head %}
1313
{% include '@Tabler/includes/html_head.html.twig' %}

templates/security.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!doctype html{% block html_start %}{% endblock %}>
2-
<html lang="{{ app.request.locale }}"{% if tabler_bundle.isRightToLeft() %} dir="rtl"{% endif %} data-bs-theme="{{ tabler_theme() }}" data-bs-theme-base="{{ tabler_bundle.getThemeBase() }}" data-bs-theme-radius="{{ tabler_bundle.getThemeRadius() }}" data-bs-theme-primary="{{ tabler_bundle.getThemePrimary() }}">
2+
<html lang="{{ app.locale }}"{% if tabler_bundle.isRightToLeft() %} dir="rtl"{% endif %} data-bs-theme="{{ tabler_theme() }}" data-bs-theme-base="{{ tabler_bundle.getThemeBase() }}" data-bs-theme-radius="{{ tabler_bundle.getThemeRadius() }}" data-bs-theme-primary="{{ tabler_bundle.getThemePrimary() }}">
33
<head>
44
{% block head %}
55
{% include '@Tabler/includes/html_head.html.twig' %}

tests/Twig/RuntimeExtensionTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use KevinPapst\TablerBundle\Twig\RuntimeExtension;
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\EventDispatcher\EventDispatcher;
16+
use Symfony\Component\HttpFoundation\RequestStack;
1617

1718
/**
1819
* @covers \KevinPapst\TablerBundle\Twig\RuntimeExtension
@@ -41,8 +42,9 @@ private function getSut(array $options): RuntimeExtension
4142
];
4243

4344
$dispatcher = new EventDispatcher();
45+
$requestStack = new RequestStack();
4446

45-
return new RuntimeExtension($dispatcher, $contextHelper, $routes, $icons);
47+
return new RuntimeExtension($requestStack, $dispatcher, $contextHelper, $routes, $icons);
4648
}
4749

4850
public function testGetRouteByAlias(): void

0 commit comments

Comments
 (0)