ComposerRepository.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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\Package\Version\VersionParser;
  15. use Composer\Json\JsonFile;
  16. use Composer\Cache;
  17. use Composer\Config;
  18. use Composer\IO\IOInterface;
  19. use Composer\Util\RemoteFilesystem;
  20. /**
  21. * @author Jordi Boggiano <j.boggiano@seld.be>
  22. */
  23. class ComposerRepository extends ArrayRepository implements NotifiableRepositoryInterface, StreamableRepositoryInterface
  24. {
  25. protected $config;
  26. protected $url;
  27. protected $io;
  28. protected $cache;
  29. protected $notifyUrl;
  30. protected $minimalPackages;
  31. protected $loader;
  32. public function __construct(array $repoConfig, IOInterface $io, Config $config)
  33. {
  34. if (!preg_match('{^\w+://}', $repoConfig['url'])) {
  35. // assume http as the default protocol
  36. $repoConfig['url'] = 'http://'.$repoConfig['url'];
  37. }
  38. $repoConfig['url'] = rtrim($repoConfig['url'], '/');
  39. if (function_exists('filter_var') && version_compare(PHP_VERSION, '5.3.3', '>=') && !filter_var($repoConfig['url'], FILTER_VALIDATE_URL)) {
  40. throw new \UnexpectedValueException('Invalid url given for Composer repository: '.$repoConfig['url']);
  41. }
  42. $this->config = $config;
  43. $this->url = $repoConfig['url'];
  44. $this->io = $io;
  45. $this->cache = new Cache($io, $config->get('home').'/cache/'.preg_replace('{[^a-z0-9.]}i', '-', $this->url));
  46. $this->loader = new ArrayLoader();
  47. }
  48. /**
  49. * {@inheritDoc}
  50. */
  51. public function notifyInstall(PackageInterface $package)
  52. {
  53. if (!$this->notifyUrl || !$this->config->get('notify-on-install')) {
  54. return;
  55. }
  56. // TODO use an optional curl_multi pool for all the notifications
  57. $url = str_replace('%package%', $package->getPrettyName(), $this->notifyUrl);
  58. $params = array(
  59. 'version' => $package->getPrettyVersion(),
  60. 'version_normalized' => $package->getVersion(),
  61. );
  62. $opts = array('http' =>
  63. array(
  64. 'method' => 'POST',
  65. 'header' => 'Content-type: application/x-www-form-urlencoded',
  66. 'content' => http_build_query($params, '', '&'),
  67. 'timeout' => 3,
  68. )
  69. );
  70. $context = stream_context_create($opts);
  71. @file_get_contents($url, false, $context);
  72. }
  73. public function getMinimalPackages()
  74. {
  75. if (isset($this->minimalPackages)) {
  76. return $this->minimalPackages;
  77. }
  78. $repoData = $this->loadDataFromServer();
  79. $this->minimalPackages = array();
  80. $versionParser = new VersionParser;
  81. foreach ($repoData as $package) {
  82. $version = !empty($package['version_normalized']) ? $package['version_normalized'] : $versionParser->normalize($package['version']);
  83. $data = array(
  84. 'name' => strtolower($package['name']),
  85. 'repo' => $this,
  86. 'version' => $version,
  87. 'raw' => $package,
  88. );
  89. if (!empty($package['replace'])) {
  90. $data['replace'] = $package['replace'];
  91. }
  92. if (!empty($package['provide'])) {
  93. $data['provide'] = $package['provide'];
  94. }
  95. $this->minimalPackages[] = $data;
  96. }
  97. return $this->minimalPackages;
  98. }
  99. public function loadPackage(array $data, $id)
  100. {
  101. $package = $this->loader->load($data);
  102. $package->setId($id);
  103. $package->setRepository($data['repo']);
  104. return $package;
  105. }
  106. protected function initialize()
  107. {
  108. parent::initialize();
  109. $repoData = $this->loadDataFromServer();
  110. foreach ($repoData as $package) {
  111. $this->addPackage($this->loader->load($package));
  112. }
  113. }
  114. protected function loadDataFromServer()
  115. {
  116. if (!extension_loaded('openssl') && 'https' === substr($this->url, 0, 5)) {
  117. throw new \RuntimeException('You must enable the openssl extension in your php.ini to load information from '.$this->url);
  118. }
  119. try {
  120. $json = new JsonFile($this->url.'/packages.json', new RemoteFilesystem($this->io));
  121. $data = $json->read();
  122. if (!empty($data['notify'])) {
  123. if ('/' === $data['notify'][0]) {
  124. $this->notifyUrl = preg_replace('{(https?://[^/]+).*}i', '$1' . $data['notify'], $this->url);
  125. } else {
  126. $this->notifyUrl = $data['notify'];
  127. }
  128. }
  129. $this->cache->write('packages.json', json_encode($data));
  130. } catch (\Exception $e) {
  131. if ($contents = $this->cache->read('packages.json')) {
  132. $this->io->write('<warning>'.$e->getMessage().'</warning>');
  133. $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>');
  134. $data = json_decode($contents, true);
  135. } else {
  136. throw $e;
  137. }
  138. }
  139. return $this->loadIncludes($data);
  140. }
  141. protected function loadIncludes($data)
  142. {
  143. $packages = array();
  144. // legacy repo handling
  145. if (!isset($data['packages']) && !isset($data['includes'])) {
  146. foreach ($data as $pkg) {
  147. foreach ($pkg['versions'] as $metadata) {
  148. $packages[] = $metadata;
  149. }
  150. }
  151. return;
  152. }
  153. if (isset($data['packages'])) {
  154. foreach ($data['packages'] as $package => $versions) {
  155. foreach ($versions as $version => $metadata) {
  156. $packages[] = $metadata;
  157. }
  158. }
  159. }
  160. if (isset($data['includes'])) {
  161. foreach ($data['includes'] as $include => $metadata) {
  162. if ($this->cache->sha1($include) === $metadata['sha1']) {
  163. $includedData = json_decode($this->cache->read($include), true);
  164. } else {
  165. $json = new JsonFile($this->url.'/'.$include, new RemoteFilesystem($this->io));
  166. $includedData = $json->read();
  167. $this->cache->write($include, json_encode($includedData));
  168. }
  169. $packages = array_merge($packages, $this->loadIncludes($includedData));
  170. }
  171. }
  172. return $packages;
  173. }
  174. }