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