ChannelReader.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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\Pear;
  12. use Composer\Util\RemoteFilesystem;
  13. /**
  14. * PEAR Channel package reader.
  15. *
  16. * Reads channel packages info from and builds Package's
  17. *
  18. * @author Alexey Prilipko <palex@farpost.com>
  19. */
  20. class ChannelReader extends BaseChannelReader
  21. {
  22. /** @var array of ('xpath test' => 'rest implementation') */
  23. private $readerMap;
  24. public function __construct(RemoteFilesystem $rfs)
  25. {
  26. parent::__construct($rfs);
  27. $rest10reader = new ChannelRest10Reader($rfs);
  28. $rest11reader = new ChannelRest11Reader($rfs);
  29. $this->readerMap = array(
  30. 'REST1.3' => $rest11reader,
  31. 'REST1.2' => $rest11reader,
  32. 'REST1.1' => $rest11reader,
  33. 'REST1.0' => $rest10reader,
  34. );
  35. }
  36. /**
  37. * Reads PEAR channel through REST interface and builds list of packages
  38. *
  39. * @param string $url PEAR Channel url
  40. * @throws \UnexpectedValueException
  41. * @return ChannelInfo
  42. */
  43. public function read($url)
  44. {
  45. $xml = $this->requestXml($url, "/channel.xml");
  46. $channelName = (string) $xml->name;
  47. $channelAlias = (string) $xml->suggestedalias;
  48. $supportedVersions = array_keys($this->readerMap);
  49. $selectedRestVersion = $this->selectRestVersion($xml, $supportedVersions);
  50. if (!$selectedRestVersion) {
  51. throw new \UnexpectedValueException(sprintf('PEAR repository %s does not supports any of %s protocols.', $url, implode(', ', $supportedVersions)));
  52. }
  53. $reader = $this->readerMap[$selectedRestVersion['version']];
  54. $packageDefinitions = $reader->read($selectedRestVersion['baseUrl']);
  55. return new ChannelInfo($channelName, $channelAlias, $packageDefinitions);
  56. }
  57. /**
  58. * Reads channel supported REST interfaces and selects one of them
  59. *
  60. * @param \SimpleXMLElement $channelXml
  61. * @param string[] $supportedVersions supported PEAR REST protocols
  62. * @return array|null hash with selected version and baseUrl
  63. */
  64. private function selectRestVersion($channelXml, $supportedVersions)
  65. {
  66. $channelXml->registerXPathNamespace('ns', self::CHANNEL_NS);
  67. foreach ($supportedVersions as $version) {
  68. $xpathTest = "ns:servers/ns:*/ns:rest/ns:baseurl[@type='{$version}']";
  69. $testResult = $channelXml->xpath($xpathTest);
  70. foreach ($testResult as $result) {
  71. // Choose first https:// option.
  72. $result = (string) $result;
  73. if (preg_match('{^https://}i', $result)) {
  74. return array('version' => $version, 'baseUrl' => $result);
  75. }
  76. }
  77. // Fallback to non-https if it does not exist.
  78. if (count($testResult) > 0) {
  79. return array('version' => $version, 'baseUrl' => (string) $testResult[0]);
  80. }
  81. }
  82. return null;
  83. }
  84. }