Skip to content

allow Twig 4 #58145

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

Merged
merged 1 commit into from
Sep 2, 2024
Merged

allow Twig 4 #58145

merged 1 commit into from
Sep 2, 2024

Conversation

xabbuh
Copy link
Member

@xabbuh xabbuh commented Sep 2, 2024

Q A
Branch? 7.2
Bug fix? no
New feature? yes
Deprecations? no
Issues
License MIT

@xabbuh
Copy link
Member Author

xabbuh commented Sep 2, 2024

looks like not all of our code is ready for Twig 4, I'm looking into it

@xabbuh
Copy link
Member Author

xabbuh commented Sep 2, 2024

accessing the key or value of an invalid iterator is a bug in Twig itself: twigphp/Twig#4260

@stof
Copy link
Member

stof commented Sep 2, 2024

Looks like we only have 2 issues in Symfony itself (not counting the Twig bug):

  • the DebugCommand needs to be updated as it cannot use Environment::getGlobals() anymore
  • TransNodeTest needs to be adapted for the new way Twig compiles variable access

@xabbuh xabbuh force-pushed the twig4 branch 2 times, most recently from 20f68c9 to b51699f Compare September 2, 2024 12:56
@@ -50,6 +51,10 @@ protected function getVariableGetterWithoutStrictCheck($name)

protected function getVariableGetterWithStrictCheck($name)
{
if (class_exists(LoopIterator::class)) {
return \sprintf('(array_key_exists("%1$s", $context) ? $context["%1$s"] : throw new RuntimeError(\'Variable "%1$s" does not exist.\', 0, $this->source))', $name);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest adding a comment to make it clear that the code path below is for Twig 3.x and older (allowing to clean this method when dropping support for Twig 3)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added one

@@ -218,7 +218,7 @@ private function displayGeneralText(SymfonyStyle $io, ?string $filter = null): v
$types = ['functions', 'filters', 'tests', 'globals'];
foreach ($types as $index => $type) {
$items = [];
foreach ($this->twig->{'get'.ucfirst($type)}() as $name => $entity) {
foreach ((new \ReflectionMethod(Environment::class, 'get' . ucfirst($type)))->invoke($this->twig) as $name => $entity) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to rely on the Twig public API instead ? Private methods of Twig are not covered by the BC promise.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we possibly could, but that would require to completely rewrite the command, for functions, filters and tests we also depend on internal methods of the Environment class

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I can revert the change from public to private for getGlobals? We will still use internal methods, but at least that works without using reflection.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that would be even better I guess

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have reverted the change here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FTR, getGlobals() is now also no longer internal since twigphp/Twig@194cced

@@ -50,6 +51,10 @@ protected function getVariableGetterWithoutStrictCheck($name)

protected function getVariableGetterWithStrictCheck($name)
{
if (class_exists(LoopIterator::class)) {
return \sprintf('(array_key_exists("%1$s", $context) ? $context["%1$s"] : throw new RuntimeError(\'Variable "%1$s" does not exist.\', 0, $this->source))', $name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest adding a comment to make it clear that the code path below is for Twig 3.x and older (allowing to clean this method when dropping support for Twig 3)

fabpot added a commit to twigphp/Twig that referenced this pull request Sep 2, 2024
…ached (xabbuh)

This PR was merged into the 4.x branch.

Discussion
----------

don't read current key and value when end of iterator is reached

see failures in symfony/symfony#58145

Commits
-------

12b4cdc don't read current key and value when end of iterator is reached
@xabbuh xabbuh force-pushed the twig4 branch 2 times, most recently from 7f965f4 to 38258f5 Compare September 2, 2024 14:39
@fabpot
Copy link
Member

fabpot commented Sep 2, 2024

Thank you @xabbuh.

@fabpot fabpot merged commit 8d8fbb0 into symfony:7.2 Sep 2, 2024
8 of 10 checks passed
@xabbuh xabbuh deleted the twig4 branch September 2, 2024 15:42
@xabbuh xabbuh mentioned this pull request Oct 23, 2024
nicolas-grekas added a commit that referenced this pull request Oct 23, 2024
This PR was merged into the 7.2 branch.

Discussion
----------

revert allowing Twig 4

| Q             | A
| ------------- | ---
| Branch?       | 7.2
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Issues        | reverts #58145
| License       | MIT

As Twig 4 will not be released before Symfony 7.2 we should not claim compatibility before a stable Twig 4 release.

Commits
-------

c9107ae revert allowing Twig 4
@fabpot fabpot mentioned this pull request Oct 27, 2024
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.

4 participants