PlatformRepository.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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\MemoryPackage;
  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 MemoryPackage('php', $version, $prettyVersion);
  31. $php->setDescription('The PHP interpreter');
  32. parent::addPackage($php);
  33. $loadedExtensions = get_loaded_extensions();
  34. // Extensions scanning
  35. foreach ($loadedExtensions as $name) {
  36. if (in_array($name, array('standard', 'Core'))) {
  37. continue;
  38. }
  39. $reflExt = new \ReflectionExtension($name);
  40. try {
  41. $prettyVersion = $reflExt->getVersion();
  42. $version = $versionParser->normalize($prettyVersion);
  43. } catch (\UnexpectedValueException $e) {
  44. $prettyVersion = '0';
  45. $version = $versionParser->normalize($prettyVersion);
  46. }
  47. $ext = new MemoryPackage('ext-'.$name, $version, $prettyVersion);
  48. $ext->setDescription('The '.$name.' PHP extension');
  49. parent::addPackage($ext);
  50. }
  51. // Another quick loop, just for possible libraries
  52. // Doing it this way to know that functions or constants exist before
  53. // relying on them.
  54. foreach ($loadedExtensions as $name) {
  55. switch ($name) {
  56. case 'curl':
  57. $curlVersion = curl_version();
  58. $prettyVersion = $curlVersion['version'];
  59. break;
  60. case 'iconv':
  61. $prettyVersion = ICONV_VERSION;
  62. break;
  63. case 'libxml':
  64. $prettyVersion = LIBXML_DOTTED_VERSION;
  65. break;
  66. case 'openssl':
  67. $prettyVersion = str_replace('OpenSSL', '', OPENSSL_VERSION_TEXT);
  68. $prettyVersion = trim($prettyVersion);
  69. break;
  70. case 'pcre':
  71. $prettyVersion = PCRE_VERSION;
  72. break;
  73. case 'uuid':
  74. $prettyVersion = UUID_VERSION;
  75. break;
  76. case 'xsl':
  77. $prettyVersion = LIBXSLT_DOTTED_VERSION;
  78. break;
  79. default:
  80. // None handled extensions have no special cases, skip
  81. continue;
  82. }
  83. try {
  84. $version = $versionParser->normalize($prettyVersion);
  85. } catch (\UnexpectedValueException $e) {
  86. continue;
  87. }
  88. $ext = new MemoryPackage('lib-'.$name, $version, $prettyVersion);
  89. $ext->setDescription('The '.$name.' PHP library');
  90. parent::addPackage($ext);
  91. }
  92. }
  93. }