Fix compat with older versions of PHP

This commit is contained in:
Fabien Potencier
2024-04-28 20:55:06 +02:00
parent 84bff0643c
commit 70b2e3ca5d
3 changed files with 14 additions and 12 deletions
+1 -1
View File
@@ -131,7 +131,7 @@ class Environment
$this->setCache($options['cache']);
$this->extensionSet = new ExtensionSet();
$this->defaultRuntimeLoader = new FactoryRuntimeLoader([
EscaperRuntime::class => fn () => new EscaperRuntime($options['autoescape'], $this->charset),
EscaperRuntime::class => function () use ($options) { return new EscaperRuntime($options['autoescape'], $this->charset); },
]);
$this->addExtension(new CoreExtension());
+7 -6
View File
@@ -21,12 +21,13 @@ use Twig\TwigFilter;
final class EscaperExtension extends AbstractExtension
{
private ?Environment $environment = null;
private array $escapers = [];
private $environment;
private $escapers = [];
private $escaper;
public function __construct(
private EscaperRuntime $escaper,
) {
public function __construct(EscaperRuntime $escaper)
{
$this->escaper = $escaper;
}
public function getTokenParsers(): array
@@ -94,7 +95,7 @@ final class EscaperExtension extends AbstractExtension
{
trigger_deprecation('twig/twig', '3.10', 'The "%s()" method is deprecated, use the "Twig\Runtime\EscaperRuntime::setEscaper()" method instead (be warned that Environment is not passed anymore to the callable).', __METHOD__);
if (!$this->environment) {
if (!isset($this->environment)) {
throw new \LogicException('You must call setEnvironment() before calling setEscaper().');
}
+6 -5
View File
@@ -27,16 +27,17 @@ final class EscaperRuntime implements RuntimeExtensionInterface
/** @internal */
public $safeLookup = [];
private $charset;
/**
* @param string|false|callable $defaultStrategy An escaping strategy
*
* @see setDefaultStrategy()
*/
public function __construct(
$defaultStrategy = 'html',
private string $charset = 'UTF-8',
) {
public function __construct($defaultStrategy = 'html', $charset = 'UTF-8')
{
$this->setDefaultStrategy($defaultStrategy);
$this->charset = $charset;
}
/**
@@ -127,7 +128,7 @@ final class EscaperRuntime implements RuntimeExtensionInterface
*
* @throws RuntimeException
*/
public function escape($string, string $strategy = 'html', ?string $charset = null, bool $autoescape = false): mixed
public function escape($string, string $strategy = 'html', ?string $charset = null, bool $autoescape = false)
{
if ($autoescape && $string instanceof Markup) {
return $string;