ComposerRepository.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. /**
  74. * {@inheritDoc}
  75. */
  76. public function getMinimalPackages()
  77. {
  78. if (isset($this->minimalPackages)) {
  79. return $this->minimalPackages;
  80. }
  81. $repoData = $this->loadDataFromServer();
  82. $this->minimalPackages = array();
  83. $versionParser = new VersionParser;
  84. foreach ($repoData as $package) {
  85. $version = !empty($package['version_normalized']) ? $package['version_normalized'] : $versionParser->normalize($package['version']);
  86. $data = array(
  87. 'name' => strtolower($package['name']),
  88. 'repo' => $this,
  89. 'version' => $version,
  90. 'raw' => $package,
  91. );
  92. if (!empty($package['replace'])) {
  93. $data['replace'] = $package['replace'];
  94. }
  95. if (!empty($package['provide'])) {
  96. $data['provide'] = $package['provide'];
  97. }
  98. // add branch aliases
  99. if ($aliasNormalized = $this->loader->getBranchAlias($package)) {
  100. $data['alias'] = preg_replace('{(\.9{7})+}', '.x', $aliasNormalized);
  101. $data['alias_normalized'] = $aliasNormalized;
  102. }
  103. $this->minimalPackages[] = $data;
  104. }
  105. return $this->minimalPackages;
  106. }
  107. /**
  108. * {@inheritDoc}
  109. */
  110. public function filterPackages($callback, $class = 'Composer\Package\Package')
  111. {
  112. $repoData = $this->loadDataFromServer();
  113. foreach ($repoData as $package) {
  114. if (false === $callback($package = $this->loader->load($package, $class))) {
  115. return false;
  116. }
  117. if ($package->getAlias()) {
  118. if (false === $callback($this->createAliasPackage($package))) {
  119. return false;
  120. }
  121. }
  122. }
  123. return true;
  124. }
  125. /**
  126. * {@inheritDoc}
  127. */
  128. public function loadPackage(array $data)
  129. {
  130. $package = $this->loader->load($data['raw'], 'Composer\Package\Package');
  131. $package->setRepository($this);
  132. return $package;
  133. }
  134. /**
  135. * {@inheritDoc}
  136. */
  137. public function loadAliasPackage(array $data, PackageInterface $aliasOf)
  138. {
  139. $aliasPackage = $this->createAliasPackage($aliasOf, $data['version'], $data['alias']);
  140. $aliasPackage->setRepository($this);
  141. return $aliasPackage;
  142. }
  143. /**
  144. * {@inheritDoc}
  145. */
  146. protected function initialize()
  147. {
  148. parent::initialize();
  149. $repoData = $this->loadDataFromServer();
  150. foreach ($repoData as $package) {
  151. $this->addPackage($this->loader->load($package, 'Composer\Package\CompletePackage'));
  152. }
  153. }
  154. protected function loadDataFromServer()
  155. {
  156. if (!extension_loaded('openssl') && 'https' === substr($this->url, 0, 5)) {
  157. throw new \RuntimeException('You must enable the openssl extension in your php.ini to load information from '.$this->url);
  158. }
  159. try {
  160. $json = new JsonFile($this->url.'/packages.json', new RemoteFilesystem($this->io));
  161. $data = $json->read();
  162. if (!empty($data['notify'])) {
  163. if ('/' === $data['notify'][0]) {
  164. $this->notifyUrl = preg_replace('{(https?://[^/]+).*}i', '$1' . $data['notify'], $this->url);
  165. } else {
  166. $this->notifyUrl = $data['notify'];
  167. }
  168. }
  169. $this->cache->write('packages.json', json_encode($data));
  170. } catch (\Exception $e) {
  171. if ($contents = $this->cache->read('packages.json')) {
  172. $this->io->write('<warning>'.$e->getMessage().'</warning>');
  173. $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>');
  174. $data = json_decode($contents, true);
  175. } else {
  176. throw $e;
  177. }
  178. }
  179. return $this->loadIncludes($data);
  180. }
  181. protected function loadIncludes($data)
  182. {
  183. $packages = array();
  184. // legacy repo handling
  185. if (!isset($data['packages']) && !isset($data['includes'])) {
  186. foreach ($data as $pkg) {
  187. foreach ($pkg['versions'] as $metadata) {
  188. $packages[] = $metadata;
  189. }
  190. }
  191. return;
  192. }
  193. if (isset($data['packages'])) {
  194. foreach ($data['packages'] as $package => $versions) {
  195. foreach ($versions as $version => $metadata) {
  196. $packages[] = $metadata;
  197. }
  198. }
  199. }
  200. if (isset($data['includes'])) {
  201. foreach ($data['includes'] as $include => $metadata) {
  202. if ($this->cache->sha1($include) === $metadata['sha1']) {
  203. $includedData = json_decode($this->cache->read($include), true);
  204. } else {
  205. $json = new JsonFile($this->url.'/'.$include, new RemoteFilesystem($this->io));
  206. $includedData = $json->read();
  207. $this->cache->write($include, json_encode($includedData));
  208. }
  209. $packages = array_merge($packages, $this->loadIncludes($includedData));
  210. }
  211. }
  212. return $packages;
  213. }
  214. }