mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-07 16:06:53 +00:00
Ignore exceptions from undefined handlers when using the guard tag
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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 %}', '')));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user