3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Version
;
16 * Class to store and retrieve the version of Zend Framework.
21 * Zend Framework version identification - see compareVersion()
23 const VERSION
= '2.4.9';
26 * Github Service Identifier for version information is retrieved from
28 const VERSION_SERVICE_GITHUB
= 'GITHUB';
31 * Zend (framework.zend.com) Service Identifier for version information is retrieved from
33 const VERSION_SERVICE_ZEND
= 'ZEND';
36 * The latest stable version Zend Framework available
40 protected static $latestVersion;
43 * Compare the specified Zend Framework version string $version
44 * with the current Zend\Version\Version::VERSION of Zend Framework.
46 * @param string $version A version string (e.g. "0.7.1").
47 * @return int -1 if the $version is older,
48 * 0 if they are the same,
49 * and +1 if $version is newer.
52 public static function compareVersion($version)
54 $version = strtolower($version);
55 $version = preg_replace('/(\d)pr(\d?)/', '$1a$2', $version);
57 return version_compare($version, strtolower(self
::VERSION
));
61 * Fetches the version of the latest stable release.
63 * By default, this uses the API provided by framework.zend.com for version
66 * If $service is set to VERSION_SERVICE_GITHUB, this will use the GitHub
67 * API (v3) and only returns refs that begin with * 'tags/release-'.
68 * Because GitHub returns the refs in alphabetical order, we need to reduce
69 * the array to a single value, comparing the version numbers with
72 * @see http://developer.github.com/v3/git/refs/#get-all-references
73 * @link https://api.github.com/repos/zendframework/zf2/git/refs/tags/release-
74 * @link http://framework.zend.com/api/zf-version?v=2
75 * @param string $service Version service with which to retrieve the version
76 * @param Http\Client $httpClient HTTP client with which to retrieve the version
79 public static function getLatest($service = self
::VERSION_SERVICE_ZEND
, Http\Client
$httpClient = null)
81 if (null !== self
::$latestVersion) {
82 return self
::$latestVersion;
85 self
::$latestVersion = 'not available';
87 if (null === $httpClient && !ini_get('allow_url_fopen')) {
90 'allow_url_fopen is not set, and no Zend\Http\Client ' .
91 'was passed. You must either set allow_url_fopen in ' .
92 'your PHP configuration or pass a configured ' .
93 'Zend\Http\Client as the second argument to %s.',
99 return self
::$latestVersion;
103 if ($service === self
::VERSION_SERVICE_GITHUB
) {
104 $response = self
::getLatestFromGithub($httpClient);
105 } elseif ($service === self
::VERSION_SERVICE_ZEND
) {
106 $response = self
::getLatestFromZend($httpClient);
110 'Unknown version service: %s',
118 self
::$latestVersion = $response;
121 return self
::$latestVersion;
125 * Returns true if the running version of Zend Framework is
126 * the latest (or newer??) than the latest tag on GitHub,
127 * which is returned by self::getLatest().
131 public static function isLatest()
133 return self
::compareVersion(self
::getLatest()) < 1;
137 * Get the API response to a call from a configured HTTP client
139 * @param Http\Client $httpClient Configured HTTP client
140 * @return string|false API response or false on error
142 protected static function getApiResponse(Http\Client
$httpClient)
145 $response = $httpClient->send();
146 } catch (Http\Exception\RuntimeException
$e) {
150 if (!$response->isSuccess()) {
154 return $response->getBody();
158 * Get the latest version from Github
160 * @param Http\Client $httpClient Configured HTTP client
161 * @return string|null API response or false on error
163 protected static function getLatestFromGithub(Http\Client
$httpClient = null)
165 $url = 'https://api.github.com/repos/zendframework/zf2/git/refs/tags/release-';
167 if ($httpClient === null) {
168 $context = stream_context_create(
171 'user_agent' => sprintf('Zend-Version/%s', self
::VERSION
),
175 $apiResponse = file_get_contents($url, false, $context);
177 $request = new Http\
Request();
178 $request->setUri($url);
179 $httpClient->setRequest($request);
180 $apiResponse = self
::getApiResponse($httpClient);
187 $decodedResponse = Json
::decode($apiResponse, Json
::TYPE_ARRAY
);
189 // Simplify the API response into a simple array of version numbers
190 $tags = array_map(function ($tag) {
191 return substr($tag['ref'], 18); // Reliable because we're
192 // filtering on 'refs/tags/release-'
193 }, $decodedResponse);
195 // Fetch the latest version number from the array
196 return array_reduce($tags, function ($a, $b) {
197 return version_compare($a, $b, '>') ?
$a : $b;
202 * Get the latest version from framework.zend.com
204 * @param Http\Client $httpClient Configured HTTP client
205 * @return string|null API response or false on error
207 protected static function getLatestFromZend(Http\Client
$httpClient = null)
209 $url = 'http://framework.zend.com/api/zf-version?v=2';
211 if ($httpClient === null) {
212 $apiResponse = file_get_contents($url);
214 $request = new Http\
Request();
215 $request->setUri($url);
216 $httpClient->setRequest($request);
217 $apiResponse = self
::getApiResponse($httpClient);