Skip to content

Commit ea5767d

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

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

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

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,33 @@ 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);
67-
$this->twig->parse($this->twig->tokenize(new Source($value, '')));
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+
68+
try {
69+
$this->twig->parse($this->twig->tokenize(new Source($value, '')));
70+
} finally {
71+
if (!$constraint->skipDeprecations) {
72+
restore_error_handler();
73+
}
74+
}
6875
} catch (Error $e) {
6976
$this->context->buildViolation($constraint->message)
7077
->setParameter('{{ error }}', $e->getMessage())
@@ -73,9 +80,6 @@ public function validate(mixed $value, Constraint $constraint): void
7380
->addViolation();
7481
} finally {
7582
$this->twig->setLoader($realLoader);
76-
if (!$constraint->skipDeprecations) {
77-
restore_error_handler();
78-
}
7983
}
8084
}
8185
}

0 commit comments

Comments
 (0)