Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
/*
3
 * This file is part of sebastian/version.
4
 *
5
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
 
11
namespace SebastianBergmann;
12
 
13
final class Version
14
{
15
    /**
16
     * @var string
17
     */
18
    private $path;
19
 
20
    /**
21
     * @var string
22
     */
23
    private $release;
24
 
25
    /**
26
     * @var string
27
     */
28
    private $version;
29
 
30
    public function __construct(string $release, string $path)
31
    {
32
        $this->release = $release;
33
        $this->path    = $path;
34
    }
35
 
36
    public function getVersion(): string
37
    {
38
        if ($this->version === null) {
39
            if (\substr_count($this->release, '.') + 1 === 3) {
40
                $this->version = $this->release;
41
            } else {
42
                $this->version = $this->release . '-dev';
43
            }
44
 
45
            $git = $this->getGitInformation($this->path);
46
 
47
            if ($git) {
48
                if (\substr_count($this->release, '.') + 1 === 3) {
49
                    $this->version = $git;
50
                } else {
51
                    $git = \explode('-', $git);
52
 
53
                    $this->version = $this->release . '-' . \end($git);
54
                }
55
            }
56
        }
57
 
58
        return $this->version;
59
    }
60
 
61
    /**
62
     * @return bool|string
63
     */
64
    private function getGitInformation(string $path)
65
    {
66
        if (!\is_dir($path . DIRECTORY_SEPARATOR . '.git')) {
67
            return false;
68
        }
69
 
70
        $process = \proc_open(
71
            'git describe --tags',
72
            [
73
                1 => ['pipe', 'w'],
74
                2 => ['pipe', 'w'],
75
            ],
76
            $pipes,
77
            $path
78
        );
79
 
80
        if (!\is_resource($process)) {
81
            return false;
82
        }
83
 
84
        $result = \trim(\stream_get_contents($pipes[1]));
85
 
86
        \fclose($pipes[1]);
87
        \fclose($pipes[2]);
88
 
89
        $returnCode = \proc_close($process);
90
 
91
        if ($returnCode !== 0) {
92
            return false;
93
        }
94
 
95
        return $result;
96
    }
97
}