Composer.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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;
  12. use Composer\Repository\ComposerRepository;
  13. use Composer\Repository\PlatformRepository;
  14. use Composer\Repository\GitRepository;
  15. use Composer\Repository\PearRepository;
  16. /**
  17. * @author Jordi Boggiano <j.boggiano@seld.be>
  18. */
  19. class Composer
  20. {
  21. protected $repositories = array();
  22. protected $downloaders = array();
  23. protected $installers = array();
  24. public function __construct()
  25. {
  26. $this->addRepository('Packagist', array('composer' => 'http://packagist.org'));
  27. }
  28. public function addDownloader($type, $downloader)
  29. {
  30. $type = strtolower($type);
  31. $this->downloaders[$type] = $downloader;
  32. }
  33. public function getDownloader($type)
  34. {
  35. $type = strtolower($type);
  36. if (!isset($this->downloaders[$type])) {
  37. throw new \UnexpectedValueException('Unknown source type: '.$type);
  38. }
  39. return $this->downloaders[$type];
  40. }
  41. public function addInstaller($type, $installer)
  42. {
  43. $type = strtolower($type);
  44. $this->installers[$type] = $installer;
  45. }
  46. public function getInstaller($type)
  47. {
  48. $type = strtolower($type);
  49. if (!isset($this->installers[$type])) {
  50. throw new \UnexpectedValueException('Unknown dependency type: '.$type);
  51. }
  52. return $this->installers[$type];
  53. }
  54. public function addRepository($name, $spec)
  55. {
  56. if (null === $spec) {
  57. unset($this->repositories[$name]);
  58. }
  59. if (is_array($spec) && count($spec) === 1) {
  60. return $this->repositories[$name] = $this->createRepository($name, key($spec), current($spec));
  61. }
  62. throw new \UnexpectedValueException('Invalid repositories specification '.json_encode($spec).', should be: {"type": "url"}');
  63. }
  64. public function getRepositories()
  65. {
  66. return $this->repositories;
  67. }
  68. public function createRepository($name, $type, $spec)
  69. {
  70. if (is_string($spec)) {
  71. $spec = array('url' => $spec);
  72. }
  73. $spec['url'] = rtrim($spec['url'], '/');
  74. switch ($type) {
  75. case 'git-bare':
  76. case 'git-multi':
  77. throw new \Exception($type.' repositories not supported yet');
  78. break;
  79. case 'git':
  80. return new GitRepository($spec['url']);
  81. case 'composer':
  82. return new ComposerRepository($spec['url']);
  83. case 'pear':
  84. return new PearRepository($spec['url'], $name);
  85. default:
  86. throw new \UnexpectedValueException('Unknown repository type: '.$type.', could not create repository '.$name);
  87. }
  88. }
  89. }