-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve performance #216
base: master
Are you sure you want to change the base?
Improve performance #216
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,16 +51,20 @@ | |
*/ | ||
public function parse(array &$tokens) | ||
{ | ||
$startRegexp = new Regexp('/^' . Liquid::get('TAG_START') . '/'); | ||
$tagRegexp = new Regexp('/^' . Liquid::get('TAG_START') . Liquid::get('WHITESPACE_CONTROL') . '?\s*(\w+)\s*(.*?)' . Liquid::get('WHITESPACE_CONTROL') . '?' . Liquid::get('TAG_END') . '$/s'); | ||
$variableStartRegexp = new Regexp('/^' . Liquid::get('VARIABLE_START') . '/'); | ||
$startRegexp = new Regexp('/^' . Liquid::$config['TAG_START'] . '/'); | ||
$tagRegexp = new Regexp('/^' . Liquid::$config['TAG_START'] . Liquid::$config['WHITESPACE_CONTROL'] . '?\s*(\w+)\s*(.*?)' . Liquid::$config['WHITESPACE_CONTROL'] . '?' . Liquid::$config['TAG_END'] . '$/s'); | ||
$variableStartRegexp = new Regexp('/^' . Liquid::$config['VARIABLE_START'] . '/'); | ||
|
||
$this->nodelist = array(); | ||
|
||
$tags = Template::getTags(); | ||
|
||
while (count($tokens)) { | ||
$token = array_shift($tokens); | ||
for ($i = 0, $n = count($tokens); $i < $n; $i++) { | ||
if ($tokens[$i] === null) { | ||
continue; | ||
} | ||
$token = $tokens[$i]; | ||
$tokens[$i] = null; | ||
|
||
if ($startRegexp->match($token)) { | ||
$this->whitespaceHandler($token); | ||
|
@@ -112,13 +116,13 @@ | |
* | ||
* @param string $token | ||
*/ | ||
protected function whitespaceHandler($token) | ||
Check warning on line 119 in src/Liquid/AbstractBlock.php GitHub Actions / PHP 8.2
|
||
{ | ||
/* | ||
* This assumes that TAG_START is always '{%', and a whitespace control indicator | ||
* is exactly one character long, on a third position. | ||
*/ | ||
if (mb_substr($token, 2, 1) === Liquid::get('WHITESPACE_CONTROL')) { | ||
if ($token[2] === Liquid::$config['WHITESPACE_CONTROL']) { | ||
$previousToken = end($this->nodelist); | ||
if (is_string($previousToken)) { // this can also be a tag or a variable | ||
$this->nodelist[key($this->nodelist)] = rtrim($previousToken); | ||
|
@@ -129,7 +133,7 @@ | |
* This assumes that TAG_END is always '%}', and a whitespace control indicator | ||
* is exactly one character long, on a third position from the end. | ||
*/ | ||
self::$trimWhitespace = mb_substr($token, -3, 1) === Liquid::get('WHITESPACE_CONTROL'); | ||
self::$trimWhitespace = $token[-3] === Liquid::$config['WHITESPACE_CONTROL']; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this requires bumping min php to 7.1 because of negative string offset https://wiki.php.net/rfc/negative-string-offsets There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We already require 7.4 min, so that's non-issue 🙂 |
||
} | ||
|
||
/** | ||
|
@@ -157,7 +161,7 @@ | |
$result = ''; | ||
|
||
foreach ($list as $token) { | ||
if (is_object($token) && method_exists($token, 'render')) { | ||
Check warning on line 164 in src/Liquid/AbstractBlock.php GitHub Actions / PHP 8.2
|
||
$value = $token->render($context); | ||
} else { | ||
$value = $token; | ||
|
@@ -185,7 +189,7 @@ | |
/** | ||
* An action to execute when the end tag is reached | ||
*/ | ||
protected function endTag() | ||
Check warning on line 192 in src/Liquid/AbstractBlock.php GitHub Actions / PHP 8.2
|
||
{ | ||
// Do nothing by default | ||
} | ||
|
@@ -203,9 +207,9 @@ | |
{ | ||
switch ($tag) { | ||
case 'else': | ||
throw new ParseException($this->blockName() . " does not expect else tag"); | ||
Check warning on line 210 in src/Liquid/AbstractBlock.php GitHub Actions / PHP 8.2
Check warning on line 210 in src/Liquid/AbstractBlock.php GitHub Actions / PHP 8.2
|
||
case 'end': | ||
throw new ParseException("'end' is not a valid delimiter for " . $this->blockName() . " tags. Use " . $this->blockDelimiter()); | ||
Check warning on line 212 in src/Liquid/AbstractBlock.php GitHub Actions / PHP 8.2
Check warning on line 212 in src/Liquid/AbstractBlock.php GitHub Actions / PHP 8.2
Check warning on line 212 in src/Liquid/AbstractBlock.php GitHub Actions / PHP 8.2
Check warning on line 212 in src/Liquid/AbstractBlock.php GitHub Actions / PHP 8.2
Check warning on line 212 in src/Liquid/AbstractBlock.php GitHub Actions / PHP 8.2
|
||
default: | ||
throw new ParseException("Unknown tag $tag"); | ||
} | ||
|
@@ -254,7 +258,7 @@ | |
*/ | ||
private function createVariable($token) | ||
{ | ||
$variableRegexp = new Regexp('/^' . Liquid::get('VARIABLE_START') . Liquid::get('WHITESPACE_CONTROL') . '?(.*?)' . Liquid::get('WHITESPACE_CONTROL') . '?' . Liquid::get('VARIABLE_END') . '$/s'); | ||
$variableRegexp = new Regexp('/^' . Liquid::$config['VARIABLE_START'] . Liquid::$config['WHITESPACE_CONTROL'] . '?(.*?)' . Liquid::$config['WHITESPACE_CONTROL'] . '?' . Liquid::$config['VARIABLE_END'] . '$/s'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think about making it an instance variable instead so we would fetch the constants only once? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I.e. |
||
if ($variableRegexp->match($token)) { | ||
return new Variable($variableRegexp->matches[1]); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.