From 7c3b46fefa7e1050003062818f7cc6fdeeddbb3d Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Wed, 23 Sep 2015 17:48:30 +0200 Subject: [PATCH] Make regex less complicated --- library/Icinga/Application/Version.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/library/Icinga/Application/Version.php b/library/Icinga/Application/Version.php index 85495966b..886ac1433 100644 --- a/library/Icinga/Application/Version.php +++ b/library/Icinga/Application/Version.php @@ -33,26 +33,26 @@ class Version } } - if (false === ($appVersion = @file_get_contents( - Icinga::app()->getApplicationDir() . DIRECTORY_SEPARATOR . 'VERSION' - ))) { + if (false === ($appVersion = @file_get_contents(Icinga::app()->getApplicationDir('VERSION')))) { return false; } $matches = array(); - if (false === ($res = preg_match( - '/(?\w+)(?:\s*\(.*?(?:(?<=[\(,])\s*tag\s*:\s*v(?P.+?)\s*(?=[\),]).*?)?\))?\s*(?P\S+)/ms', - $appVersion, - $matches - )) || $res === 0) { + if (! @preg_match('/^(?P\S+)(?: \((.+?)\))? (?P\S+)/', $appVersion, $matches)) { return false; } - foreach ($matches as $key => $value) { - if (is_int($key) || $value === '') { - unset($matches[$key]); + if (array_key_exists(1, $matches)) { + $tagMatches = array(); + foreach (explode(', ', $matches[1]) as $gitRef) { + if (@preg_match('/^tag: v(.+)/', $gitRef, $tagMatches)) { + $matches['appVersion'] = $tagMatches[1]; + break; + } } + unset($matches[1]); } + return $matches; } }