spinen /
laravel-version
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Spinen\Version; |
||
| 4 | |||
| 5 | /** |
||
| 6 | * Class Version |
||
| 7 | * |
||
| 8 | * Parse the version file into its parts |
||
| 9 | * |
||
| 10 | * @package Spinen\Version |
||
| 11 | */ |
||
| 12 | class Version |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * Major version |
||
| 16 | * |
||
| 17 | * @var integer|null |
||
| 18 | */ |
||
| 19 | public $major = null; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Meta |
||
| 23 | * |
||
| 24 | * Extra data about the version with dots separating key data and underscores for spaces |
||
| 25 | * |
||
| 26 | * @var string|null |
||
| 27 | */ |
||
| 28 | public $meta = null; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Minor version |
||
| 32 | * |
||
| 33 | * @var integer|null |
||
| 34 | */ |
||
| 35 | public $minor = null; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Patch version |
||
| 39 | * |
||
| 40 | * @var integer|null |
||
| 41 | */ |
||
| 42 | public $patch = null; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Pre release |
||
| 46 | * |
||
| 47 | * This will most likely be the branch version, unless we are on the master branch |
||
| 48 | * |
||
| 49 | * @var string|null |
||
| 50 | */ |
||
| 51 | public $pre_release = null; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Version in SemVer format |
||
| 55 | * |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | public $semver; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The version of the application |
||
| 62 | * |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | public $version = "UNDEFINED"; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The file that holds the version info |
||
| 69 | * |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | protected $version_file; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Version constructor. |
||
| 76 | * |
||
| 77 | * @param string $file |
||
| 78 | */ |
||
| 79 | 9 | public function __construct($file) |
|
| 80 | { |
||
| 81 | 9 | $this->version_file = $file; |
|
| 82 | |||
| 83 | 9 | $this->parseVersionFile(); |
|
| 84 | |||
| 85 | 9 | $this->generateSemVer(); |
|
| 86 | 9 | } |
|
| 87 | |||
| 88 | /** |
||
| 89 | * If the object is used as a string, then return the full version |
||
| 90 | * |
||
| 91 | * @return string |
||
| 92 | */ |
||
| 93 | 8 | public function __toString() |
|
| 94 | { |
||
| 95 | 8 | return $this->semver; |
|
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Put together version parts into the SemVer format |
||
| 100 | */ |
||
| 101 | 9 | protected function generateSemVer() |
|
| 102 | { |
||
| 103 | 9 | $this->semver = $this->version; |
|
| 104 | |||
| 105 | 9 | if ($this->pre_release) { |
|
| 106 | 3 | $this->semver .= '-' . $this->pre_release; |
|
| 107 | } |
||
| 108 | |||
| 109 | 9 | if ($this->meta) { |
|
| 110 | 3 | $this->semver .= '+' . $this->meta; |
|
| 111 | } |
||
| 112 | 9 | } |
|
| 113 | |||
| 114 | /** |
||
| 115 | * If the read-in line matches a version, then parse it |
||
| 116 | * |
||
| 117 | * @param string $version |
||
| 118 | * |
||
| 119 | * @return bool |
||
| 120 | */ |
||
| 121 | 7 | protected function parseVersion($version) |
|
| 122 | { |
||
| 123 | 7 | if (!preg_match('/\\d+\\.\\d+\\.\\d+/u', $version)) { |
|
| 124 | 1 | return false; |
|
| 125 | } |
||
| 126 | |||
| 127 | 6 | $this->version = $version; |
|
| 128 | |||
| 129 | 6 | list($this->major, $this->minor, $this->patch) = array_map('intval', explode(".", $this->version)); |
|
| 130 | |||
| 131 | 6 | return true; |
|
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Read the version file into the various parts |
||
| 136 | * |
||
| 137 | * @return void |
||
| 138 | */ |
||
| 139 | 9 | protected function parseVersionFile() |
|
| 140 | { |
||
| 141 | 9 | if (!file_exists($this->version_file)) { |
|
| 142 | 1 | return; |
|
| 143 | } |
||
| 144 | |||
| 145 | // Read file in as an array & remove any empty lines |
||
| 146 | 8 | $version_parts = array_filter(explode("\n", @file_get_contents($this->version_file))); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 147 | |||
| 148 | // First line is the version |
||
| 149 | 8 | if (empty($version_parts) or !$this->parseVersion(array_shift($version_parts))) { |
|
| 150 | 2 | return; |
|
| 151 | } |
||
| 152 | |||
| 153 | // Next line is branch/pre release |
||
| 154 | 6 | $pre_release = array_shift($version_parts); |
|
| 155 | |||
| 156 | 6 | $this->pre_release = ($pre_release !== 'master') ? $pre_release : null; |
|
| 157 | |||
| 158 | // Is there anything left in the file for meta? |
||
| 159 | 6 | if (empty($version_parts)) { |
|
| 160 | 3 | return; |
|
| 161 | } |
||
| 162 | |||
| 163 | // Everything left is the meta, so concatenate it with .'s & replace the spaces with _'s |
||
| 164 | 3 | $this->meta = preg_replace("/\\s+/u", "_", implode('.', $version_parts)); |
|
| 165 | 3 | } |
|
| 166 | } |
||
| 167 |