Ignore exceptions from undefined handlers when using the guard tag

This commit is contained in:
Fabien Potencier
2024-12-19 08:36:59 +01:00
parent f99eab8541
commit b53c639f7d
3 changed files with 28 additions and 1 deletions
+1
View File
@@ -1,5 +1,6 @@
# 3.18.0 (2024-XX-XX)
* Ignore `SyntaxError` exceptions from undefined handlers when using the `guard` tag
* Add a way to stream template rendering (`TemplateWrapper::stream()` and `TemplateWrapper::streamBlock()` )
# 3.17.1 (2024-12-12)
+5 -1
View File
@@ -33,7 +33,11 @@ final class GuardTokenParser extends AbstractTokenParser
$nameToken = $stream->expect(Token::NAME_TYPE);
$exists = null !== $this->parser->getEnvironment()->$method($nameToken->getValue());
try {
$exists = null !== $this->parser->getEnvironment()->$method($nameToken->getValue());
} catch (SyntaxError) {
$exists = false;
}
$stream->expect(Token::BLOCK_END_TYPE);
if ($exists) {
@@ -0,0 +1,22 @@
<?php
namespace Twig\Tests\TokenParser;
use PHPUnit\Framework\TestCase;
use Twig\Environment;
use Twig\Error\SyntaxError;
use Twig\Loader\ArrayLoader;
use Twig\Parser;
use Twig\Source;
class GuardTokenParserTest extends TestCase
{
public function testUndefinedHandlers()
{
$this->expectNotToPerformAssertions();
$env = new Environment(new ArrayLoader(), ['cache' => false, 'autoescape' => false]);
$env->registerUndefinedFunctionCallback(fn ($name) => throw new SyntaxError('boom.'));
(new Parser($env))->parse($env->tokenize(new Source('{% guard function boom %}{% endguard %}', '')));
}
}