From 4cb4bc63c78dfae35a5a4a3b842fac58c7fc9f42 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Mon, 22 Jun 2015 17:21:08 +0200 Subject: [PATCH] GraphiteQuery: allow to extract variable names --- library/Graphite/GraphiteQuery.php | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/library/Graphite/GraphiteQuery.php b/library/Graphite/GraphiteQuery.php index fda0ecd..2f171e9 100644 --- a/library/Graphite/GraphiteQuery.php +++ b/library/Graphite/GraphiteQuery.php @@ -14,6 +14,8 @@ class GraphiteQuery protected $searchPattern; + protected $variablePattern = '/\$(\w+)/'; + public function __construct(GraphiteWeb $web) { $this->web = $web; @@ -112,15 +114,22 @@ class GraphiteQuery return $this->replaceRemainingVariables($this->search); } + public function extractVariableNames($pattern) + { + if (preg_match_all($this->variablePattern, $pattern, $m)) { + return $m[1]; + } + + return array(); + } + protected function extractVars($string, $pattern) { - $regexVar = '/\$(\w+)/'; $vars = array(); + $varnames = $this->extractVariableNames($pattern); - if (preg_match_all($regexVar, $pattern, $m)) { - $varnames = $m[1]; - - $parts = preg_split($regexVar, $pattern); + if (! empty($varnames)) { + $parts = preg_split($this->variablePattern, $pattern); foreach ($parts as $key => $val) { $parts[$key] = preg_quote($val, '/'); }