From 760d4111442fac8c960ae9fcb3ac7ec05c6cf23d Mon Sep 17 00:00:00 2001 From: Alexey Kopytko Date: Sat, 31 May 2025 20:37:41 +0900 Subject: [PATCH] Improve performance by direct config access Revisiting #216 --- src/Liquid/AbstractBlock.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/Liquid/AbstractBlock.php b/src/Liquid/AbstractBlock.php index 45afee0..957814f 100644 --- a/src/Liquid/AbstractBlock.php +++ b/src/Liquid/AbstractBlock.php @@ -42,6 +42,19 @@ class AbstractBlock extends AbstractTag private ?Regexp $variableRegexp; + public function __construct($markup, array &$tokens, ?FileSystem $fileSystem = null) + { + $this->config = &Liquid::$config; + + $this->startRegexp = new Regexp('/^' . $this->config['TAG_START'] . '/'); + $this->tagRegexp = new Regexp('/^' . $this->config['TAG_START'] . $this->config['WHITESPACE_CONTROL'] . '?\s*(\w+)\s*(.*?)' . $this->config['WHITESPACE_CONTROL'] . '?' . $this->config['TAG_END'] . '$/s'); + $this->variableStartRegexp = new Regexp('/^' . $this->config['VARIABLE_START'] . '/'); + $this->whitespaceControl = $this->config['WHITESPACE_CONTROL']; + $this->variableRegexp = new Regexp('/^' . $this->config['VARIABLE_START'] . $this->config['WHITESPACE_CONTROL'] . '?(.*?)' . $this->config['WHITESPACE_CONTROL'] . '?' . $this->config['VARIABLE_END'] . '$/s'); + + parent::__construct($markup, $tokens, $fileSystem); + } + /** * @return array */ @@ -60,10 +73,6 @@ public function getNodelist() */ public function parse(array &$tokens) { - // Constructor is not reliably called by subclasses, so we need to ensure these are set - $this->startRegexp ??= new Regexp('/^' . Liquid::get('TAG_START') . '/'); - $this->tagRegexp ??= new Regexp('/^' . Liquid::get('TAG_START') . Liquid::get('WHITESPACE_CONTROL') . '?\s*(\w+)\s*(.*?)' . Liquid::get('WHITESPACE_CONTROL') . '?' . Liquid::get('TAG_END') . '$/s'); - $this->variableStartRegexp ??= new Regexp('/^' . Liquid::get('VARIABLE_START') . '/'); $startRegexp = $this->startRegexp; $tagRegexp = $this->tagRegexp; @@ -132,7 +141,6 @@ public function parse(array &$tokens) */ protected function whitespaceHandler($token) { - $this->whitespaceControl ??= Liquid::get('WHITESPACE_CONTROL'); /* * This assumes that TAG_START is always '{%', and a whitespace control indicator @@ -274,7 +282,6 @@ private function blockName() */ private function createVariable($token) { - $this->variableRegexp ??= new Regexp('/^' . Liquid::get('VARIABLE_START') . Liquid::get('WHITESPACE_CONTROL') . '?(.*?)' . Liquid::get('WHITESPACE_CONTROL') . '?' . Liquid::get('VARIABLE_END') . '$/s'); if ($this->variableRegexp->match($token)) { return new Variable($this->variableRegexp->matches[1]);