Merge branch '2.x' into 3.x

* 2.x:
  Making the Lexer initialize itself lazily, to avoid loading the extension set early
This commit is contained in:
Fabien Potencier
2023-05-03 19:47:35 +02:00
+13
View File
@@ -19,6 +19,8 @@ use Twig\Error\SyntaxError;
*/
class Lexer
{
private $isInitialized = false;
private $tokens;
private $code;
private $cursor;
@@ -61,6 +63,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 = [
@@ -153,6 +164,8 @@ class Lexer
public function tokenize(Source $source): TokenStream
{
$this->initialize();
$this->source = $source;
$this->code = str_replace(["\r\n", "\r"], "\n", $source->getCode());
$this->cursor = 0;