This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
/
Version.php
121 lines (108 loc) · 4.12 KB
/
Version.php
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Version.php
*/
namespace Zend\Version;
use Zend\Json\Json;
/**
* Class to store and retrieve the version of Zend Framework.
*
* @category Zend
* @package Zend_Version
*/
final class Version
{
/**
* Zend Framework version identification - see compareVersion()
*/
const VERSION = '2.0.3dev';
/**
* Github Service Identifier for version information is retreived from
*/
const VERSION_SERVICE_GITHUB = 'GITHUB';
/**
* Zend (framework.zend.com) Service Identifier for version information is retreived from
*/
const VERSION_SERVICE_ZEND = 'ZEND';
/**
* The latest stable version Zend Framework available
*
* @var string
*/
protected static $latestVersion;
/**
* Compare the specified Zend Framework version string $version
* with the current Zend_Version::VERSION of Zend Framework.
*
* @param string $version A version string (e.g. "0.7.1").
* @return int -1 if the $version is older,
* 0 if they are the same,
* and +1 if $version is newer.
*
*/
public static function compareVersion($version)
{
$version = strtolower($version);
$version = preg_replace('/(\d)pr(\d?)/', '$1a$2', $version);
return version_compare($version, strtolower(self::VERSION));
}
/**
* Fetches the version of the latest stable release.
*
* By Default, this uses the GitHub API (v3) and only returns refs that begin with
* 'tags/release-'. Because GitHub returns the refs in alphabetical order,
* we need to reduce the array to a single value, comparing the version
* numbers with version_compare().
*
* If $service is set to VERSION_SERVICE_ZEND this will fall back to calling the
* classic style of version retreival.
*
*
* @see http://developer.github.com/v3/git/refs/#get-all-references
* @link https://api.github.com/repos/zendframework/zf2/git/refs/tags/release-
* @link http://framework.zend.com/api/zf-version?v=2
* @param string $service Version Service with which to retrieve the version
* @return string
*/
public static function getLatest($service = self::VERSION_SERVICE_GITHUB)
{
if (null === self::$latestVersion) {
self::$latestVersion = 'not available';
if ($service == self::VERSION_SERVICE_GITHUB) {
$url = 'https://api.github.com/repos/zendframework/zf2/git/refs/tags/release-';
$apiResponse = Json::decode(file_get_contents($url), Json::TYPE_ARRAY);
// Simplify the API response into a simple array of version numbers
$tags = array_map(function($tag) {
return substr($tag['ref'], 18); // Reliable because we're filtering on 'refs/tags/release-'
}, $apiResponse);
// Fetch the latest version number from the array
self::$latestVersion = array_reduce($tags, function($a, $b) {
return version_compare($a, $b, '>') ? $a : $b;
});
} elseif($service == self::VERSION_SERVICE_ZEND) {
$handle = fopen('http://framework.zend.com/api/zf-version?v=2', 'r');
if (false !== $handle) {
self::$_latestVersion = stream_get_contents($handle);
fclose($handle);
}
}
}
return self::$latestVersion;
}
/**
* Returns true if the running version of Zend Framework is
* the latest (or newer??) than the latest tag on GitHub,
* which is returned by static::getLatest().
*
* @return boolean
*/
public static function isLatest()
{
return static::compareVersion(static::getLatest()) < 1;
}
}