PlatformRepository.php 11 KB

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