Skip to content

Commit

Permalink
Outsource version-getting logic to Version::get()
Browse files Browse the repository at this point in the history
refs #9247
  • Loading branch information
Al2Klimov committed Jun 3, 2015
1 parent 5b8de49 commit 757e993
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 86 deletions.
80 changes: 2 additions & 78 deletions application/controllers/AboutController.php
Expand Up @@ -4,88 +4,12 @@
# namespace Icinga\Application\Controllers;

use Icinga\Web\Controller\ActionController;
use Icinga\Application\Icinga;
use Icinga\Exception\IcingaException;
use Icinga\Version;

class AboutController extends ActionController
{
public function indexAction()
{
$this->view->appVersion = null;
$this->view->gitCommitID = null;
$this->view->gitCommitDate = null;

if (false !== ($appVersion = @file(
Icinga::app()->getApplicationDir() . DIRECTORY_SEPARATOR . 'VERSION',
FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES
))) {
foreach ($appVersion as $av) {
$matches = array();
if (false === ($res = preg_match(
'/(?<!.)\s*(.+?)\s*:\s*(.+?)\s*(?!.)/ms', $av, $matches
))) {
throw new IcingaException('Failed at preg_match()');
}
if ($res === 0) {
continue;
}

switch ($matches[1]) {
case 'GitCommitID':
if ($this->view->gitCommitID !== null) {
break;
}

$matches2 = array();
if (false === ($res = preg_match(
'/(?<!.)(.+?)(?:\s*\(\s*(.+?)\s*\))?(?!.)/ms',
$matches[2],
$matches2
))) {
throw new IcingaException('Failed at preg_match()');
}
if ($res === 0) {
break;
}

$this->view->gitCommitID = $matches2[1];
if (! isset($matches2[2])) {
break;
}

foreach (preg_split(
'/\s*,\s*/', $matches2[2], -1, PREG_SPLIT_NO_EMPTY
) as $refName) {
$matches3 = array();
if (false === ($res = preg_match(
'/(?<!.)tag\s*:\s*v(.+?)(?!.)/ms',
$refName,
$matches3
))) {
throw new IcingaException('Failed at preg_match()');
}
if ($res === 1) {
$this->view->appVersion = $matches3[1];
break;
}
}
break;
case 'GitCommitDate':
if ($this->view->gitCommitDate !== null) {
break;
}

$matches2 = array();
if (false === ($res = preg_match(
'/(?<!.)(\S+)/ms', $matches[2], $matches2
))) {
throw new IcingaException('Failed at preg_match()');
}
if ($res === 1) {
$this->view->gitCommitDate = $matches2[1];
}
}
}
}
$this->view->version = Version::get();
}
}
17 changes: 9 additions & 8 deletions application/views/scripts/about/index.phtml
Expand Up @@ -2,14 +2,15 @@
<h1>Icinga Web 2</h1>
<?php
$versionInfo = array();
foreach (array(
array($this->translate('Version: %s'), $appVersion),
array($this->translate('Git commit ID: %s'), $gitCommitID),
array($this->translate('Git commit date: %s'), $gitCommitDate)
) as $version) {
list($label, $value) = $version;
if ($value !== null) {
$versionInfo[] = sprintf($label, htmlspecialchars($value));
if ($version !== false) {
foreach (array(
'appVersion' => $this->translate('Version: %s'),
'gitCommitID' => $this->translate('Git commit ID: %s'),
'gitCommitDate' => $this->translate('Git commit date: %s')
) as $key => $label) {
if (null !== ($value = $version[$key])) {
$versionInfo[] = sprintf($label, htmlspecialchars($value));
}
}
}
?>
Expand Down
105 changes: 105 additions & 0 deletions library/Icinga/Version.php
@@ -0,0 +1,105 @@
<?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */

namespace Icinga;

use Icinga\Application\Icinga;
use Icinga\Exception\IcingaException;

class Version
{
/**
* Get the version of this instance of Icinga Web 2
*
* @return array|bool array on success, false otherwise
*/
public static function get()
{
$versionInfo = array(
'appVersion' => null,
'gitCommitID' => null,
'gitCommitDate' => null
);

if (false !== ($appVersion = @file(
Icinga::app()->getApplicationDir() . DIRECTORY_SEPARATOR . 'VERSION',
FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES
))) {
foreach ($appVersion as $av) {
$matches = array();
if (false === ($res = preg_match(
'/(?<!.)\s*(.+?)\s*:\s*(.+?)\s*(?!.)/ms', $av, $matches
))) {
throw new IcingaException('Failed at preg_match()');
}
if ($res === 0) {
continue;
}

switch ($matches[1]) {
case 'GitCommitID':
if ($versionInfo['gitCommitID'] !== null) {
break;
}

$matches2 = array();
if (false === ($res = preg_match(
'/(?<!.)(.+?)(?:\s*\(\s*(.+?)\s*\))?(?!.)/ms',
$matches[2],
$matches2
))) {
throw new IcingaException('Failed at preg_match()');
}
if ($res === 0) {
break;
}

$versionInfo['gitCommitID'] = $matches2[1];
if (! isset($matches2[2])) {
break;
}

foreach (preg_split(
'/\s*,\s*/', $matches2[2], -1, PREG_SPLIT_NO_EMPTY
) as $refName) {
$matches3 = array();
if (false === ($res = preg_match(
'/(?<!.)tag\s*:\s*v(.+?)(?!.)/ms',
$refName,
$matches3
))) {
throw new IcingaException('Failed at preg_match()');
}
if ($res === 1) {
$versionInfo['appVersion'] = $matches3[1];
break;
}
}
break;
case 'GitCommitDate':
if ($versionInfo['gitCommitDate'] !== null) {
break;
}

$matches2 = array();
if (false === ($res = preg_match(
'/(?<!.)(\S+)/ms', $matches[2], $matches2
))) {
throw new IcingaException('Failed at preg_match()');
}
if ($res === 1) {
$versionInfo['gitCommitDate'] = $matches2[1];
}
}
}
}

foreach (array('gitCommitID', 'gitCommitDate') as $key) {
if ($versionInfo[$key] === null) {
return false;
}
}

return $versionInfo;
}
}

0 comments on commit 757e993

Please sign in to comment.