BaseChannelReader.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. * Base PEAR Channel reader.
  15. *
  16. * Provides xml namespaces and red
  17. *
  18. * @author Alexey Prilipko <palex@farpost.com>
  19. */
  20. abstract class BaseChannelReader
  21. {
  22. /**
  23. * PEAR REST Interface namespaces
  24. */
  25. const CHANNEL_NS = 'http://pear.php.net/channel-1.0';
  26. const ALL_CATEGORIES_NS = 'http://pear.php.net/dtd/rest.allcategories';
  27. const CATEGORY_PACKAGES_INFO_NS = 'http://pear.php.net/dtd/rest.categorypackageinfo';
  28. const ALL_PACKAGES_NS = 'http://pear.php.net/dtd/rest.allpackages';
  29. const ALL_RELEASES_NS = 'http://pear.php.net/dtd/rest.allreleases';
  30. const PACKAGE_INFO_NS = 'http://pear.php.net/dtd/rest.package';
  31. /** @var RemoteFilesystem */
  32. private $rfs;
  33. protected function __construct(RemoteFilesystem $rfs)
  34. {
  35. $this->rfs = $rfs;
  36. }
  37. /**
  38. * Read content from remote filesystem.
  39. *
  40. * @param $origin string server
  41. * @param $path string relative path to content
  42. * @return \SimpleXMLElement
  43. */
  44. protected function requestContent($origin, $path)
  45. {
  46. $url = rtrim($origin, '/') . '/' . ltrim($path, '/');
  47. $content = $this->rfs->getContents($origin, $url, false);
  48. if (!$content) {
  49. throw new \UnexpectedValueException('The PEAR channel at ' . $url . ' did not respond.');
  50. }
  51. return $content;
  52. }
  53. /**
  54. * Read xml content from remote filesystem
  55. *
  56. * @param $origin string server
  57. * @param $path string relative path to content
  58. * @return \SimpleXMLElement
  59. */
  60. protected function requestXml($origin, $path)
  61. {
  62. // http://components.ez.no/p/packages.xml is malformed. to read it we must ignore parsing errors.
  63. $xml = simplexml_load_string($this->requestContent($origin, $path), "SimpleXMLElement", LIBXML_NOERROR);
  64. if (false == $xml) {
  65. $url = rtrim($origin, '/') . '/' . ltrim($path, '/');
  66. throw new \UnexpectedValueException(sprintf('The PEAR channel at ' . $origin . ' is broken. (Invalid XML at file `%s`)', $path));
  67. }
  68. return $xml;
  69. }
  70. }