From 2f7e868017acc698228446041a79ee786abf77bd Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Wed, 3 May 2023 06:40:00 -0400 Subject: [PATCH] Making the Lexer initialize itself lazily, to avoid loading the extension set early --- src/Lexer.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Lexer.php b/src/Lexer.php index edde9a7a0..78931d711 100644 --- a/src/Lexer.php +++ b/src/Lexer.php @@ -21,6 +21,8 @@ use Twig\Error\SyntaxError; */ class Lexer { + private $isInitialized = false; + private $tokens; private $code; private $cursor; @@ -63,6 +65,15 @@ class Lexer 'whitespace_line_chars' => ' \t\0\x0B', 'interpolation' => ['#{', '}'], ], $options); + } + + private function initialize() + { + if ($this->isInitialized) { + return; + } + + $this->isInitialized = true; // when PHP 7.3 is the min version, we will be able to remove the '#' part in preg_quote as it's part of the default $this->regexes = [ @@ -155,6 +166,8 @@ class Lexer public function tokenize(Source $source) { + $this->initialize(); + $this->source = $source; $this->code = str_replace(["\r\n", "\r"], "\n", $source->getCode()); $this->cursor = 0;