1
0

PlatformRepository.php 6.8 KB

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