Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions system/src/Grav/Common/Twig/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ public function getFunctions()
new \Twig_SimpleFunction('repeat', [$this, 'repeatFunc']),
new \Twig_SimpleFunction('regex_replace', [$this, 'regexReplace']),
new \Twig_SimpleFunction('regex_filter', [$this, 'regexFilter']),
new \Twig_SimpleFunction('preg_match', [$this, 'pregMatch']),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cannot we just call the method directly:

new \Twig_SimpleFunction('preg_match', 'preg_match')

Copy link
Contributor Author

@thexmanxyz thexmanxyz Jan 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mahagr The preg_match code is taken directly from Gantry 5 in the form it is currently in this PR (without offset and flags parameter). TBH IDK if incompatibilities might occur if we have different method signatures. Same for the the preg_split but only with the flags parameter omitted. I decided to also omit the flags parameter as it was skipped for preg_match within Gantry5 as well.

Additionally preg_match in Gantry 5 does not act the same way for return values as the default PHP method. Hence we can only update this PR for preg_split to something like this:

new \Twig_SimpleFunction('preg_split', 'preg_split')

otherwise we might break existing code that relies on the Twig preg_match function. Let me know and I will push a commit with the updated code for preg_split.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mahagr you probably know best about the code in G5, but it seems that false return value is there for a reason in G5.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right. The preg_match() PHP method returns true/false by default and puts the matching strings into the third parameter. It looks like @newkind added this method back in 2015, so I guess it's being used in a few themes.

If this was a filter, I would agree on the returned value, but in the function, not sure. I guess that you cannot update values inside parameters in twig, so it would make sense to remove the third parameter altogether -- it's being returned anyway.

new \Twig_SimpleFunction('preg_split', [$this, 'pregSplit']),
new \Twig_SimpleFunction('string', [$this, 'stringFunc']),
new \Twig_SimpleFunction('url', [$this, 'urlFunc']),
new \Twig_SimpleFunction('json_decode', [$this, 'jsonDecodeFilter']),
Expand Down Expand Up @@ -1098,6 +1100,34 @@ public function regexFilter($array, $regex, $flags = 0)
return preg_grep($regex, $array, $flags);
}

/**
* Twig wrapper for PHP's preg_match method
*
* @param string $pattern the regex pattern to use for match
* @param string $subject the content to perform the match on
* @param array &$matches if given, the parameter is filled with the match results. The first array elements contains the text which matches the complete search pattern. The second element contains the text which matches the first bracketed subpattern and so on.
* @return mixed returns the matches if there is at least one match in the subject for a given pattern or false if not.
*/
public function pregMatch($pattern, $subject, &$matches = [])
{
preg_match($pattern, $subject, $matches);

return $matches ?: false;
}

/**
* Twig wrapper for PHP's preg_split method
*
* @param string $pattern the regex pattern to use for split
* @param string $subject the content to perform the split on
* @param int $limit the maximum possible splits for the given pattern
* @return array the resulting array after performing the split operation
*/
public function pregSplit($pattern, $subject, $limit = -1)
{
return preg_split($pattern, $subject, $limit);
}

/**
* redirect browser from twig
*
Expand Down