1
0

PlatformRepository.php 6.2 KB

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