| 1 | <?php |
||
| 2 | |||
| 3 | namespace Ballen\GitVersionNumber; |
||
| 4 | |||
| 5 | use Ballen\Executioner\Executioner as Executable; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * Git Version Number |
||
| 9 | * |
||
| 10 | * A library for extracting and utilising your project's Git version information. |
||
| 11 | * |
||
| 12 | * @author Bobby Allen <[email protected]> |
||
| 13 | * @license https://raw.githubusercontent.com/allebb/git-version-number/master/LICENSE |
||
| 14 | * @link https://github.com/allebb/git-version-number |
||
| 15 | * @link http://bobbyallen.me |
||
| 16 | * |
||
| 17 | */ |
||
| 18 | class Version |
||
| 19 | { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * The git binary path. |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | private $gitBin; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Optional path to the .git directory (root of the project if it is not the same as the PHP script) |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | private $gitPath; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The Git Tag version number. |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | private $version = '0.0.0-0-0000000'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The Git commit hash. |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | private $hash = null; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Class constructor |
||
| 47 | * @param string $gitPath The root project path (if not the current directory) |
||
| 48 | * @param string $gitBin The path to the Git binary (by default will use the system PATH variable.) |
||
| 49 | */ |
||
| 50 | 26 | public function __construct($gitPath = '.', $gitBin = 'git') |
|
| 51 | { |
||
| 52 | 26 | $this->gitBin = $gitBin; |
|
| 53 | 26 | $this->setRepositoryPath($gitPath); |
|
| 54 | 26 | $this->extractVersion(); |
|
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Get the version number as a dot seperated string. |
||
| 59 | * @param int $bits Optional version number bits to return. |
||
| 60 | * @return string |
||
| 61 | */ |
||
| 62 | 16 | public function getVersionString($bits = null) |
|
| 63 | { |
||
| 64 | 16 | if (is_null($bits)) { |
|
| 65 | 16 | return $this->version; |
|
| 66 | } |
||
| 67 | 4 | return implode('.', $this->versionFromBits($bits)); |
|
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Returns the version number as a full number (integer) this is the same as the version string but without dots and will drop leading zeros! |
||
| 72 | * @param int $bits Optional version number bits to return. |
||
| 73 | * @return int |
||
| 74 | */ |
||
| 75 | 6 | public function getVersionNumber($bits = null) |
|
| 76 | { |
||
| 77 | 6 | if (is_null($bits)) { |
|
| 78 | 2 | return (int)str_replace('.', '', $this->version); |
|
| 79 | } |
||
| 80 | 4 | return implode('', $this->versionFromBits($bits)); |
|
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Returns the version hash (if one is present) (eg. g0106dc9) |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | 4 | public function getVersionHash() |
|
| 88 | { |
||
| 89 | 4 | return $this->hash; |
|
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Returns an array containing the version string parts. |
||
| 94 | * @return array |
||
| 95 | */ |
||
| 96 | 12 | public function getVersionBits() |
|
| 97 | { |
||
| 98 | 12 | return explode('.', $this->getVersionString()); |
|
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Method overload, outputs the full text version number as outputted by the 'git describe --tags' command. |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | 2 | public function __toString() |
|
| 106 | { |
||
| 107 | 2 | return $this->version; |
|
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Sets the Git repository path ensuring the /.git is appended to the end of the path. |
||
| 112 | * @return void |
||
| 113 | */ |
||
| 114 | 26 | private function setRepositoryPath($path) |
|
| 115 | { |
||
| 116 | 26 | $this->gitPath = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '.git'; |
|
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Extracts the version from the Git repository. |
||
| 121 | * @return void |
||
| 122 | */ |
||
| 123 | 26 | private function extractVersion() |
|
| 124 | { |
||
| 125 | 26 | $git = Executable::make($this->gitBin) |
|
| 126 | 26 | ->addArgument(sprintf('--git-dir=%s', $this->gitPath)) |
|
| 127 | 26 | ->addArgument('describe') |
|
| 128 | 26 | ->addArgument('--tags') |
|
| 129 | 26 | ->addArgument('--always'); |
|
| 130 | |||
| 131 | try { |
||
| 132 | 26 | $git->execute(); |
|
| 133 | 2 | } catch (\Ballen\Executioner\Exceptions\ExecutionException $exception) { |
|
| 134 | // We could not obtain/execute the git command. |
||
| 135 | } |
||
| 136 | |||
| 137 | 26 | $version = trim($git->resultAsText()); |
|
| 138 | 26 | $this->version = '0.0.0-0-' . $version; |
|
| 139 | |||
| 140 | 26 | if (strlen($version) > 7) { |
|
| 141 | 26 | $this->version = str_replace('v', '', $version); |
|
| 142 | } |
||
| 143 | 26 | $this->versionBits(); |
|
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Computes and sets the version number and object hash properties. |
||
| 148 | * @return void |
||
| 149 | */ |
||
| 150 | 26 | private function versionBits() |
|
| 151 | { |
||
| 152 | 26 | $versionBits = explode('-', $this->version); |
|
| 153 | 26 | if (strlen($versionBits[0])) { |
|
| 154 | 26 | $this->version = $versionBits[0]; |
|
| 155 | 26 | if (isset($versionBits[1])) { |
|
| 156 | 26 | $this->version = $versionBits[0] . '.' . $versionBits[1]; |
|
| 157 | } |
||
| 158 | 26 | if (isset($versionBits[2])) { |
|
| 159 | 26 | $this->hash = $versionBits[2]; |
|
| 160 | } |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Returns an customised array of the total number of version bits. |
||
| 166 | * @param int $bits The total number of bits (segments) to return. |
||
| 167 | * @return array |
||
| 168 | */ |
||
| 169 | 8 | private function versionFromBits($bits) |
|
| 170 | { |
||
| 171 | 8 | $version = []; |
|
| 172 | 8 | foreach (range(0, ($bits - 1)) as $bit) { |
|
| 173 | 8 | $version[$bit] = $this->getVersionBits()[$bit]; |
|
| 174 | } |
||
| 175 | 8 | return $version; |
|
| 176 | } |
||
| 177 | } |
||
| 178 |