Skip to content

Commit

Permalink
bug #3769 Updates CoreExtension::twig_constant to check for definitio…
Browse files Browse the repository at this point in the history
…n first to avoid hard crash (Alex Henderson-Roche)

This PR was merged into the 2.x branch.

Discussion
----------

Updates CoreExtension::twig_constant to check for definition first to avoid hard crash

The behaviour of PHP's constant() method has been updated after php/php-src#9905 was accepted and fixed in PHP 8.1 (and previously changed post PHP 8.0) . This PR prevents a fatal error in the case a constant is supplied that doesn't actually exist.

Commits
-------

56b3122 Updates CoreExtension::twig_constant to check for definition first to avoid hard crash
  • Loading branch information
fabpot committed Dec 26, 2022
2 parents dec3069 + 56b3122 commit 6c2bb9f
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Extension/CoreExtension.php
Expand Up @@ -1329,6 +1329,10 @@ function twig_constant($constant, $object = null)
$constant = \get_class($object).'::'.$constant;
}

if (!\defined($constant)) {
throw new RuntimeError(sprintf('Constant "%s" is undefined.', $constant));
}

return \constant($constant);
}

Expand Down

0 comments on commit 6c2bb9f

Please sign in to comment.