Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HttpFoundation] Feature: Introduce flashbag cache to Twig AppVariable #49042

Open
wants to merge 1 commit into
base: 6.3
Choose a base branch
from

Conversation

JoshuaBehrens
Copy link

Q A
Branch? 6.3 for features
Bug fix? no
New feature? yes
Deprecations? no
License MIT
Doc PR symfony/symfony-docs#...

So this change is coming from a PR at Shopware which shall help intercompatibility of flashbag usage in a multi vendor application when using plugins trying to access the flashbag content.

In Shopware you have a section in the checkout that renders flashbag entries containing error/warning/info messages:

https://github.com/shopware/platform/blob/d6c853ca3b6b167d61d70e6329f4fcface5c5245/src/Storefront/Resources/views/storefront/page/checkout/_page.html.twig#L12-L18

{% block base_flashbags_checkout %}
    <div class="flashbags">
        {% for type, messages in app.flashes %}
            {% sw_include '@Storefront/storefront/utilities/alert.html.twig' with { type: type, list: messages } %}
        {% endfor %}
    </div>
{% endblock %}

This happens not as early in the template as some like to want. The content is also not stored in a variable which makes it difficult to re-access the content.

So now I want to display the content in a different form for mobile users. For that I have to move it somewhere else, store it in a variable and override the block and use a stored variation of the flashbag:

{% extends ... %}

{% block other_block %}
    {% set myPluginFlashes = app.flashes %}
    {% set myPluginErrorMessages in myPluginFlashes.error|default([]) %}

    {% for error in myPluginErrorMessages %}
        {# Do some fancy mobile notification popup style stuff #}
    {% endfor %}
{% endblock %}

{% block base_flashbags_checkout %}
    <div class="flashbags">
        {% for type, messages in myPluginFlashes %}
            {% sw_include '@Storefront/storefront/utilities/alert.html.twig' with { type: type, list: messages } %}
        {% endfor %}
    </div>
{% endblock %}

Now imagine every Shopware extension does it this way. This will break easily and will not work well together.

In a perfect world I could just write:

{% extends ... %}

{% block other_block %}
    {% for error in app.flashes('error') %}
        {# Do some fancy mobile notification popup style stuff #}
    {% endfor %}
{% endblock %}

My thoughts/journey:
So therefore I just cache it in the flashbag. But wait. The flashbag is made to forget. But it has a peek method. I could use the peek instead of the get in the AppVariable. But then the flashbag never forgets what has been used. I could forward the flashbag peek to the AppVariable as new method. But then everyone has to use "the right method". To difficult to migrate to, still likely to break. Ok, then I make a cache in the AppVariable so it can forget what it was asked but keep it for the rest of the rendering.

I am not sure whether this is a feature or a bugfix. It does not really feel like a bugfix as it contains a breaking change. But really does not feel like a feature either.

TODOs I was not aware of at the time of writing:

please update src/**/CHANGELOG.md files --- Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry
symfony/symfony-docs#...

  • Always add tests and ensure they pass. (ok, yes I assumed to have to write a test)

@carsonbot
Copy link

Hey!

Thanks for your PR. You are targeting branch "6.3" but it seems your PR description refers to branch "6.3 for features".
Could you update the PR description or change target branch? This helps core maintainers a lot.

Cheers!

Carsonbot

@stof
Copy link
Member

stof commented Jan 20, 2023

this is definitely a feature, not a bugfix

src/Symfony/Bridge/Twig/AppVariable.php Outdated Show resolved Hide resolved
src/Symfony/Bridge/Twig/AppVariable.php Outdated Show resolved Hide resolved
@carsonbot carsonbot changed the title Feature: Introduce flashbag cache to Twig AppVariable [HttpFoundation] Feature: Introduce flashbag cache to Twig AppVariable Jan 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants