ComposerRepository.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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\Loader\ArrayLoader;
  13. use Composer\Package\PackageInterface;
  14. use Composer\Json\JsonFile;
  15. use Composer\Cache;
  16. use Composer\Config;
  17. use Composer\IO\IOInterface;
  18. use Composer\Util\RemoteFilesystem;
  19. /**
  20. * @author Jordi Boggiano <j.boggiano@seld.be>
  21. */
  22. class ComposerRepository extends ArrayRepository implements NotifiableRepositoryInterface
  23. {
  24. protected $config;
  25. protected $url;
  26. protected $io;
  27. protected $packages;
  28. protected $cache;
  29. protected $notifyUrl;
  30. public function __construct(array $repoConfig, IOInterface $io, Config $config)
  31. {
  32. if (!preg_match('{^\w+://}', $repoConfig['url'])) {
  33. // assume http as the default protocol
  34. $repoConfig['url'] = 'http://'.$repoConfig['url'];
  35. }
  36. $repoConfig['url'] = rtrim($repoConfig['url'], '/');
  37. if (function_exists('filter_var') && version_compare(PHP_VERSION, '5.3.3', '>=') && !filter_var($repoConfig['url'], FILTER_VALIDATE_URL)) {
  38. throw new \UnexpectedValueException('Invalid url given for Composer repository: '.$repoConfig['url']);
  39. }
  40. $this->config = $config;
  41. $this->url = $repoConfig['url'];
  42. $this->io = $io;
  43. $this->cache = new Cache($io, $config->get('home').'/cache/'.preg_replace('{[^a-z0-9.]}i', '-', $this->url));
  44. }
  45. /**
  46. * {@inheritDoc}
  47. */
  48. public function notifyInstall(PackageInterface $package)
  49. {
  50. if (!$this->notifyUrl || !$this->config->get('notify-on-install')) {
  51. return;
  52. }
  53. // TODO use an optional curl_multi pool for all the notifications
  54. $url = str_replace('%package%', $package->getPrettyName(), $this->notifyUrl);
  55. $params = array(
  56. 'version' => $package->getPrettyVersion(),
  57. 'version_normalized' => $package->getVersion(),
  58. );
  59. $opts = array('http' =>
  60. array(
  61. 'method' => 'POST',
  62. 'header' => 'Content-type: application/x-www-form-urlencoded',
  63. 'content' => http_build_query($params, '', '&'),
  64. 'timeout' => 3,
  65. )
  66. );
  67. $context = stream_context_create($opts);
  68. @file_get_contents($url, false, $context);
  69. }
  70. protected function initialize()
  71. {
  72. parent::initialize();
  73. try {
  74. $json = new JsonFile($this->url.'/packages.json', new RemoteFilesystem($this->io));
  75. $data = $json->read();
  76. if (!empty($data['notify'])) {
  77. $this->notifyUrl = preg_replace('{(https?://[^/]+).*}i', '$1' . $data['notify'], $this->url);
  78. }
  79. $this->cache->write('packages.json', json_encode($data));
  80. } catch (\Exception $e) {
  81. if ($contents = $this->cache->read('packages.json')) {
  82. $this->io->write('<warning>'.$this->url.' could not be loaded, package information was loaded from the local cache and may be out of date</warning>');
  83. $data = json_decode($contents, true);
  84. } else {
  85. throw $e;
  86. }
  87. }
  88. $loader = new ArrayLoader();
  89. $this->loadRepository($loader, $data);
  90. }
  91. protected function loadRepository(ArrayLoader $loader, $data)
  92. {
  93. // legacy repo handling
  94. if (!isset($data['packages']) && !isset($data['includes'])) {
  95. foreach ($data as $pkg) {
  96. foreach ($pkg['versions'] as $metadata) {
  97. $this->addPackage($loader->load($metadata));
  98. }
  99. }
  100. return;
  101. }
  102. if (isset($data['packages'])) {
  103. foreach ($data['packages'] as $package => $versions) {
  104. foreach ($versions as $version => $metadata) {
  105. $this->addPackage($loader->load($metadata));
  106. }
  107. }
  108. }
  109. if (isset($data['includes'])) {
  110. foreach ($data['includes'] as $include => $metadata) {
  111. if ($this->cache->sha1($include) === $metadata['sha1']) {
  112. $includedData = json_decode($this->cache->read($include), true);
  113. } else {
  114. $json = new JsonFile($this->url.'/'.$include, new RemoteFilesystem($this->io));
  115. $includedData = $json->read();
  116. $this->cache->write($include, json_encode($includedData));
  117. }
  118. $this->loadRepository($loader, $includedData);
  119. }
  120. }
  121. }
  122. }