platformrepository.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace OC\App;
  3. /**
  4. * Class PlatformRepository
  5. *
  6. * Inspired by the composer project - licensed under MIT
  7. * https://github.com/composer/composer/blob/master/src/Composer/Repository/PlatformRepository.php#L82
  8. *
  9. * @package OC\App
  10. */
  11. class PlatformRepository {
  12. public function __construct() {
  13. $this->packages = $this->initialize();
  14. }
  15. protected function initialize() {
  16. $loadedExtensions = get_loaded_extensions();
  17. $packages = array();
  18. // Extensions scanning
  19. foreach ($loadedExtensions as $name) {
  20. if (in_array($name, array('standard', 'Core'))) {
  21. continue;
  22. }
  23. $ext = new \ReflectionExtension($name);
  24. try {
  25. $prettyVersion = $ext->getVersion();
  26. $prettyVersion = $this->normalizeVersion($prettyVersion);
  27. } catch (\UnexpectedValueException $e) {
  28. $prettyVersion = '0';
  29. $prettyVersion = $this->normalizeVersion($prettyVersion);
  30. }
  31. $packages[$this->buildPackageName($name)] = $prettyVersion;
  32. }
  33. foreach ($loadedExtensions as $name) {
  34. $prettyVersion = null;
  35. switch ($name) {
  36. case 'curl':
  37. $curlVersion = curl_version();
  38. $prettyVersion = $curlVersion['version'];
  39. break;
  40. case 'iconv':
  41. $prettyVersion = ICONV_VERSION;
  42. break;
  43. case 'intl':
  44. $name = 'ICU';
  45. if (defined('INTL_ICU_VERSION')) {
  46. $prettyVersion = INTL_ICU_VERSION;
  47. } else {
  48. $reflector = new \ReflectionExtension('intl');
  49. ob_start();
  50. $reflector->info();
  51. $output = ob_get_clean();
  52. preg_match('/^ICU version => (.*)$/m', $output, $matches);
  53. $prettyVersion = $matches[1];
  54. }
  55. break;
  56. case 'libxml':
  57. $prettyVersion = LIBXML_DOTTED_VERSION;
  58. break;
  59. case 'openssl':
  60. $prettyVersion = preg_replace_callback('{^(?:OpenSSL\s*)?([0-9.]+)([a-z]?).*}', function ($match) {
  61. return $match[1] . (empty($match[2]) ? '' : '.' . (ord($match[2]) - 96));
  62. }, OPENSSL_VERSION_TEXT);
  63. break;
  64. case 'pcre':
  65. $prettyVersion = preg_replace('{^(\S+).*}', '$1', PCRE_VERSION);
  66. break;
  67. case 'uuid':
  68. $prettyVersion = phpversion('uuid');
  69. break;
  70. case 'xsl':
  71. $prettyVersion = LIBXSLT_DOTTED_VERSION;
  72. break;
  73. default:
  74. // None handled extensions have no special cases, skip
  75. continue 2;
  76. }
  77. try {
  78. $prettyVersion = $this->normalizeVersion($prettyVersion);
  79. } catch (\UnexpectedValueException $e) {
  80. continue;
  81. }
  82. $packages[$this->buildPackageName($name)] = $prettyVersion;
  83. }
  84. return $packages;
  85. }
  86. private function buildPackageName($name) {
  87. return str_replace(' ', '-', $name);
  88. }
  89. /**
  90. * @param $name
  91. * @return string
  92. */
  93. public function findLibrary($name) {
  94. $extName = $this->buildPackageName($name);
  95. if (isset($this->packages[$extName])) {
  96. return $this->packages[$extName];
  97. }
  98. return null;
  99. }
  100. private static $modifierRegex = '[._-]?(?:(stable|beta|b|RC|alpha|a|patch|pl|p)(?:[.-]?(\d+))?)?([.-]?dev)?';
  101. /**
  102. * Normalizes a version string to be able to perform comparisons on it
  103. *
  104. * https://github.com/composer/composer/blob/master/src/Composer/Package/Version/VersionParser.php#L94
  105. *
  106. * @param string $version
  107. * @param string $fullVersion optional complete version string to give more context
  108. * @throws \UnexpectedValueException
  109. * @return string
  110. */
  111. public function normalizeVersion($version, $fullVersion = null) {
  112. $version = trim($version);
  113. if (null === $fullVersion) {
  114. $fullVersion = $version;
  115. }
  116. // ignore aliases and just assume the alias is required instead of the source
  117. if (preg_match('{^([^,\s]+) +as +([^,\s]+)$}', $version, $match)) {
  118. $version = $match[1];
  119. }
  120. // match master-like branches
  121. if (preg_match('{^(?:dev-)?(?:master|trunk|default)$}i', $version)) {
  122. return '9999999-dev';
  123. }
  124. if ('dev-' === strtolower(substr($version, 0, 4))) {
  125. return 'dev-' . substr($version, 4);
  126. }
  127. // match classical versioning
  128. if (preg_match('{^v?(\d{1,3})(\.\d+)?(\.\d+)?(\.\d+)?' . self::$modifierRegex . '$}i', $version, $matches)) {
  129. $version = $matches[1]
  130. . (!empty($matches[2]) ? $matches[2] : '.0')
  131. . (!empty($matches[3]) ? $matches[3] : '.0')
  132. . (!empty($matches[4]) ? $matches[4] : '.0');
  133. $index = 5;
  134. } elseif (preg_match('{^v?(\d{4}(?:[.:-]?\d{2}){1,6}(?:[.:-]?\d{1,3})?)' . self::$modifierRegex . '$}i', $version, $matches)) { // match date-based versioning
  135. $version = preg_replace('{\D}', '-', $matches[1]);
  136. $index = 2;
  137. } elseif (preg_match('{^v?(\d{4,})(\.\d+)?(\.\d+)?(\.\d+)?' . self::$modifierRegex . '$}i', $version, $matches)) {
  138. $version = $matches[1]
  139. . (!empty($matches[2]) ? $matches[2] : '.0')
  140. . (!empty($matches[3]) ? $matches[3] : '.0')
  141. . (!empty($matches[4]) ? $matches[4] : '.0');
  142. $index = 5;
  143. }
  144. // add version modifiers if a version was matched
  145. if (isset($index)) {
  146. if (!empty($matches[$index])) {
  147. if ('stable' === $matches[$index]) {
  148. return $version;
  149. }
  150. $version .= '-' . $this->expandStability($matches[$index]) . (!empty($matches[$index + 1]) ? $matches[$index + 1] : '');
  151. }
  152. if (!empty($matches[$index + 2])) {
  153. $version .= '-dev';
  154. }
  155. return $version;
  156. }
  157. $extraMessage = '';
  158. if (preg_match('{ +as +' . preg_quote($version) . '$}', $fullVersion)) {
  159. $extraMessage = ' in "' . $fullVersion . '", the alias must be an exact version';
  160. } elseif (preg_match('{^' . preg_quote($version) . ' +as +}', $fullVersion)) {
  161. $extraMessage = ' in "' . $fullVersion . '", the alias source must be an exact version, if it is a branch name you should prefix it with dev-';
  162. }
  163. throw new \UnexpectedValueException('Invalid version string "' . $version . '"' . $extraMessage);
  164. }
  165. private function expandStability($stability) {
  166. $stability = strtolower($stability);
  167. switch ($stability) {
  168. case 'a':
  169. return 'alpha';
  170. case 'b':
  171. return 'beta';
  172. case 'p':
  173. case 'pl':
  174. return 'patch';
  175. case 'rc':
  176. return 'RC';
  177. default:
  178. return $stability;
  179. }
  180. }
  181. }