ComposerRepository.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 $loader;
  31. private $rawData;
  32. private $minimalPackages;
  33. public function __construct(array $repoConfig, IOInterface $io, Config $config)
  34. {
  35. if (!preg_match('{^[\w.]+://}', $repoConfig['url'])) {
  36. // assume http as the default protocol
  37. $repoConfig['url'] = 'http://'.$repoConfig['url'];
  38. }
  39. $repoConfig['url'] = rtrim($repoConfig['url'], '/');
  40. if (function_exists('filter_var') && version_compare(PHP_VERSION, '5.3.3', '>=') && !filter_var($repoConfig['url'], FILTER_VALIDATE_URL)) {
  41. throw new \UnexpectedValueException('Invalid url given for Composer repository: '.$repoConfig['url']);
  42. }
  43. $this->config = $config;
  44. $this->url = $repoConfig['url'];
  45. $this->io = $io;
  46. $this->cache = new Cache($io, $config->get('home').'/cache/'.preg_replace('{[^a-z0-9.]}i', '-', $this->url));
  47. $this->loader = new ArrayLoader();
  48. }
  49. /**
  50. * {@inheritDoc}
  51. */
  52. public function notifyInstall(PackageInterface $package)
  53. {
  54. if (!$this->notifyUrl || !$this->config->get('notify-on-install')) {
  55. return;
  56. }
  57. // TODO use an optional curl_multi pool for all the notifications
  58. $url = str_replace('%package%', $package->getPrettyName(), $this->notifyUrl);
  59. $params = array(
  60. 'version' => $package->getPrettyVersion(),
  61. 'version_normalized' => $package->getVersion(),
  62. );
  63. $opts = array('http' =>
  64. array(
  65. 'method' => 'POST',
  66. 'header' => 'Content-type: application/x-www-form-urlencoded',
  67. 'content' => http_build_query($params, '', '&'),
  68. 'timeout' => 3,
  69. )
  70. );
  71. $context = stream_context_create($opts);
  72. @file_get_contents($url, false, $context);
  73. }
  74. /**
  75. * {@inheritDoc}
  76. */
  77. public function getMinimalPackages()
  78. {
  79. if (isset($this->minimalPackages)) {
  80. return $this->minimalPackages;
  81. }
  82. if (null === $this->rawData) {
  83. $this->rawData = $this->loadDataFromServer();
  84. }
  85. $this->minimalPackages = array();
  86. $versionParser = new VersionParser;
  87. foreach ($this->rawData as $package) {
  88. $version = !empty($package['version_normalized']) ? $package['version_normalized'] : $versionParser->normalize($package['version']);
  89. $data = array(
  90. 'name' => strtolower($package['name']),
  91. 'repo' => $this,
  92. 'version' => $version,
  93. 'raw' => $package,
  94. );
  95. if (!empty($package['replace'])) {
  96. $data['replace'] = $package['replace'];
  97. }
  98. if (!empty($package['provide'])) {
  99. $data['provide'] = $package['provide'];
  100. }
  101. // add branch aliases
  102. if ($aliasNormalized = $this->loader->getBranchAlias($package)) {
  103. $data['alias'] = preg_replace('{(\.9{7})+}', '.x', $aliasNormalized);
  104. $data['alias_normalized'] = $aliasNormalized;
  105. }
  106. $this->minimalPackages[] = $data;
  107. }
  108. return $this->minimalPackages;
  109. }
  110. /**
  111. * {@inheritDoc}
  112. */
  113. public function filterPackages($callback, $class = 'Composer\Package\Package')
  114. {
  115. if (null === $this->rawData) {
  116. $this->rawData = $this->loadDataFromServer();
  117. }
  118. foreach ($this->rawData as $package) {
  119. if (false === call_user_func($callback, $package = $this->createPackage($package, $class))) {
  120. return false;
  121. }
  122. if ($package->getAlias()) {
  123. if (false === call_user_func($callback, $this->createAliasPackage($package))) {
  124. return false;
  125. }
  126. }
  127. }
  128. return true;
  129. }
  130. /**
  131. * {@inheritDoc}
  132. */
  133. public function loadPackage(array $data)
  134. {
  135. $package = $this->createPackage($data['raw'], 'Composer\Package\Package');
  136. $package->setRepository($this);
  137. return $package;
  138. }
  139. /**
  140. * {@inheritDoc}
  141. */
  142. public function loadAliasPackage(array $data, PackageInterface $aliasOf)
  143. {
  144. $aliasPackage = $this->createAliasPackage($aliasOf, $data['version'], $data['alias']);
  145. $aliasPackage->setRepository($this);
  146. return $aliasPackage;
  147. }
  148. /**
  149. * {@inheritDoc}
  150. */
  151. protected function initialize()
  152. {
  153. parent::initialize();
  154. $repoData = $this->loadDataFromServer();
  155. foreach ($repoData as $package) {
  156. $this->addPackage($this->createPackage($package, 'Composer\Package\CompletePackage'));
  157. }
  158. }
  159. protected function loadDataFromServer()
  160. {
  161. if (!extension_loaded('openssl') && 'https' === substr($this->url, 0, 5)) {
  162. throw new \RuntimeException('You must enable the openssl extension in your php.ini to load information from '.$this->url);
  163. }
  164. try {
  165. $jsonUrlParts = parse_url($this->url);
  166. if (isset($jsonUrlParts['path']) && false !== strpos($jsonUrlParts['path'], '/packages.json')) {
  167. $jsonUrl = $this->url;
  168. } else {
  169. $jsonUrl = $this->url . '/packages.json';
  170. }
  171. $json = new JsonFile($jsonUrl, new RemoteFilesystem($this->io));
  172. $data = $json->read();
  173. if (!empty($data['notify'])) {
  174. if ('/' === $data['notify'][0]) {
  175. $this->notifyUrl = preg_replace('{(https?://[^/]+).*}i', '$1' . $data['notify'], $this->url);
  176. } else {
  177. $this->notifyUrl = $data['notify'];
  178. }
  179. }
  180. $this->cache->write('packages.json', json_encode($data));
  181. } catch (\Exception $e) {
  182. if ($contents = $this->cache->read('packages.json')) {
  183. $this->io->write('<warning>'.$e->getMessage().'</warning>');
  184. $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>');
  185. $data = json_decode($contents, true);
  186. } else {
  187. throw $e;
  188. }
  189. }
  190. return $this->loadIncludes($data);
  191. }
  192. protected function loadIncludes($data)
  193. {
  194. $packages = array();
  195. // legacy repo handling
  196. if (!isset($data['packages']) && !isset($data['includes'])) {
  197. foreach ($data as $pkg) {
  198. foreach ($pkg['versions'] as $metadata) {
  199. $packages[] = $metadata;
  200. }
  201. }
  202. return;
  203. }
  204. if (isset($data['packages'])) {
  205. foreach ($data['packages'] as $package => $versions) {
  206. foreach ($versions as $version => $metadata) {
  207. $packages[] = $metadata;
  208. }
  209. }
  210. }
  211. if (isset($data['includes'])) {
  212. foreach ($data['includes'] as $include => $metadata) {
  213. if ($this->cache->sha1($include) === $metadata['sha1']) {
  214. $includedData = json_decode($this->cache->read($include), true);
  215. } else {
  216. $json = new JsonFile($this->url.'/'.$include, new RemoteFilesystem($this->io));
  217. $includedData = $json->read();
  218. $this->cache->write($include, json_encode($includedData));
  219. }
  220. $packages = array_merge($packages, $this->loadIncludes($includedData));
  221. }
  222. }
  223. return $packages;
  224. }
  225. protected function createPackage(array $data, $class)
  226. {
  227. try {
  228. return $this->loader->load($data, 'Composer\Package\CompletePackage');
  229. } catch (\Exception $e) {
  230. throw new \RuntimeException('Could not load package '.(isset($data['name']) ? $data['name'] : json_encode($data)).' in '.$this->url.': ['.get_class($e).'] '.$e->getMessage(), 0, $e);
  231. }
  232. }
  233. }