PlatformRepository.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. switch ($name) {
  61. case 'curl':
  62. $curlVersion = curl_version();
  63. $prettyVersion = $curlVersion['version'];
  64. break;
  65. case 'iconv':
  66. $prettyVersion = ICONV_VERSION;
  67. break;
  68. case 'libxml':
  69. $prettyVersion = LIBXML_DOTTED_VERSION;
  70. break;
  71. case 'openssl':
  72. $prettyVersion = preg_replace_callback('{^(?:OpenSSL\s*)?([0-9.]+)([a-z]?).*}', function ($match) {
  73. return $match[1] . (empty($match[2]) ? '' : '.'.(ord($match[2]) - 96));
  74. }, OPENSSL_VERSION_TEXT);
  75. break;
  76. case 'pcre':
  77. $prettyVersion = preg_replace('{^(\S+).*}', '$1', PCRE_VERSION);
  78. break;
  79. case 'uuid':
  80. $prettyVersion = phpversion('uuid');
  81. break;
  82. case 'xsl':
  83. $prettyVersion = LIBXSLT_DOTTED_VERSION;
  84. break;
  85. default:
  86. // None handled extensions have no special cases, skip
  87. continue 2;
  88. }
  89. try {
  90. $version = $versionParser->normalize($prettyVersion);
  91. } catch (\UnexpectedValueException $e) {
  92. continue;
  93. }
  94. $lib = new CompletePackage('lib-'.$name, $version, $prettyVersion);
  95. $lib->setDescription('The '.$name.' PHP library');
  96. parent::addPackage($lib);
  97. }
  98. }
  99. }