ComposerRepository.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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\MemoryPackage;
  13. use Composer\Package\BasePackage;
  14. use Composer\Package\Link;
  15. use Composer\Package\LinkConstraint\VersionConstraint;
  16. /**
  17. * @author Jordi Boggiano <j.boggiano@seld.be>
  18. */
  19. class ComposerRepository extends ArrayRepository
  20. {
  21. protected $packages;
  22. public function __construct($url)
  23. {
  24. $this->url = $url;
  25. }
  26. protected function initialize()
  27. {
  28. parent::initialize();
  29. $packages = @json_decode(file_get_contents($this->url.'/packages.json'), true);
  30. if (!$packages) {
  31. throw new \UnexpectedValueException('Could not parse package list from the '.$this->url.' repository');
  32. }
  33. foreach ($packages as $data) {
  34. $this->createPackages($data);
  35. }
  36. }
  37. protected function createPackages($data)
  38. {
  39. foreach ($data['versions'] as $rev) {
  40. $version = BasePackage::parseVersion($rev['version']);
  41. $package = new MemoryPackage($rev['name'], $version['version'], $version['type']);
  42. $package->setSourceType($rev['source']['type']);
  43. $package->setSourceUrl($rev['source']['url']);
  44. if (isset($rev['license'])) {
  45. $package->setLicense($rev['license']);
  46. }
  47. $links = array(
  48. 'require',
  49. 'conflict',
  50. 'provide',
  51. 'replace',
  52. 'recommend',
  53. 'suggest',
  54. );
  55. foreach ($links as $link) {
  56. if (isset($rev[$link])) {
  57. $method = 'set'.$link.'s';
  58. $package->{$method}($this->createLinks($rev['name'], $link.'s', $rev[$link]));
  59. }
  60. }
  61. $this->addPackage($package);
  62. }
  63. }
  64. protected function createLinks($name, $description, $linkSpecs)
  65. {
  66. $links = array();
  67. foreach ($linkSpecs as $dep => $ver) {
  68. preg_match('#^([>=<~]*)([\d.]+.*)$#', $ver, $match);
  69. if (!$match[1]) {
  70. $match[1] = '=';
  71. }
  72. $constraint = new VersionConstraint($match[1], $match[2]);
  73. $links[] = new Link($name, $dep, $constraint, $description);
  74. }
  75. return $links;
  76. }
  77. }