mirror of
https://github.com/Icinga/icingaweb2-module-graphite.git
synced 2026-04-20 22:01:40 -04:00
38 lines
938 B
PHP
38 lines
938 B
PHP
<?php
|
|
|
|
namespace Icinga\Module\Graphite;
|
|
|
|
class GraphiteUtil
|
|
{
|
|
protected static $variablePattern = '/\$(\w+)/';
|
|
|
|
public static function extractVariableNames($pattern)
|
|
{
|
|
if (preg_match_all(self::$variablePattern, $pattern, $m)) {
|
|
return $m[1];
|
|
}
|
|
|
|
return array();
|
|
}
|
|
|
|
public static function extractVars($string, $pattern)
|
|
{
|
|
$vars = array();
|
|
$varnames = self::extractVariableNames($pattern);
|
|
|
|
if (! empty($varnames)) {
|
|
$parts = preg_split(self::$variablePattern, $pattern);
|
|
foreach ($parts as $key => $val) {
|
|
$parts[$key] = preg_quote($val, '/');
|
|
}
|
|
|
|
$regex = '/' . implode('([^\.]+?)', $parts) . '/';
|
|
if (preg_match($regex, $string, $m)) {
|
|
array_shift($m);
|
|
$vars = array_combine($varnames, $m);
|
|
}
|
|
}
|
|
|
|
return $vars;
|
|
}
|
|
}
|