BaseChannelReader.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. * @throws \UnexpectedValueException
  43. * @return \SimpleXMLElement
  44. */
  45. protected function requestContent($origin, $path)
  46. {
  47. $url = rtrim($origin, '/') . '/' . ltrim($path, '/');
  48. $content = $this->rfs->getContents($origin, $url, false);
  49. if (!$content) {
  50. throw new \UnexpectedValueException('The PEAR channel at ' . $url . ' did not respond.');
  51. }
  52. return str_replace('http://pear.php.net/rest/', 'https://pear.php.net/rest/', $content);
  53. }
  54. /**
  55. * Read xml content from remote filesystem
  56. *
  57. * @param $origin string server
  58. * @param $path string relative path to content
  59. * @throws \UnexpectedValueException
  60. * @return \SimpleXMLElement
  61. */
  62. protected function requestXml($origin, $path)
  63. {
  64. // http://components.ez.no/p/packages.xml is malformed. to read it we must ignore parsing errors.
  65. $xml = simplexml_load_string($this->requestContent($origin, $path), "SimpleXMLElement", LIBXML_NOERROR);
  66. if (false === $xml) {
  67. throw new \UnexpectedValueException(sprintf('The PEAR channel at ' . $origin . ' is broken. (Invalid XML at file `%s`)', $path));
  68. }
  69. return $xml;
  70. }
  71. }