PlatformRepository.php 13 KB

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