summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Application/Version.php
blob: 3045e8ce6eeed308742b256e7b2949e3a43d8d0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */

namespace Icinga\Application;

/**
 * Retrieve the version of Icinga Web 2
 */
class Version
{
    const VERSION = '2.11.4';

    /**
     * Get the version of this instance of Icinga Web 2
     *
     * @return array
     */
    public static function get()
    {
        $version = array('appVersion' => self::VERSION);
        preg_match('/2.(\d+)\./', self::VERSION, $matches);
        $version['docVersion'] = isset($matches[1]) ? '2.' . $matches[1] : null;

        if (false !== ($appVersion = @file_get_contents(Icinga::app()->getApplicationDir('VERSION')))) {
            $matches = array();
            if (@preg_match('/^(?P<gitCommitID>\w+) (?P<gitCommitDate>\S+)/', $appVersion, $matches)) {
                return array_merge($version, $matches);
            }
        }

        $gitCommitId = static::getGitHead(Icinga::app()->getBaseDir());
        if ($gitCommitId !== false) {
            $version['gitCommitID'] = $gitCommitId;
        }

        return $version;
    }

    /**
     * Get the current commit of the Git repository in the given path
     *
     * @param   string  $repo       Path to the Git repository
     * @param   bool    $bare       Whether the Git repository is bare
     *
     * @return  string|bool         False if not available
     */
    public static function getGitHead($repo, $bare = false)
    {
        if (! $bare) {
            $repo .= '/.git';
        }

        $head = @file_get_contents($repo . '/HEAD');

        if ($head !== false) {
            if (preg_match('/^ref: (.+)/', $head, $matches)) {
                return @file_get_contents($repo . '/' . $matches[1]);
            }

            return $head;
        }

        return false;
    }
}