From 70b2e3ca5d26fd286b71360259aff18ed0f8aad6 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 28 Apr 2024 20:55:06 +0200 Subject: [PATCH] Fix compat with older versions of PHP --- src/Environment.php | 2 +- src/Extension/EscaperExtension.php | 13 +++++++------ src/Runtime/EscaperRuntime.php | 11 ++++++----- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/Environment.php b/src/Environment.php index 5e0e0c2d2..bb2a257d1 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -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()); diff --git a/src/Extension/EscaperExtension.php b/src/Extension/EscaperExtension.php index 7da6854d1..8167f92bb 100644 --- a/src/Extension/EscaperExtension.php +++ b/src/Extension/EscaperExtension.php @@ -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().'); } diff --git a/src/Runtime/EscaperRuntime.php b/src/Runtime/EscaperRuntime.php index 6b55c7fb3..0124bfe24 100644 --- a/src/Runtime/EscaperRuntime.php +++ b/src/Runtime/EscaperRuntime.php @@ -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;