PlatformRepository.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Repository;
  12. use Composer\Package\CompletePackage;
  13. use Composer\Package\PackageInterface;
  14. use Composer\Package\Version\VersionParser;
  15. use Composer\Plugin\PluginInterface;
  16. use Composer\Util\ProcessExecutor;
  17. use Composer\Util\Silencer;
  18. use Composer\Util\Platform;
  19. use Composer\XdebugHandler\XdebugHandler;
  20. use Symfony\Component\Process\ExecutableFinder;
  21. /**
  22. * @author Jordi Boggiano <j.boggiano@seld.be>
  23. */
  24. class PlatformRepository extends ArrayRepository
  25. {
  26. const PLATFORM_PACKAGE_REGEX = '{^(?:php(?:-64bit|-ipv6|-zts|-debug)?|hhvm|(?:ext|lib)-[a-z0-9](?:[_.-]?[a-z0-9]+)*|composer-plugin-api)$}iD';
  27. private $versionParser;
  28. /**
  29. * Defines overrides so that the platform can be mocked
  30. *
  31. * Should be an array of package name => version number mappings
  32. *
  33. * @var array
  34. */
  35. private $overrides = array();
  36. private $process;
  37. public function __construct(array $packages = array(), array $overrides = array(), ProcessExecutor $process = null)
  38. {
  39. $this->process = $process === null ? (new ProcessExecutor()) : $process;
  40. foreach ($overrides as $name => $version) {
  41. $this->overrides[strtolower($name)] = array('name' => $name, 'version' => $version);
  42. }
  43. parent::__construct($packages);
  44. }
  45. public function getRepoName()
  46. {
  47. return 'platform repo';
  48. }
  49. protected function initialize()
  50. {
  51. parent::initialize();
  52. $this->versionParser = new VersionParser();
  53. // Add each of the override versions as options.
  54. // Later we might even replace the extensions instead.
  55. foreach ($this->overrides as $override) {
  56. // Check that it's a platform package.
  57. if (!preg_match(self::PLATFORM_PACKAGE_REGEX, $override['name'])) {
  58. throw new \InvalidArgumentException('Invalid platform package name in config.platform: '.$override['name']);
  59. }
  60. $this->addOverriddenPackage($override);
  61. }
  62. $prettyVersion = PluginInterface::PLUGIN_API_VERSION;
  63. $version = $this->versionParser->normalize($prettyVersion);
  64. $composerPluginApi = new CompletePackage('composer-plugin-api', $version, $prettyVersion);
  65. $composerPluginApi->setDescription('The Composer Plugin API');
  66. $this->addPackage($composerPluginApi);
  67. try {
  68. $prettyVersion = PHP_VERSION;
  69. $version = $this->versionParser->normalize($prettyVersion);
  70. } catch (\UnexpectedValueException $e) {
  71. $prettyVersion = preg_replace('#^([^~+-]+).*$#', '$1', PHP_VERSION);
  72. $version = $this->versionParser->normalize($prettyVersion);
  73. }
  74. $php = new CompletePackage('php', $version, $prettyVersion);
  75. $php->setDescription('The PHP interpreter');
  76. $this->addPackage($php);
  77. if (PHP_DEBUG) {
  78. $phpdebug = new CompletePackage('php-debug', $version, $prettyVersion);
  79. $phpdebug->setDescription('The PHP interpreter, with debugging symbols');
  80. $this->addPackage($phpdebug);
  81. }
  82. if (defined('PHP_ZTS') && PHP_ZTS) {
  83. $phpzts = new CompletePackage('php-zts', $version, $prettyVersion);
  84. $phpzts->setDescription('The PHP interpreter, with Zend Thread Safety');
  85. $this->addPackage($phpzts);
  86. }
  87. if (PHP_INT_SIZE === 8) {
  88. $php64 = new CompletePackage('php-64bit', $version, $prettyVersion);
  89. $php64->setDescription('The PHP interpreter, 64bit');
  90. $this->addPackage($php64);
  91. }
  92. // The AF_INET6 constant is only defined if ext-sockets is available but
  93. // IPv6 support might still be available.
  94. if (defined('AF_INET6') || Silencer::call('inet_pton', '::') !== false) {
  95. $phpIpv6 = new CompletePackage('php-ipv6', $version, $prettyVersion);
  96. $phpIpv6->setDescription('The PHP interpreter, with IPv6 support');
  97. $this->addPackage($phpIpv6);
  98. }
  99. $loadedExtensions = get_loaded_extensions();
  100. // Extensions scanning
  101. foreach ($loadedExtensions as $name) {
  102. if (in_array($name, array('standard', 'Core'))) {
  103. continue;
  104. }
  105. $reflExt = new \ReflectionExtension($name);
  106. $prettyVersion = $reflExt->getVersion();
  107. $this->addExtension($name, $prettyVersion);
  108. }
  109. // Check for Xdebug in a restarted process
  110. if (!in_array('xdebug', $loadedExtensions, true) && ($prettyVersion = XdebugHandler::getSkippedVersion())) {
  111. $this->addExtension('xdebug', $prettyVersion);
  112. }
  113. // Another quick loop, just for possible libraries
  114. // Doing it this way to know that functions or constants exist before
  115. // relying on them.
  116. foreach ($loadedExtensions as $name) {
  117. $prettyVersion = null;
  118. $description = 'The '.$name.' PHP library';
  119. switch ($name) {
  120. case 'curl':
  121. $curlVersion = curl_version();
  122. $prettyVersion = $curlVersion['version'];
  123. break;
  124. case 'iconv':
  125. $prettyVersion = ICONV_VERSION;
  126. break;
  127. case 'intl':
  128. $name = 'ICU';
  129. if (defined('INTL_ICU_VERSION')) {
  130. $prettyVersion = INTL_ICU_VERSION;
  131. } else {
  132. $reflector = new \ReflectionExtension('intl');
  133. ob_start();
  134. $reflector->info();
  135. $output = ob_get_clean();
  136. preg_match('/^ICU version => (.*)$/m', $output, $matches);
  137. $prettyVersion = $matches[1];
  138. }
  139. break;
  140. case 'imagick':
  141. $imagick = new \Imagick();
  142. $imageMagickVersion = $imagick->getVersion();
  143. // 6.x: ImageMagick 6.2.9 08/24/06 Q16 http://www.imagemagick.org
  144. // 7.x: ImageMagick 7.0.8-34 Q16 x86_64 2019-03-23 https://imagemagick.org
  145. preg_match('/^ImageMagick ([\d.]+)(?:-(\d+))?/', $imageMagickVersion['versionString'], $matches);
  146. if (isset($matches[2])) {
  147. $prettyVersion = "{$matches[1]}.{$matches[2]}";
  148. } else {
  149. $prettyVersion = $matches[1];
  150. }
  151. break;
  152. case 'libxml':
  153. $prettyVersion = LIBXML_DOTTED_VERSION;
  154. break;
  155. case 'openssl':
  156. $prettyVersion = preg_replace_callback('{^(?:OpenSSL|LibreSSL)?\s*([0-9.]+)([a-z]*).*}i', function ($match) {
  157. if (empty($match[2])) {
  158. return $match[1];
  159. }
  160. // OpenSSL versions add another letter when they reach Z.
  161. // e.g. OpenSSL 0.9.8zh 3 Dec 2015
  162. if (!preg_match('{^z*[a-z]$}', $match[2])) {
  163. // 0.9.8abc is garbage
  164. return 0;
  165. }
  166. $len = strlen($match[2]);
  167. $patchVersion = ($len - 1) * 26; // All Z
  168. $patchVersion += ord($match[2][$len - 1]) - 96;
  169. return $match[1].'.'.$patchVersion;
  170. }, OPENSSL_VERSION_TEXT);
  171. $description = OPENSSL_VERSION_TEXT;
  172. break;
  173. case 'pcre':
  174. $prettyVersion = preg_replace('{^(\S+).*}', '$1', PCRE_VERSION);
  175. break;
  176. case 'uuid':
  177. $prettyVersion = phpversion('uuid');
  178. break;
  179. case 'xsl':
  180. $prettyVersion = LIBXSLT_DOTTED_VERSION;
  181. break;
  182. default:
  183. // None handled extensions have no special cases, skip
  184. continue 2;
  185. }
  186. try {
  187. $version = $this->versionParser->normalize($prettyVersion);
  188. } catch (\UnexpectedValueException $e) {
  189. continue;
  190. }
  191. $lib = new CompletePackage('lib-'.$name, $version, $prettyVersion);
  192. $lib->setDescription($description);
  193. $this->addPackage($lib);
  194. }
  195. $hhvmVersion = defined('HHVM_VERSION') ? HHVM_VERSION : null;
  196. if ($hhvmVersion === null && !Platform::isWindows()) {
  197. $finder = new ExecutableFinder();
  198. $hhvm = $finder->find('hhvm');
  199. if ($hhvm !== null) {
  200. $exitCode = $this->process->execute(
  201. ProcessExecutor::escape($hhvm).
  202. ' --php -d hhvm.jit=0 -r "echo HHVM_VERSION;" 2>/dev/null',
  203. $hhvmVersion
  204. );
  205. if ($exitCode !== 0) {
  206. $hhvmVersion = null;
  207. }
  208. }
  209. }
  210. if ($hhvmVersion) {
  211. try {
  212. $prettyVersion = $hhvmVersion;
  213. $version = $this->versionParser->normalize($prettyVersion);
  214. } catch (\UnexpectedValueException $e) {
  215. $prettyVersion = preg_replace('#^([^~+-]+).*$#', '$1', $hhvmVersion);
  216. $version = $this->versionParser->normalize($prettyVersion);
  217. }
  218. $hhvm = new CompletePackage('hhvm', $version, $prettyVersion);
  219. $hhvm->setDescription('The HHVM Runtime (64bit)');
  220. $this->addPackage($hhvm);
  221. }
  222. }
  223. /**
  224. * {@inheritDoc}
  225. */
  226. public function addPackage(PackageInterface $package)
  227. {
  228. // Skip if overridden
  229. if (isset($this->overrides[$package->getName()])) {
  230. $overrider = $this->findPackage($package->getName(), '*');
  231. if ($package->getVersion() === $overrider->getVersion()) {
  232. $actualText = 'same as actual';
  233. } else {
  234. $actualText = 'actual: '.$package->getPrettyVersion();
  235. }
  236. $overrider->setDescription($overrider->getDescription().', '.$actualText);
  237. return;
  238. }
  239. // Skip if PHP is overridden and we are adding a php-* package
  240. if (isset($this->overrides['php']) && 0 === strpos($package->getName(), 'php-')) {
  241. $overrider = $this->addOverriddenPackage($this->overrides['php'], $package->getPrettyName());
  242. if ($package->getVersion() === $overrider->getVersion()) {
  243. $actualText = 'same as actual';
  244. } else {
  245. $actualText = 'actual: '.$package->getPrettyVersion();
  246. }
  247. $overrider->setDescription($overrider->getDescription().', '.$actualText);
  248. return;
  249. }
  250. parent::addPackage($package);
  251. }
  252. private function addOverriddenPackage(array $override, $name = null)
  253. {
  254. $version = $this->versionParser->normalize($override['version']);
  255. $package = new CompletePackage($name ?: $override['name'], $version, $override['version']);
  256. $package->setDescription('Package overridden via config.platform');
  257. $package->setExtra(array('config.platform' => true));
  258. parent::addPackage($package);
  259. return $package;
  260. }
  261. /**
  262. * Parses the version and adds a new package to the repository
  263. *
  264. * @param string $name
  265. * @param null|string $prettyVersion
  266. */
  267. private function addExtension($name, $prettyVersion)
  268. {
  269. $extraDescription = null;
  270. try {
  271. $version = $this->versionParser->normalize($prettyVersion);
  272. } catch (\UnexpectedValueException $e) {
  273. $extraDescription = ' (actual version: '.$prettyVersion.')';
  274. if (preg_match('{^(\d+\.\d+\.\d+(?:\.\d+)?)}', $prettyVersion, $match)) {
  275. $prettyVersion = $match[1];
  276. } else {
  277. $prettyVersion = '0';
  278. }
  279. $version = $this->versionParser->normalize($prettyVersion);
  280. }
  281. $packageName = $this->buildPackageName($name);
  282. $ext = new CompletePackage($packageName, $version, $prettyVersion);
  283. $ext->setDescription('The '.$name.' PHP extension'.$extraDescription);
  284. $this->addPackage($ext);
  285. }
  286. /**
  287. * @param string $name
  288. * @return string
  289. */
  290. private function buildPackageName($name)
  291. {
  292. return 'ext-' . str_replace(' ', '-', $name);
  293. }
  294. }