ComposerRepository.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 loadPackage(array $data)
  111. {
  112. $package = $this->loader->load($data['raw'], 'Composer\Package\Package');
  113. $package->setRepository($this);
  114. return $package;
  115. }
  116. /**
  117. * {@inheritDoc}
  118. */
  119. public function loadAliasPackage(array $data, PackageInterface $aliasOf)
  120. {
  121. $aliasPackage = $this->createAliasPackage($aliasOf, $data['version'], $data['alias']);
  122. $aliasPackage->setRepository($this);
  123. return $aliasPackage;
  124. }
  125. /**
  126. * {@inheritDoc}
  127. */
  128. protected function initialize()
  129. {
  130. parent::initialize();
  131. $repoData = $this->loadDataFromServer();
  132. foreach ($repoData as $package) {
  133. $this->addPackage($this->loader->load($package, 'Composer\Package\Package'));
  134. }
  135. }
  136. protected function loadDataFromServer()
  137. {
  138. if (!extension_loaded('openssl') && 'https' === substr($this->url, 0, 5)) {
  139. throw new \RuntimeException('You must enable the openssl extension in your php.ini to load information from '.$this->url);
  140. }
  141. try {
  142. $json = new JsonFile($this->url.'/packages.json', new RemoteFilesystem($this->io));
  143. $data = $json->read();
  144. if (!empty($data['notify'])) {
  145. if ('/' === $data['notify'][0]) {
  146. $this->notifyUrl = preg_replace('{(https?://[^/]+).*}i', '$1' . $data['notify'], $this->url);
  147. } else {
  148. $this->notifyUrl = $data['notify'];
  149. }
  150. }
  151. $this->cache->write('packages.json', json_encode($data));
  152. } catch (\Exception $e) {
  153. if ($contents = $this->cache->read('packages.json')) {
  154. $this->io->write('<warning>'.$e->getMessage().'</warning>');
  155. $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>');
  156. $data = json_decode($contents, true);
  157. } else {
  158. throw $e;
  159. }
  160. }
  161. return $this->loadIncludes($data);
  162. }
  163. protected function loadIncludes($data)
  164. {
  165. $packages = array();
  166. // legacy repo handling
  167. if (!isset($data['packages']) && !isset($data['includes'])) {
  168. foreach ($data as $pkg) {
  169. foreach ($pkg['versions'] as $metadata) {
  170. $packages[] = $metadata;
  171. }
  172. }
  173. return;
  174. }
  175. if (isset($data['packages'])) {
  176. foreach ($data['packages'] as $package => $versions) {
  177. foreach ($versions as $version => $metadata) {
  178. $packages[] = $metadata;
  179. }
  180. }
  181. }
  182. if (isset($data['includes'])) {
  183. foreach ($data['includes'] as $include => $metadata) {
  184. if ($this->cache->sha1($include) === $metadata['sha1']) {
  185. $includedData = json_decode($this->cache->read($include), true);
  186. } else {
  187. $json = new JsonFile($this->url.'/'.$include, new RemoteFilesystem($this->io));
  188. $includedData = $json->read();
  189. $this->cache->write($include, json_encode($includedData));
  190. }
  191. $packages = array_merge($packages, $this->loadIncludes($includedData));
  192. }
  193. }
  194. return $packages;
  195. }
  196. }