Skip to content

Commit 7bee0a8

Browse files
committed
use deprecation catching error handler only when parsing Twig templates
1 parent 195f1f1 commit 7bee0a8

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

src/Symfony/Bridge/Twig/Validator/Constraints/TwigValidator.php

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,37 +45,43 @@ public function validate(mixed $value, Constraint $constraint): void
4545

4646
$value = (string) $value;
4747

48-
if (!$constraint->skipDeprecations) {
49-
$prevErrorHandler = set_error_handler(static function ($level, $message, $file, $line) use (&$prevErrorHandler) {
50-
if (\E_USER_DEPRECATED !== $level) {
51-
return $prevErrorHandler ? $prevErrorHandler($level, $message, $file, $line) : false;
52-
}
53-
54-
$templateLine = 0;
55-
if (preg_match('/ at line (\d+)[ .]/', $message, $matches)) {
56-
$templateLine = $matches[1];
57-
}
58-
59-
throw new Error($message, $templateLine);
60-
});
61-
}
62-
6348
$realLoader = $this->twig->getLoader();
6449
try {
6550
$temporaryLoader = new ArrayLoader([$value]);
6651
$this->twig->setLoader($temporaryLoader);
52+
53+
if (!$constraint->skipDeprecations) {
54+
$prevErrorHandler = set_error_handler(static function ($level, $message, $file, $line) use (&$prevErrorHandler) {
55+
if (\E_USER_DEPRECATED !== $level) {
56+
return $prevErrorHandler ? $prevErrorHandler($level, $message, $file, $line) : false;
57+
}
58+
59+
$templateLine = 0;
60+
if (preg_match('/ at line (\d+)[ .]/', $message, $matches)) {
61+
$templateLine = $matches[1];
62+
}
63+
64+
throw new Error($message, $templateLine);
65+
});
66+
}
67+
6768
$this->twig->parse($this->twig->tokenize(new Source($value, '')));
69+
70+
if (!$constraint->skipDeprecations) {
71+
restore_error_handler();
72+
}
6873
} catch (Error $e) {
74+
if (!$constraint->skipDeprecations) {
75+
restore_error_handler();
76+
}
77+
6978
$this->context->buildViolation($constraint->message)
7079
->setParameter('{{ error }}', $e->getMessage())
7180
->setParameter('{{ line }}', $e->getTemplateLine())
7281
->setCode(Twig::INVALID_TWIG_ERROR)
7382
->addViolation();
7483
} finally {
7584
$this->twig->setLoader($realLoader);
76-
if (!$constraint->skipDeprecations) {
77-
restore_error_handler();
78-
}
7985
}
8086
}
8187
}

0 commit comments

Comments
 (0)