ComposerRepository.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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\Loader\ArrayLoader;
  13. use Composer\Package\Package;
  14. use Composer\Package\PackageInterface;
  15. use Composer\Package\AliasPackage;
  16. use Composer\Package\Version\VersionParser;
  17. use Composer\DependencyResolver\Pool;
  18. use Composer\Json\JsonFile;
  19. use Composer\Cache;
  20. use Composer\Config;
  21. use Composer\IO\IOInterface;
  22. use Composer\Util\RemoteFilesystem;
  23. use Composer\Plugin\PluginEvents;
  24. use Composer\Plugin\PreFileDownloadEvent;
  25. use Composer\EventDispatcher\EventDispatcher;
  26. /**
  27. * @author Jordi Boggiano <j.boggiano@seld.be>
  28. */
  29. class ComposerRepository extends ArrayRepository implements StreamableRepositoryInterface
  30. {
  31. protected $config;
  32. protected $options;
  33. protected $url;
  34. protected $baseUrl;
  35. protected $io;
  36. protected $rfs;
  37. protected $cache;
  38. protected $notifyUrl;
  39. protected $searchUrl;
  40. protected $hasProviders = false;
  41. protected $providersUrl;
  42. protected $lazyProvidersUrl;
  43. protected $providerListing;
  44. protected $providers = array();
  45. protected $providersByUid = array();
  46. protected $loader;
  47. protected $rootAliases;
  48. protected $allowSslDowngrade = false;
  49. protected $eventDispatcher;
  50. protected $sourceMirrors;
  51. protected $distMirrors;
  52. private $rawData;
  53. private $minimalPackages;
  54. private $degradedMode = false;
  55. private $rootData;
  56. public function __construct(array $repoConfig, IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null)
  57. {
  58. if (!preg_match('{^[\w.]+\??://}', $repoConfig['url'])) {
  59. // assume http as the default protocol
  60. $repoConfig['url'] = 'http://'.$repoConfig['url'];
  61. }
  62. $repoConfig['url'] = rtrim($repoConfig['url'], '/');
  63. if ('https?' === substr($repoConfig['url'], 0, 6)) {
  64. $repoConfig['url'] = (extension_loaded('openssl') ? 'https' : 'http') . substr($repoConfig['url'], 6);
  65. }
  66. $urlBits = parse_url($repoConfig['url']);
  67. if ($urlBits === false || empty($urlBits['scheme'])) {
  68. throw new \UnexpectedValueException('Invalid url given for Composer repository: '.$repoConfig['url']);
  69. }
  70. if (!isset($repoConfig['options'])) {
  71. $repoConfig['options'] = array();
  72. }
  73. if (isset($repoConfig['allow_ssl_downgrade']) && true === $repoConfig['allow_ssl_downgrade']) {
  74. $this->allowSslDowngrade = true;
  75. }
  76. $this->config = $config;
  77. $this->options = $repoConfig['options'];
  78. $this->url = $repoConfig['url'];
  79. $this->baseUrl = rtrim(preg_replace('{^(.*)(?:/packages.json)?(?:[?#].*)?$}', '$1', $this->url), '/');
  80. $this->io = $io;
  81. $this->cache = new Cache($io, $config->get('cache-repo-dir').'/'.preg_replace('{[^a-z0-9.]}i', '-', $this->url), 'a-z0-9.$');
  82. $this->loader = new ArrayLoader();
  83. $this->rfs = new RemoteFilesystem($this->io, $this->config, $this->options);
  84. $this->eventDispatcher = $eventDispatcher;
  85. }
  86. public function setRootAliases(array $rootAliases)
  87. {
  88. $this->rootAliases = $rootAliases;
  89. }
  90. /**
  91. * {@inheritDoc}
  92. */
  93. public function findPackage($name, $version)
  94. {
  95. if (!$this->hasProviders()) {
  96. return parent::findPackage($name, $version);
  97. }
  98. // normalize version & name
  99. $versionParser = new VersionParser();
  100. $version = $versionParser->normalize($version);
  101. $name = strtolower($name);
  102. foreach ($this->getProviderNames() as $providerName) {
  103. if ($name === $providerName) {
  104. $packages = $this->whatProvides(new Pool('dev'), $providerName);
  105. foreach ($packages as $package) {
  106. if ($name == $package->getName() && $version === $package->getVersion()) {
  107. return $package;
  108. }
  109. }
  110. }
  111. }
  112. }
  113. /**
  114. * {@inheritDoc}
  115. */
  116. public function findPackages($name, $version = null)
  117. {
  118. if (!$this->hasProviders()) {
  119. return parent::findPackages($name, $version);
  120. }
  121. // normalize name
  122. $name = strtolower($name);
  123. // normalize version
  124. if (null !== $version) {
  125. $versionParser = new VersionParser();
  126. $version = $versionParser->normalize($version);
  127. }
  128. $packages = array();
  129. foreach ($this->getProviderNames() as $providerName) {
  130. if ($name === $providerName) {
  131. $packages = $this->whatProvides(new Pool('dev'), $providerName);
  132. foreach ($packages as $package) {
  133. if ($name == $package->getName() && (null === $version || $version === $package->getVersion())) {
  134. $packages[] = $package;
  135. }
  136. }
  137. }
  138. }
  139. return $packages;
  140. }
  141. public function getPackages()
  142. {
  143. if ($this->hasProviders()) {
  144. throw new \LogicException('Composer repositories that have providers can not load the complete list of packages, use getProviderNames instead.');
  145. }
  146. return parent::getPackages();
  147. }
  148. /**
  149. * {@inheritDoc}
  150. */
  151. public function getMinimalPackages()
  152. {
  153. if (isset($this->minimalPackages)) {
  154. return $this->minimalPackages;
  155. }
  156. if (null === $this->rawData) {
  157. $this->rawData = $this->loadDataFromServer();
  158. }
  159. $this->minimalPackages = array();
  160. $versionParser = new VersionParser;
  161. foreach ($this->rawData as $package) {
  162. $version = !empty($package['version_normalized']) ? $package['version_normalized'] : $versionParser->normalize($package['version']);
  163. $data = array(
  164. 'name' => strtolower($package['name']),
  165. 'repo' => $this,
  166. 'version' => $version,
  167. 'raw' => $package,
  168. );
  169. if (!empty($package['replace'])) {
  170. $data['replace'] = $package['replace'];
  171. }
  172. if (!empty($package['provide'])) {
  173. $data['provide'] = $package['provide'];
  174. }
  175. // add branch aliases
  176. if ($aliasNormalized = $this->loader->getBranchAlias($package)) {
  177. $data['alias'] = preg_replace('{(\.9{7})+}', '.x', $aliasNormalized);
  178. $data['alias_normalized'] = $aliasNormalized;
  179. }
  180. $this->minimalPackages[] = $data;
  181. }
  182. return $this->minimalPackages;
  183. }
  184. /**
  185. * {@inheritDoc}
  186. */
  187. public function search($query, $mode = 0)
  188. {
  189. $this->loadRootServerFile();
  190. if ($this->searchUrl && $mode === self::SEARCH_FULLTEXT) {
  191. $url = str_replace('%query%', $query, $this->searchUrl);
  192. $hostname = parse_url($url, PHP_URL_HOST) ?: $url;
  193. $json = $this->rfs->getContents($hostname, $url, false);
  194. $results = JsonFile::parseJson($json, $url);
  195. return $results['results'];
  196. }
  197. if ($this->hasProviders()) {
  198. $results = array();
  199. $regex = '{(?:'.implode('|', preg_split('{\s+}', $query)).')}i';
  200. foreach ($this->getProviderNames() as $name) {
  201. if (preg_match($regex, $name)) {
  202. $results[] = array('name' => $name);
  203. }
  204. }
  205. return $results;
  206. }
  207. return parent::search($query, $mode);
  208. }
  209. public function getProviderNames()
  210. {
  211. $this->loadRootServerFile();
  212. if (null === $this->providerListing) {
  213. $this->loadProviderListings($this->loadRootServerFile());
  214. }
  215. if ($this->providersUrl) {
  216. return array_keys($this->providerListing);
  217. }
  218. // BC handling for old providers-includes
  219. $providers = array();
  220. foreach (array_keys($this->providerListing) as $provider) {
  221. $providers[] = substr($provider, 2, -5);
  222. }
  223. return $providers;
  224. }
  225. /**
  226. * {@inheritDoc}
  227. */
  228. public function loadPackage(array $data)
  229. {
  230. $package = $this->createPackage($data['raw'], 'Composer\Package\Package');
  231. if ($package instanceof AliasPackage) {
  232. $package = $package->getAliasOf();
  233. }
  234. $package->setRepository($this);
  235. return $package;
  236. }
  237. protected function configurePackageTransportOptions(PackageInterface $package)
  238. {
  239. foreach ($package->getDistUrls() as $url) {
  240. if (strpos($url, $this->baseUrl) === 0) {
  241. $package->setTransportOptions($this->options);
  242. return;
  243. }
  244. }
  245. }
  246. /**
  247. * {@inheritDoc}
  248. */
  249. public function loadAliasPackage(array $data, PackageInterface $aliasOf)
  250. {
  251. $aliasPackage = $this->createAliasPackage($aliasOf, $data['version'], $data['alias']);
  252. $aliasPackage->setRepository($this);
  253. return $aliasPackage;
  254. }
  255. public function hasProviders()
  256. {
  257. $this->loadRootServerFile();
  258. return $this->hasProviders;
  259. }
  260. public function resetPackageIds()
  261. {
  262. foreach ($this->providersByUid as $package) {
  263. if ($package instanceof AliasPackage) {
  264. $package->getAliasOf()->setId(-1);
  265. }
  266. $package->setId(-1);
  267. }
  268. }
  269. public function whatProvides(Pool $pool, $name)
  270. {
  271. if (isset($this->providers[$name])) {
  272. return $this->providers[$name];
  273. }
  274. // skip platform packages
  275. if (preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $name) || '__root__' === $name) {
  276. return array();
  277. }
  278. if (null === $this->providerListing) {
  279. $this->loadProviderListings($this->loadRootServerFile());
  280. }
  281. if ($this->lazyProvidersUrl && !isset($this->providerListing[$name])) {
  282. $hash = null;
  283. $url = str_replace('%package%', $name, $this->lazyProvidersUrl);
  284. $cacheKey = false;
  285. } elseif ($this->providersUrl) {
  286. // package does not exist in this repo
  287. if (!isset($this->providerListing[$name])) {
  288. return array();
  289. }
  290. $hash = $this->providerListing[$name]['sha256'];
  291. $url = str_replace(array('%package%', '%hash%'), array($name, $hash), $this->providersUrl);
  292. $cacheKey = 'provider-'.strtr($name, '/', '$').'.json';
  293. } else {
  294. // BC handling for old providers-includes
  295. $url = 'p/'.$name.'.json';
  296. // package does not exist in this repo
  297. if (!isset($this->providerListing[$url])) {
  298. return array();
  299. }
  300. $hash = $this->providerListing[$url]['sha256'];
  301. $cacheKey = null;
  302. }
  303. if ($cacheKey && $this->cache->sha256($cacheKey) === $hash) {
  304. $packages = json_decode($this->cache->read($cacheKey), true);
  305. } else {
  306. $packages = $this->fetchFile($url, $cacheKey, $hash);
  307. }
  308. $this->providers[$name] = array();
  309. foreach ($packages['packages'] as $versions) {
  310. foreach ($versions as $version) {
  311. // avoid loading the same objects twice
  312. if (isset($this->providersByUid[$version['uid']])) {
  313. // skip if already assigned
  314. if (!isset($this->providers[$name][$version['uid']])) {
  315. // expand alias in two packages
  316. if ($this->providersByUid[$version['uid']] instanceof AliasPackage) {
  317. $this->providers[$name][$version['uid']] = $this->providersByUid[$version['uid']]->getAliasOf();
  318. $this->providers[$name][$version['uid'].'-alias'] = $this->providersByUid[$version['uid']];
  319. } else {
  320. $this->providers[$name][$version['uid']] = $this->providersByUid[$version['uid']];
  321. }
  322. // check for root aliases
  323. if (isset($this->providersByUid[$version['uid'].'-root'])) {
  324. $this->providers[$name][$version['uid'].'-root'] = $this->providersByUid[$version['uid'].'-root'];
  325. }
  326. }
  327. } else {
  328. if (isset($version['provide']) || isset($version['replace'])) {
  329. // collect names
  330. $names = array(
  331. strtolower($version['name']) => true,
  332. );
  333. if (isset($version['provide'])) {
  334. foreach ($version['provide'] as $target => $constraint) {
  335. $names[strtolower($target)] = true;
  336. }
  337. }
  338. if (isset($version['replace'])) {
  339. foreach ($version['replace'] as $target => $constraint) {
  340. $names[strtolower($target)] = true;
  341. }
  342. }
  343. $names = array_keys($names);
  344. } else {
  345. $names = array(strtolower($version['name']));
  346. }
  347. if (!$pool->isPackageAcceptable(strtolower($version['name']), VersionParser::parseStability($version['version']))) {
  348. continue;
  349. }
  350. // load acceptable packages in the providers
  351. $package = $this->createPackage($version, 'Composer\Package\Package');
  352. $package->setRepository($this);
  353. if ($package instanceof AliasPackage) {
  354. $aliased = $package->getAliasOf();
  355. $aliased->setRepository($this);
  356. $this->providers[$name][$version['uid']] = $aliased;
  357. $this->providers[$name][$version['uid'].'-alias'] = $package;
  358. // override provider with its alias so it can be expanded in the if block above
  359. $this->providersByUid[$version['uid']] = $package;
  360. } else {
  361. $this->providers[$name][$version['uid']] = $package;
  362. $this->providersByUid[$version['uid']] = $package;
  363. }
  364. // handle root package aliases
  365. unset($rootAliasData);
  366. if (isset($this->rootAliases[$package->getName()][$package->getVersion()])) {
  367. $rootAliasData = $this->rootAliases[$package->getName()][$package->getVersion()];
  368. } elseif ($package instanceof AliasPackage && isset($this->rootAliases[$package->getName()][$package->getAliasOf()->getVersion()])) {
  369. $rootAliasData = $this->rootAliases[$package->getName()][$package->getAliasOf()->getVersion()];
  370. }
  371. if (isset($rootAliasData)) {
  372. $alias = $this->createAliasPackage($package, $rootAliasData['alias_normalized'], $rootAliasData['alias']);
  373. $alias->setRepository($this);
  374. $this->providers[$name][$version['uid'].'-root'] = $alias;
  375. $this->providersByUid[$version['uid'].'-root'] = $alias;
  376. }
  377. }
  378. }
  379. }
  380. return $this->providers[$name];
  381. }
  382. /**
  383. * {@inheritDoc}
  384. */
  385. protected function initialize()
  386. {
  387. parent::initialize();
  388. $repoData = $this->loadDataFromServer();
  389. foreach ($repoData as $package) {
  390. $this->addPackage($this->createPackage($package, 'Composer\Package\CompletePackage'));
  391. }
  392. }
  393. /**
  394. * Adds a new package to the repository
  395. *
  396. * @param PackageInterface $package
  397. */
  398. public function addPackage(PackageInterface $package)
  399. {
  400. parent::addPackage($package);
  401. $this->configurePackageTransportOptions($package);
  402. }
  403. protected function loadRootServerFile()
  404. {
  405. if (null !== $this->rootData) {
  406. return $this->rootData;
  407. }
  408. if (!extension_loaded('openssl') && 'https' === substr($this->url, 0, 5)) {
  409. throw new \RuntimeException('You must enable the openssl extension in your php.ini to load information from '.$this->url);
  410. }
  411. $jsonUrlParts = parse_url($this->url);
  412. if (isset($jsonUrlParts['path']) && false !== strpos($jsonUrlParts['path'], '/packages.json')) {
  413. $jsonUrl = $this->url;
  414. } else {
  415. $jsonUrl = $this->url . '/packages.json';
  416. }
  417. $data = $this->fetchFile($jsonUrl, 'packages.json');
  418. if (!empty($data['notify-batch'])) {
  419. $this->notifyUrl = $this->canonicalizeUrl($data['notify-batch']);
  420. } elseif (!empty($data['notify_batch'])) {
  421. // TODO remove this BC notify_batch support
  422. $this->notifyUrl = $this->canonicalizeUrl($data['notify_batch']);
  423. } elseif (!empty($data['notify'])) {
  424. $this->notifyUrl = $this->canonicalizeUrl($data['notify']);
  425. }
  426. if (!empty($data['search'])) {
  427. $this->searchUrl = $this->canonicalizeUrl($data['search']);
  428. }
  429. if (!empty($data['mirrors'])) {
  430. foreach ($data['mirrors'] as $mirror) {
  431. if (!empty($mirror['git-url'])) {
  432. $this->sourceMirrors['git'][] = array('url' => $mirror['git-url'], 'preferred' => !empty($mirror['preferred']));
  433. }
  434. if (!empty($mirror['hg-url'])) {
  435. $this->sourceMirrors['hg'][] = array('url' => $mirror['hg-url'], 'preferred' => !empty($mirror['preferred']));
  436. }
  437. if (!empty($mirror['dist-url'])) {
  438. $this->distMirrors[] = array('url' => $mirror['dist-url'], 'preferred' => !empty($mirror['preferred']));
  439. }
  440. }
  441. }
  442. if (!empty($data['warning'])) {
  443. $this->io->write('<warning>Warning from '.$this->url.': '.$data['warning'].'</warning>');
  444. }
  445. if (!empty($data['providers-lazy-url'])) {
  446. $this->lazyProvidersUrl = $this->canonicalizeUrl($data['providers-lazy-url']);
  447. $this->hasProviders = true;
  448. }
  449. if ($this->allowSslDowngrade) {
  450. $this->url = str_replace('https://', 'http://', $this->url);
  451. }
  452. if (!empty($data['providers-url'])) {
  453. $this->providersUrl = $this->canonicalizeUrl($data['providers-url']);
  454. $this->hasProviders = true;
  455. }
  456. if (!empty($data['providers']) || !empty($data['providers-includes'])) {
  457. $this->hasProviders = true;
  458. }
  459. return $this->rootData = $data;
  460. }
  461. protected function canonicalizeUrl($url)
  462. {
  463. if ('/' === $url[0]) {
  464. return preg_replace('{(https?://[^/]+).*}i', '$1' . $url, $this->url);
  465. }
  466. return $url;
  467. }
  468. protected function loadDataFromServer()
  469. {
  470. $data = $this->loadRootServerFile();
  471. return $this->loadIncludes($data);
  472. }
  473. protected function loadProviderListings($data)
  474. {
  475. if (isset($data['providers'])) {
  476. if (!is_array($this->providerListing)) {
  477. $this->providerListing = array();
  478. }
  479. $this->providerListing = array_merge($this->providerListing, $data['providers']);
  480. }
  481. if ($this->providersUrl && isset($data['provider-includes'])) {
  482. $includes = $data['provider-includes'];
  483. foreach ($includes as $include => $metadata) {
  484. $url = $this->baseUrl . '/' . str_replace('%hash%', $metadata['sha256'], $include);
  485. $cacheKey = str_replace(array('%hash%','$'), '', $include);
  486. if ($this->cache->sha256($cacheKey) === $metadata['sha256']) {
  487. $includedData = json_decode($this->cache->read($cacheKey), true);
  488. } else {
  489. $includedData = $this->fetchFile($url, $cacheKey, $metadata['sha256']);
  490. }
  491. $this->loadProviderListings($includedData);
  492. }
  493. } elseif (isset($data['providers-includes'])) {
  494. // BC layer for old-style providers-includes
  495. $includes = $data['providers-includes'];
  496. foreach ($includes as $include => $metadata) {
  497. if ($this->cache->sha256($include) === $metadata['sha256']) {
  498. $includedData = json_decode($this->cache->read($include), true);
  499. } else {
  500. $includedData = $this->fetchFile($include, null, $metadata['sha256']);
  501. }
  502. $this->loadProviderListings($includedData);
  503. }
  504. }
  505. }
  506. protected function loadIncludes($data)
  507. {
  508. $packages = array();
  509. // legacy repo handling
  510. if (!isset($data['packages']) && !isset($data['includes'])) {
  511. foreach ($data as $pkg) {
  512. foreach ($pkg['versions'] as $metadata) {
  513. $packages[] = $metadata;
  514. }
  515. }
  516. return $packages;
  517. }
  518. if (isset($data['packages'])) {
  519. foreach ($data['packages'] as $package => $versions) {
  520. foreach ($versions as $version => $metadata) {
  521. $packages[] = $metadata;
  522. }
  523. }
  524. }
  525. if (isset($data['includes'])) {
  526. foreach ($data['includes'] as $include => $metadata) {
  527. if ($this->cache->sha1($include) === $metadata['sha1']) {
  528. $includedData = json_decode($this->cache->read($include), true);
  529. } else {
  530. $includedData = $this->fetchFile($include);
  531. }
  532. $packages = array_merge($packages, $this->loadIncludes($includedData));
  533. }
  534. }
  535. return $packages;
  536. }
  537. protected function createPackage(array $data, $class)
  538. {
  539. try {
  540. if (!isset($data['notification-url'])) {
  541. $data['notification-url'] = $this->notifyUrl;
  542. }
  543. $package = $this->loader->load($data, 'Composer\Package\CompletePackage');
  544. if (isset($this->sourceMirrors[$package->getSourceType()])) {
  545. $package->setSourceMirrors($this->sourceMirrors[$package->getSourceType()]);
  546. }
  547. $package->setDistMirrors($this->distMirrors);
  548. $this->configurePackageTransportOptions($package);
  549. return $package;
  550. } catch (\Exception $e) {
  551. throw new \RuntimeException('Could not load package '.(isset($data['name']) ? $data['name'] : json_encode($data)).' in '.$this->url.': ['.get_class($e).'] '.$e->getMessage(), 0, $e);
  552. }
  553. }
  554. protected function fetchFile($filename, $cacheKey = null, $sha256 = null)
  555. {
  556. if (null === $cacheKey) {
  557. $cacheKey = $filename;
  558. $filename = $this->baseUrl.'/'.$filename;
  559. }
  560. $retries = 3;
  561. while ($retries--) {
  562. try {
  563. $preFileDownloadEvent = new PreFileDownloadEvent(PluginEvents::PRE_FILE_DOWNLOAD, $this->rfs, $filename);
  564. if ($this->eventDispatcher) {
  565. $this->eventDispatcher->dispatch($preFileDownloadEvent->getName(), $preFileDownloadEvent);
  566. }
  567. $hostname = parse_url($filename, PHP_URL_HOST) ?: $filename;
  568. $json = $preFileDownloadEvent->getRemoteFilesystem()->getContents($hostname, $filename, false);
  569. if ($sha256 && $sha256 !== hash('sha256', $json)) {
  570. if ($retries) {
  571. usleep(100000);
  572. continue;
  573. }
  574. // TODO use scarier wording once we know for sure it doesn't do false positives anymore
  575. throw new RepositorySecurityException('The contents of '.$filename.' do not match its signature. This should indicate a man-in-the-middle attack. Try running composer again and report this if you think it is a mistake.');
  576. }
  577. $data = JsonFile::parseJson($json, $filename);
  578. if ($cacheKey) {
  579. $this->cache->write($cacheKey, $json);
  580. }
  581. break;
  582. } catch (\Exception $e) {
  583. if ($retries) {
  584. usleep(100000);
  585. continue;
  586. }
  587. if ($e instanceof RepositorySecurityException) {
  588. throw $e;
  589. }
  590. if ($cacheKey && ($contents = $this->cache->read($cacheKey))) {
  591. if (!$this->degradedMode) {
  592. $this->io->write('<warning>'.$e->getMessage().'</warning>');
  593. $this->io->write('<warning>'.$this->url.' could not be fully loaded, package information was loaded from the local cache and may be out of date</warning>');
  594. }
  595. $this->degradedMode = true;
  596. $data = JsonFile::parseJson($contents, $this->cache->getRoot().$cacheKey);
  597. break;
  598. }
  599. throw $e;
  600. }
  601. }
  602. return $data;
  603. }
  604. }