Skip to content

Commit 760d411

Browse files
committed
Improve performance by direct config access
Revisiting #216
1 parent b2ad21c commit 760d411

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/Liquid/AbstractBlock.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ class AbstractBlock extends AbstractTag
4242

4343
private ?Regexp $variableRegexp;
4444

45+
public function __construct($markup, array &$tokens, ?FileSystem $fileSystem = null)
46+
{
47+
$this->config = &Liquid::$config;
48+
49+
$this->startRegexp = new Regexp('/^' . $this->config['TAG_START'] . '/');
50+
$this->tagRegexp = new Regexp('/^' . $this->config['TAG_START'] . $this->config['WHITESPACE_CONTROL'] . '?\s*(\w+)\s*(.*?)' . $this->config['WHITESPACE_CONTROL'] . '?' . $this->config['TAG_END'] . '$/s');
51+
$this->variableStartRegexp = new Regexp('/^' . $this->config['VARIABLE_START'] . '/');
52+
$this->whitespaceControl = $this->config['WHITESPACE_CONTROL'];
53+
$this->variableRegexp = new Regexp('/^' . $this->config['VARIABLE_START'] . $this->config['WHITESPACE_CONTROL'] . '?(.*?)' . $this->config['WHITESPACE_CONTROL'] . '?' . $this->config['VARIABLE_END'] . '$/s');
54+
55+
parent::__construct($markup, $tokens, $fileSystem);
56+
}
57+
4558
/**
4659
* @return array
4760
*/
@@ -60,10 +73,6 @@ public function getNodelist()
6073
*/
6174
public function parse(array &$tokens)
6275
{
63-
// Constructor is not reliably called by subclasses, so we need to ensure these are set
64-
$this->startRegexp ??= new Regexp('/^' . Liquid::get('TAG_START') . '/');
65-
$this->tagRegexp ??= new Regexp('/^' . Liquid::get('TAG_START') . Liquid::get('WHITESPACE_CONTROL') . '?\s*(\w+)\s*(.*?)' . Liquid::get('WHITESPACE_CONTROL') . '?' . Liquid::get('TAG_END') . '$/s');
66-
$this->variableStartRegexp ??= new Regexp('/^' . Liquid::get('VARIABLE_START') . '/');
6776

6877
$startRegexp = $this->startRegexp;
6978
$tagRegexp = $this->tagRegexp;
@@ -132,7 +141,6 @@ public function parse(array &$tokens)
132141
*/
133142
protected function whitespaceHandler($token)
134143
{
135-
$this->whitespaceControl ??= Liquid::get('WHITESPACE_CONTROL');
136144

137145
/*
138146
* This assumes that TAG_START is always '{%', and a whitespace control indicator
@@ -274,7 +282,6 @@ private function blockName()
274282
*/
275283
private function createVariable($token)
276284
{
277-
$this->variableRegexp ??= new Regexp('/^' . Liquid::get('VARIABLE_START') . Liquid::get('WHITESPACE_CONTROL') . '?(.*?)' . Liquid::get('WHITESPACE_CONTROL') . '?' . Liquid::get('VARIABLE_END') . '$/s');
278285

279286
if ($this->variableRegexp->match($token)) {
280287
return new Variable($this->variableRegexp->matches[1]);

0 commit comments

Comments
 (0)