PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/Version/Version.php

https://gitlab.com/OnBlox/OnBlox-Template
PHP | 226 lines | 116 code | 29 blank | 81 comment | 13 complexity | b774ec330bd50e8c30a202e3061eea0d MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  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
  8. */
  9. namespace Zend\Version;
  10. use Zend\Http;
  11. use Zend\Json\Json;
  12. /**
  13. * Class to store and retrieve the version of Zend Framework.
  14. */
  15. final class Version
  16. {
  17. /**
  18. * Zend Framework version identification - see compareVersion()
  19. */
  20. const VERSION = '2.4.9';
  21. /**
  22. * Github Service Identifier for version information is retrieved from
  23. */
  24. const VERSION_SERVICE_GITHUB = 'GITHUB';
  25. /**
  26. * Zend (framework.zend.com) Service Identifier for version information is retrieved from
  27. */
  28. const VERSION_SERVICE_ZEND = 'ZEND';
  29. /**
  30. * The latest stable version Zend Framework available
  31. *
  32. * @var string
  33. */
  34. protected static $latestVersion;
  35. /**
  36. * Compare the specified Zend Framework version string $version
  37. * with the current Zend\Version\Version::VERSION of Zend Framework.
  38. *
  39. * @param string $version A version string (e.g. "0.7.1").
  40. * @return int -1 if the $version is older,
  41. * 0 if they are the same,
  42. * and +1 if $version is newer.
  43. *
  44. */
  45. public static function compareVersion($version)
  46. {
  47. $version = strtolower($version);
  48. $version = preg_replace('/(\d)pr(\d?)/', '$1a$2', $version);
  49. return version_compare($version, strtolower(self::VERSION));
  50. }
  51. /**
  52. * Fetches the version of the latest stable release.
  53. *
  54. * By default, this uses the API provided by framework.zend.com for version
  55. * retrieval.
  56. *
  57. * If $service is set to VERSION_SERVICE_GITHUB, this will use the GitHub
  58. * API (v3) and only returns refs that begin with * 'tags/release-'.
  59. * Because GitHub returns the refs in alphabetical order, we need to reduce
  60. * the array to a single value, comparing the version numbers with
  61. * version_compare().
  62. *
  63. * @see http://developer.github.com/v3/git/refs/#get-all-references
  64. * @link https://api.github.com/repos/zendframework/zf2/git/refs/tags/release-
  65. * @link http://framework.zend.com/api/zf-version?v=2
  66. * @param string $service Version service with which to retrieve the version
  67. * @param Http\Client $httpClient HTTP client with which to retrieve the version
  68. * @return string
  69. */
  70. public static function getLatest($service = self::VERSION_SERVICE_ZEND, Http\Client $httpClient = null)
  71. {
  72. if (null !== self::$latestVersion) {
  73. return self::$latestVersion;
  74. }
  75. self::$latestVersion = 'not available';
  76. if (null === $httpClient && !ini_get('allow_url_fopen')) {
  77. trigger_error(
  78. sprintf(
  79. 'allow_url_fopen is not set, and no Zend\Http\Client ' .
  80. 'was passed. You must either set allow_url_fopen in ' .
  81. 'your PHP configuration or pass a configured ' .
  82. 'Zend\Http\Client as the second argument to %s.',
  83. __METHOD__
  84. ),
  85. E_USER_WARNING
  86. );
  87. return self::$latestVersion;
  88. }
  89. $response = false;
  90. if ($service === self::VERSION_SERVICE_GITHUB) {
  91. $response = self::getLatestFromGithub($httpClient);
  92. } elseif ($service === self::VERSION_SERVICE_ZEND) {
  93. $response = self::getLatestFromZend($httpClient);
  94. } else {
  95. trigger_error(
  96. sprintf(
  97. 'Unknown version service: %s',
  98. $service
  99. ),
  100. E_USER_WARNING
  101. );
  102. }
  103. if ($response) {
  104. self::$latestVersion = $response;
  105. }
  106. return self::$latestVersion;
  107. }
  108. /**
  109. * Returns true if the running version of Zend Framework is
  110. * the latest (or newer??) than the latest tag on GitHub,
  111. * which is returned by self::getLatest().
  112. *
  113. * @return bool
  114. */
  115. public static function isLatest()
  116. {
  117. return self::compareVersion(self::getLatest()) < 1;
  118. }
  119. /**
  120. * Get the API response to a call from a configured HTTP client
  121. *
  122. * @param Http\Client $httpClient Configured HTTP client
  123. * @return string|false API response or false on error
  124. */
  125. protected static function getApiResponse(Http\Client $httpClient)
  126. {
  127. try {
  128. $response = $httpClient->send();
  129. } catch (Http\Exception\RuntimeException $e) {
  130. return false;
  131. }
  132. if (!$response->isSuccess()) {
  133. return false;
  134. }
  135. return $response->getBody();
  136. }
  137. /**
  138. * Get the latest version from Github
  139. *
  140. * @param Http\Client $httpClient Configured HTTP client
  141. * @return string|null API response or false on error
  142. */
  143. protected static function getLatestFromGithub(Http\Client $httpClient = null)
  144. {
  145. $url = 'https://api.github.com/repos/zendframework/zf2/git/refs/tags/release-';
  146. if ($httpClient === null) {
  147. $context = stream_context_create(
  148. array(
  149. 'http' => array(
  150. 'user_agent' => sprintf('Zend-Version/%s', self::VERSION),
  151. ),
  152. )
  153. );
  154. $apiResponse = file_get_contents($url, false, $context);
  155. } else {
  156. $request = new Http\Request();
  157. $request->setUri($url);
  158. $httpClient->setRequest($request);
  159. $apiResponse = self::getApiResponse($httpClient);
  160. }
  161. if (!$apiResponse) {
  162. return false;
  163. }
  164. $decodedResponse = Json::decode($apiResponse, Json::TYPE_ARRAY);
  165. // Simplify the API response into a simple array of version numbers
  166. $tags = array_map(function ($tag) {
  167. return substr($tag['ref'], 18); // Reliable because we're
  168. // filtering on 'refs/tags/release-'
  169. }, $decodedResponse);
  170. // Fetch the latest version number from the array
  171. return array_reduce($tags, function ($a, $b) {
  172. return version_compare($a, $b, '>') ? $a : $b;
  173. });
  174. }
  175. /**
  176. * Get the latest version from framework.zend.com
  177. *
  178. * @param Http\Client $httpClient Configured HTTP client
  179. * @return string|null API response or false on error
  180. */
  181. protected static function getLatestFromZend(Http\Client $httpClient = null)
  182. {
  183. $url = 'http://framework.zend.com/api/zf-version?v=2';
  184. if ($httpClient === null) {
  185. $apiResponse = file_get_contents($url);
  186. } else {
  187. $request = new Http\Request();
  188. $request->setUri($url);
  189. $httpClient->setRequest($request);
  190. $apiResponse = self::getApiResponse($httpClient);
  191. }
  192. if (!$apiResponse) {
  193. return false;
  194. }
  195. return $apiResponse;
  196. }
  197. }