PlatformRepository.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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\Silencer;
  17. /**
  18. * @author Jordi Boggiano <j.boggiano@seld.be>
  19. */
  20. class PlatformRepository extends ArrayRepository
  21. {
  22. const PLATFORM_PACKAGE_REGEX = '{^(?:php(?:-64bit|-ipv6|-zts|-debug)?|hhvm|(?:ext|lib)-[^/]+)$}i';
  23. /**
  24. * Defines overrides so that the platform can be mocked
  25. *
  26. * Should be an array of package name => version number mappings
  27. *
  28. * @var array
  29. */
  30. private $overrides = array();
  31. public function __construct(array $packages = array(), array $overrides = array())
  32. {
  33. foreach ($overrides as $name => $version) {
  34. $this->overrides[strtolower($name)] = array('name' => $name, 'version' => $version);
  35. }
  36. parent::__construct($packages);
  37. }
  38. protected function initialize()
  39. {
  40. parent::initialize();
  41. $versionParser = new VersionParser();
  42. // Add each of the override versions as options.
  43. // Later we might even replace the extensions instead.
  44. foreach ($this->overrides as $override) {
  45. // Check that it's a platform package.
  46. if (!preg_match(self::PLATFORM_PACKAGE_REGEX, $override['name'])) {
  47. throw new \InvalidArgumentException('Invalid platform package name in config.platform: '.$override['name']);
  48. }
  49. $version = $versionParser->normalize($override['version']);
  50. $package = new CompletePackage($override['name'], $version, $override['version']);
  51. $package->setDescription('Package overridden via config.platform');
  52. $package->setExtra(array('config.platform' => true));
  53. parent::addPackage($package);
  54. }
  55. $prettyVersion = PluginInterface::PLUGIN_API_VERSION;
  56. $version = $versionParser->normalize($prettyVersion);
  57. $composerPluginApi = new CompletePackage('composer-plugin-api', $version, $prettyVersion);
  58. $composerPluginApi->setDescription('The Composer Plugin API');
  59. $this->addPackage($composerPluginApi);
  60. try {
  61. $prettyVersion = PHP_VERSION;
  62. $version = $versionParser->normalize($prettyVersion);
  63. } catch (\UnexpectedValueException $e) {
  64. $prettyVersion = preg_replace('#^([^~+-]+).*$#', '$1', PHP_VERSION);
  65. $version = $versionParser->normalize($prettyVersion);
  66. }
  67. $php = new CompletePackage('php', $version, $prettyVersion);
  68. $php->setDescription('The PHP interpreter');
  69. $this->addPackage($php);
  70. if (PHP_DEBUG) {
  71. $phpdebug = new CompletePackage('php-debug', $version, $prettyVersion);
  72. $phpdebug->setDescription('The PHP interpreter, with debugging symbols');
  73. $this->addPackage($phpdebug);
  74. }
  75. if (defined('PHP_ZTS') && PHP_ZTS) {
  76. $phpzts = new CompletePackage('php-zts', $version, $prettyVersion);
  77. $phpzts->setDescription('The PHP interpreter, with Zend Thread Safety');
  78. $this->addPackage($phpzts);
  79. }
  80. if (PHP_INT_SIZE === 8) {
  81. $php64 = new CompletePackage('php-64bit', $version, $prettyVersion);
  82. $php64->setDescription('The PHP interpreter, 64bit');
  83. $this->addPackage($php64);
  84. }
  85. // The AF_INET6 constant is only defined if ext-sockets is available but
  86. // IPv6 support might still be available.
  87. if (defined('AF_INET6') || Silencer::call('inet_pton', '::') !== false) {
  88. $phpIpv6 = new CompletePackage('php-ipv6', $version, $prettyVersion);
  89. $phpIpv6->setDescription('The PHP interpreter, with IPv6 support');
  90. $this->addPackage($phpIpv6);
  91. }
  92. $loadedExtensions = get_loaded_extensions();
  93. // Extensions scanning
  94. foreach ($loadedExtensions as $name) {
  95. if (in_array($name, array('standard', 'Core'))) {
  96. continue;
  97. }
  98. $extraDescription = null;
  99. $reflExt = new \ReflectionExtension($name);
  100. try {
  101. $prettyVersion = $reflExt->getVersion();
  102. $version = $versionParser->normalize($prettyVersion);
  103. } catch (\UnexpectedValueException $e) {
  104. $extraDescription = ' (actual version: '.$prettyVersion.')';
  105. if (preg_match('{^(\d+\.\d+\.\d+(?:\.\d+)?)}', $prettyVersion, $match)) {
  106. $prettyVersion = $match[1];
  107. } else {
  108. $prettyVersion = '0';
  109. }
  110. $version = $versionParser->normalize($prettyVersion);
  111. }
  112. $packageName = $this->buildPackageName($name);
  113. $ext = new CompletePackage($packageName, $version, $prettyVersion);
  114. $ext->setDescription('The '.$name.' PHP extension' . $extraDescription);
  115. $this->addPackage($ext);
  116. }
  117. // Another quick loop, just for possible libraries
  118. // Doing it this way to know that functions or constants exist before
  119. // relying on them.
  120. foreach ($loadedExtensions as $name) {
  121. $prettyVersion = null;
  122. $description = 'The '.$name.' PHP library';
  123. switch ($name) {
  124. case 'curl':
  125. $curlVersion = curl_version();
  126. $prettyVersion = $curlVersion['version'];
  127. break;
  128. case 'iconv':
  129. $prettyVersion = ICONV_VERSION;
  130. break;
  131. case 'intl':
  132. $name = 'ICU';
  133. if (defined('INTL_ICU_VERSION')) {
  134. $prettyVersion = INTL_ICU_VERSION;
  135. } else {
  136. $reflector = new \ReflectionExtension('intl');
  137. ob_start();
  138. $reflector->info();
  139. $output = ob_get_clean();
  140. preg_match('/^ICU version => (.*)$/m', $output, $matches);
  141. $prettyVersion = $matches[1];
  142. }
  143. break;
  144. case 'libxml':
  145. $prettyVersion = LIBXML_DOTTED_VERSION;
  146. break;
  147. case 'openssl':
  148. $prettyVersion = preg_replace_callback('{^(?:OpenSSL\s*)?([0-9.]+)([a-z]*).*}', function ($match) {
  149. if (empty($match[2])) {
  150. return $match[1];
  151. }
  152. // OpenSSL versions add another letter when they reach Z.
  153. // e.g. OpenSSL 0.9.8zh 3 Dec 2015
  154. if (!preg_match('{^z*[a-z]$}', $match[2])) {
  155. // 0.9.8abc is garbage
  156. return 0;
  157. }
  158. $len = strlen($match[2]);
  159. $patchVersion = ($len - 1) * 26; // All Z
  160. $patchVersion += ord($match[2][$len - 1]) - 96;
  161. return $match[1].'.'.$patchVersion;
  162. }, OPENSSL_VERSION_TEXT);
  163. $description = OPENSSL_VERSION_TEXT;
  164. break;
  165. case 'pcre':
  166. $prettyVersion = preg_replace('{^(\S+).*}', '$1', PCRE_VERSION);
  167. break;
  168. case 'uuid':
  169. $prettyVersion = phpversion('uuid');
  170. break;
  171. case 'xsl':
  172. $prettyVersion = LIBXSLT_DOTTED_VERSION;
  173. break;
  174. default:
  175. // None handled extensions have no special cases, skip
  176. continue 2;
  177. }
  178. try {
  179. $version = $versionParser->normalize($prettyVersion);
  180. } catch (\UnexpectedValueException $e) {
  181. continue;
  182. }
  183. $lib = new CompletePackage('lib-'.$name, $version, $prettyVersion);
  184. $lib->setDescription($description);
  185. $this->addPackage($lib);
  186. }
  187. if (defined('HHVM_VERSION')) {
  188. try {
  189. $prettyVersion = HHVM_VERSION;
  190. $version = $versionParser->normalize($prettyVersion);
  191. } catch (\UnexpectedValueException $e) {
  192. $prettyVersion = preg_replace('#^([^~+-]+).*$#', '$1', HHVM_VERSION);
  193. $version = $versionParser->normalize($prettyVersion);
  194. }
  195. $hhvm = new CompletePackage('hhvm', $version, $prettyVersion);
  196. $hhvm->setDescription('The HHVM Runtime (64bit)');
  197. $this->addPackage($hhvm);
  198. }
  199. }
  200. /**
  201. * {@inheritDoc}
  202. */
  203. public function addPackage(PackageInterface $package)
  204. {
  205. // Skip if overridden
  206. if (isset($this->overrides[strtolower($package->getName())])) {
  207. $overrider = $this->findPackage($package->getName(), '*');
  208. $overrider->setDescription($overrider->getDescription().' (actual: '.$package->getPrettyVersion().')');
  209. return;
  210. }
  211. parent::addPackage($package);
  212. }
  213. private function buildPackageName($name)
  214. {
  215. return 'ext-' . str_replace(' ', '-', $name);
  216. }
  217. }