PlatformRepository.php 4.5 KB

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