PlatformRepository.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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\Version\VersionParser;
  14. use Composer\Plugin\PluginInterface;
  15. /**
  16. * @author Jordi Boggiano <j.boggiano@seld.be>
  17. */
  18. class PlatformRepository extends ArrayRepository
  19. {
  20. const PLATFORM_PACKAGE_REGEX = '{^(?:php(?:-64bit)?|(?:ext|lib)-[^/]+)$}i';
  21. protected function initialize()
  22. {
  23. parent::initialize();
  24. $versionParser = new VersionParser();
  25. $prettyVersion = PluginInterface::PLUGIN_API_VERSION;
  26. $version = $versionParser->normalize($prettyVersion);
  27. $composerPluginApi = new CompletePackage('composer-plugin-api', $version, $prettyVersion);
  28. $composerPluginApi->setDescription('The Composer Plugin API');
  29. parent::addPackage($composerPluginApi);
  30. try {
  31. $prettyVersion = PHP_VERSION;
  32. $version = $versionParser->normalize($prettyVersion);
  33. } catch (\UnexpectedValueException $e) {
  34. $prettyVersion = preg_replace('#^([^~+-]+).*$#', '$1', PHP_VERSION);
  35. $version = $versionParser->normalize($prettyVersion);
  36. }
  37. $php = new CompletePackage('php', $version, $prettyVersion);
  38. $php->setDescription('The PHP interpreter');
  39. parent::addPackage($php);
  40. if (PHP_INT_SIZE === 8) {
  41. $php64 = new CompletePackage('php-64bit', $version, $prettyVersion);
  42. $php64->setDescription('The PHP interpreter (64bit)');
  43. parent::addPackage($php64);
  44. }
  45. $loadedExtensions = get_loaded_extensions();
  46. // Extensions scanning
  47. foreach ($loadedExtensions as $name) {
  48. if (in_array($name, array('standard', 'Core'))) {
  49. continue;
  50. }
  51. $reflExt = new \ReflectionExtension($name);
  52. try {
  53. $prettyVersion = $reflExt->getVersion();
  54. $version = $versionParser->normalize($prettyVersion);
  55. } catch (\UnexpectedValueException $e) {
  56. $prettyVersion = '0';
  57. $version = $versionParser->normalize($prettyVersion);
  58. }
  59. $packageName = $this->buildPackageName($name);
  60. $ext = new CompletePackage($packageName, $version, $prettyVersion);
  61. $ext->setDescription('The '.$name.' PHP extension');
  62. parent::addPackage($ext);
  63. }
  64. // Another quick loop, just for possible libraries
  65. // Doing it this way to know that functions or constants exist before
  66. // relying on them.
  67. foreach ($loadedExtensions as $name) {
  68. $prettyVersion = null;
  69. switch ($name) {
  70. case 'curl':
  71. $curlVersion = curl_version();
  72. $prettyVersion = $curlVersion['version'];
  73. break;
  74. case 'iconv':
  75. $prettyVersion = ICONV_VERSION;
  76. break;
  77. case 'intl':
  78. $name = 'ICU';
  79. if (defined('INTL_ICU_VERSION')) {
  80. $prettyVersion = INTL_ICU_VERSION;
  81. } else {
  82. $reflector = new \ReflectionExtension('intl');
  83. ob_start();
  84. $reflector->info();
  85. $output = ob_get_clean();
  86. preg_match('/^ICU version => (.*)$/m', $output, $matches);
  87. $prettyVersion = $matches[1];
  88. }
  89. break;
  90. case 'libxml':
  91. $prettyVersion = LIBXML_DOTTED_VERSION;
  92. break;
  93. case 'openssl':
  94. $prettyVersion = preg_replace_callback('{^(?:OpenSSL\s*)?([0-9.]+)([a-z]?).*}', function ($match) {
  95. return $match[1] . (empty($match[2]) ? '' : '.'.(ord($match[2]) - 96));
  96. }, OPENSSL_VERSION_TEXT);
  97. break;
  98. case 'pcre':
  99. $prettyVersion = preg_replace('{^(\S+).*}', '$1', PCRE_VERSION);
  100. break;
  101. case 'uuid':
  102. $prettyVersion = phpversion('uuid');
  103. break;
  104. case 'xsl':
  105. $prettyVersion = LIBXSLT_DOTTED_VERSION;
  106. break;
  107. default:
  108. // None handled extensions have no special cases, skip
  109. continue 2;
  110. }
  111. try {
  112. $version = $versionParser->normalize($prettyVersion);
  113. } catch (\UnexpectedValueException $e) {
  114. continue;
  115. }
  116. $lib = new CompletePackage('lib-'.$name, $version, $prettyVersion);
  117. $lib->setDescription('The '.$name.' PHP library');
  118. parent::addPackage($lib);
  119. }
  120. if (defined('HPHP_VERSION')) {
  121. try {
  122. $prettyVersion = HPHP_VERSION;
  123. $version = $versionParser->normalize($prettyVersion);
  124. } catch (\UnexpectedValueException $e) {
  125. $prettyVersion = preg_replace('#^([^~+-]+).*$#', '$1', HPHP_VERSION);
  126. $version = $versionParser->normalize($prettyVersion);
  127. }
  128. $hhvm = new CompletePackage('hhvm', $version, $prettyVersion);
  129. $hhvm->setDescription('The HHVM Runtime (64bit)');
  130. parent::addPackage($hhvm);
  131. }
  132. }
  133. private function buildPackageName($name)
  134. {
  135. return 'ext-' . str_replace(' ', '-', $name);
  136. }
  137. }