mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-15 20:06:31 +00:00
Making the Lexer initialize itself lazily, to avoid loading the extension set early
This commit is contained in:
committed by
Fabien Potencier
parent
872646a70f
commit
2f7e868017
@@ -21,6 +21,8 @@ use Twig\Error\SyntaxError;
|
|||||||
*/
|
*/
|
||||||
class Lexer
|
class Lexer
|
||||||
{
|
{
|
||||||
|
private $isInitialized = false;
|
||||||
|
|
||||||
private $tokens;
|
private $tokens;
|
||||||
private $code;
|
private $code;
|
||||||
private $cursor;
|
private $cursor;
|
||||||
@@ -63,6 +65,15 @@ class Lexer
|
|||||||
'whitespace_line_chars' => ' \t\0\x0B',
|
'whitespace_line_chars' => ' \t\0\x0B',
|
||||||
'interpolation' => ['#{', '}'],
|
'interpolation' => ['#{', '}'],
|
||||||
], $options);
|
], $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
|
// 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 = [
|
$this->regexes = [
|
||||||
@@ -155,6 +166,8 @@ class Lexer
|
|||||||
|
|
||||||
public function tokenize(Source $source)
|
public function tokenize(Source $source)
|
||||||
{
|
{
|
||||||
|
$this->initialize();
|
||||||
|
|
||||||
$this->source = $source;
|
$this->source = $source;
|
||||||
$this->code = str_replace(["\r\n", "\r"], "\n", $source->getCode());
|
$this->code = str_replace(["\r\n", "\r"], "\n", $source->getCode());
|
||||||
$this->cursor = 0;
|
$this->cursor = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user