~tcuthbert/wordpress/openstack-objectstorage

« back to all changes in this revision

Viewing changes to vendor/sebastian/version/src/Version.php

  • Committer: Jacek Nykis
  • Date: 2015-02-11 15:35:31 UTC
  • Revision ID: jacek.nykis@canonical.com-20150211153531-hmy6zi0ov2qfkl0b
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Copyright (c) 2013-2014, Sebastian Bergmann <sebastian@phpunit.de>.
 
4
 * All rights reserved.
 
5
 *
 
6
 * Redistribution and use in source and binary forms, with or without
 
7
 * modification, are permitted provided that the following conditions
 
8
 * are met:
 
9
 *
 
10
 *   * Redistributions of source code must retain the above copyright
 
11
 *     notice, this list of conditions and the following disclaimer.
 
12
 *
 
13
 *   * Redistributions in binary form must reproduce the above copyright
 
14
 *     notice, this list of conditions and the following disclaimer in
 
15
 *     the documentation and/or other materials provided with the
 
16
 *     distribution.
 
17
 *
 
18
 *   * Neither the name of Sebastian Bergmann nor the names of his
 
19
 *     contributors may be used to endorse or promote products derived
 
20
 *     from this software without specific prior written permission.
 
21
 *
 
22
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
23
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
24
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 
25
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 
26
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 
27
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 
28
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 
29
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 
30
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 
31
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 
32
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
33
 * POSSIBILITY OF SUCH DAMAGE.
 
34
 *
 
35
 * @package   Version
 
36
 * @author    Sebastian Bergmann <sebastian@phpunit.de>
 
37
 * @copyright 2013-2014 Sebastian Bergmann <sebastian@phpunit.de>
 
38
 * @license   http://www.opensource.org/licenses/BSD-3-Clause  The BSD 3-Clause License
 
39
 * @link      http://github.com/sebastianbergmann/version
 
40
 * @since     File available since Release 1.0.0
 
41
 */
 
42
 
 
43
namespace SebastianBergmann;
 
44
 
 
45
/**
 
46
 * @package   Version
 
47
 * @author    Sebastian Bergmann <sebastian@phpunit.de>
 
48
 * @copyright 2013-2014 Sebastian Bergmann <sebastian@phpunit.de>
 
49
 * @license   http://www.opensource.org/licenses/BSD-3-Clause  The BSD 3-Clause License
 
50
 * @link      http://github.com/sebastianbergmann/version
 
51
 * @since     Class available since Release 1.0.0
 
52
 */
 
53
class Version
 
54
{
 
55
    private $path;
 
56
    private $release;
 
57
    private $version;
 
58
 
 
59
    /**
 
60
     * @param string $release
 
61
     * @param string $path
 
62
     */
 
63
    public function __construct($release, $path)
 
64
    {
 
65
        $this->release = $release;
 
66
        $this->path    = $path;
 
67
    }
 
68
 
 
69
    /**
 
70
     * @return string
 
71
     */
 
72
    public function getVersion()
 
73
    {
 
74
        if ($this->version === null) {
 
75
            if (count(explode('.', $this->release)) == 3) {
 
76
                $this->version = $this->release;
 
77
            } else {
 
78
                $this->version = $this->release . '-dev';
 
79
            }
 
80
 
 
81
            $git = $this->getGitInformation($this->path);
 
82
 
 
83
            if ($git) {
 
84
                if (count(explode('.', $this->release)) == 3) {
 
85
                    $this->version = $git;
 
86
                } else {
 
87
                    $git = explode('-', $git);
 
88
 
 
89
                    $this->version = $this->release . '-' . end($git);
 
90
                }
 
91
            }
 
92
        }
 
93
 
 
94
        return $this->version;
 
95
    }
 
96
 
 
97
    /**
 
98
     * @param  string $path
 
99
     * @return boolean|string
 
100
     */
 
101
    private function getGitInformation($path)
 
102
    {
 
103
        if (!is_dir($path . DIRECTORY_SEPARATOR . '.git')) {
 
104
            return false;
 
105
        }
 
106
 
 
107
        $dir = getcwd();
 
108
        chdir($path);
 
109
        $result = @exec('git describe --tags 2>&1', $output, $returnCode);
 
110
        chdir($dir);
 
111
 
 
112
        if ($returnCode !== 0) {
 
113
            return false;
 
114
        }
 
115
 
 
116
        return $result;
 
117
    }
 
118
}