ComposerRepository.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. use Composer\Downloader\TransportException;
  21. /**
  22. * @author Jordi Boggiano <j.boggiano@seld.be>
  23. */
  24. class ComposerRepository extends ArrayRepository implements NotifiableRepositoryInterface, StreamableRepositoryInterface
  25. {
  26. protected $config;
  27. protected $options;
  28. protected $url;
  29. protected $io;
  30. protected $cache;
  31. protected $notifyUrl;
  32. protected $providersUrl;
  33. protected $providers = array();
  34. protected $providersByUid = array();
  35. protected $loader;
  36. private $rawData;
  37. private $minimalPackages;
  38. private $degradedMode = false;
  39. private $rootData;
  40. public function __construct(array $repoConfig, IOInterface $io, Config $config)
  41. {
  42. if (!preg_match('{^[\w.]+\??://}', $repoConfig['url'])) {
  43. // assume http as the default protocol
  44. $repoConfig['url'] = 'http://'.$repoConfig['url'];
  45. }
  46. $repoConfig['url'] = rtrim($repoConfig['url'], '/');
  47. if ('https?' === substr($repoConfig['url'], 0, 6)) {
  48. $repoConfig['url'] = (extension_loaded('openssl') ? 'https' : 'http') . substr($repoConfig['url'], 6);
  49. }
  50. if (function_exists('filter_var') && version_compare(PHP_VERSION, '5.3.3', '>=') && !filter_var($repoConfig['url'], FILTER_VALIDATE_URL)) {
  51. throw new \UnexpectedValueException('Invalid url given for Composer repository: '.$repoConfig['url']);
  52. }
  53. if (!isset($repoConfig['options'])) {
  54. $repoConfig['options'] = array();
  55. }
  56. $this->config = $config;
  57. $this->options = $repoConfig['options'];
  58. $this->url = $repoConfig['url'];
  59. $this->io = $io;
  60. $this->cache = new Cache($io, $config->get('home').'/cache/'.preg_replace('{[^a-z0-9.]}i', '-', $this->url));
  61. $this->loader = new ArrayLoader();
  62. }
  63. /**
  64. * {@inheritDoc}
  65. */
  66. public function notifyInstall(PackageInterface $package)
  67. {
  68. if (!$this->notifyUrl || !$this->config->get('notify-on-install')) {
  69. return;
  70. }
  71. // TODO use an optional curl_multi pool for all the notifications
  72. $url = str_replace('%package%', $package->getPrettyName(), $this->notifyUrl);
  73. $params = array(
  74. 'version' => $package->getPrettyVersion(),
  75. 'version_normalized' => $package->getVersion(),
  76. );
  77. $opts = array('http' =>
  78. array(
  79. 'method' => 'POST',
  80. 'header' => 'Content-type: application/x-www-form-urlencoded',
  81. 'content' => http_build_query($params, '', '&'),
  82. 'timeout' => 3,
  83. )
  84. );
  85. $context = stream_context_create($opts);
  86. @file_get_contents($url, false, $context);
  87. }
  88. /**
  89. * {@inheritDoc}
  90. */
  91. public function getMinimalPackages()
  92. {
  93. if (isset($this->minimalPackages)) {
  94. return $this->minimalPackages;
  95. }
  96. if (null === $this->rawData) {
  97. $this->rawData = $this->loadDataFromServer();
  98. }
  99. $this->minimalPackages = array();
  100. $versionParser = new VersionParser;
  101. foreach ($this->rawData as $package) {
  102. $version = !empty($package['version_normalized']) ? $package['version_normalized'] : $versionParser->normalize($package['version']);
  103. $data = array(
  104. 'name' => strtolower($package['name']),
  105. 'repo' => $this,
  106. 'version' => $version,
  107. 'raw' => $package,
  108. );
  109. if (!empty($package['replace'])) {
  110. $data['replace'] = $package['replace'];
  111. }
  112. if (!empty($package['provide'])) {
  113. $data['provide'] = $package['provide'];
  114. }
  115. // add branch aliases
  116. if ($aliasNormalized = $this->loader->getBranchAlias($package)) {
  117. $data['alias'] = preg_replace('{(\.9{7})+}', '.x', $aliasNormalized);
  118. $data['alias_normalized'] = $aliasNormalized;
  119. }
  120. $this->minimalPackages[] = $data;
  121. }
  122. return $this->minimalPackages;
  123. }
  124. /**
  125. * {@inheritDoc}
  126. */
  127. public function filterPackages($callback, $class = 'Composer\Package\Package')
  128. {
  129. if (null === $this->rawData) {
  130. $this->rawData = $this->loadDataFromServer();
  131. }
  132. foreach ($this->rawData as $package) {
  133. if (false === call_user_func($callback, $package = $this->createPackage($package, $class))) {
  134. return false;
  135. }
  136. if ($package->getAlias()) {
  137. if (false === call_user_func($callback, $this->createAliasPackage($package))) {
  138. return false;
  139. }
  140. }
  141. }
  142. return true;
  143. }
  144. /**
  145. * {@inheritDoc}
  146. */
  147. public function loadPackage(array $data)
  148. {
  149. $package = $this->createPackage($data['raw'], 'Composer\Package\Package');
  150. $package->setRepository($this);
  151. return $package;
  152. }
  153. /**
  154. * {@inheritDoc}
  155. */
  156. public function loadAliasPackage(array $data, PackageInterface $aliasOf)
  157. {
  158. $aliasPackage = $this->createAliasPackage($aliasOf, $data['version'], $data['alias']);
  159. $aliasPackage->setRepository($this);
  160. return $aliasPackage;
  161. }
  162. public function hasProviders()
  163. {
  164. $this->loadRootServerFile();
  165. return null !== $this->providersUrl;
  166. }
  167. public function whatProvides($name)
  168. {
  169. if ($name === 'php' || in_array(substr($name, 0, 4), array('ext-', 'lib-'), true) || $name === '__root__') {
  170. return array();
  171. }
  172. if (isset($this->providers[$name])) {
  173. return $this->providers[$name];
  174. }
  175. $url = str_replace('%package%', strtolower($name), $this->providersUrl);
  176. try {
  177. $json = new JsonFile($url, new RemoteFilesystem($this->io));
  178. $packages = $json->read();
  179. } catch (\RuntimeException $e) {
  180. if (!$e->getPrevious() instanceof TransportException || $e->getPrevious()->getCode() !== 404) {
  181. throw $e;
  182. }
  183. }
  184. $this->providers[$name] = array();
  185. foreach ($packages['packages'] as $name => $versions) {
  186. foreach ($versions as $version) {
  187. if (isset($this->providersByUid[$version['uid']])) {
  188. $this->providers[$name][] = $this->providersByUid[$version['uid']];
  189. } else {
  190. $package = $this->createPackage($version, 'Composer\Package\Package');
  191. $package->setRepository($this);
  192. $this->providers[$name][] = $package;
  193. $this->providersByUid[$version['uid']] = $package;
  194. if ($package->getAlias()) {
  195. $alias = $this->createAliasPackage($package);
  196. $alias->setRepository($this);
  197. $this->providers[$name][] = $alias;
  198. $this->providersByUid[$version['uid']] = $alias;
  199. }
  200. }
  201. }
  202. }
  203. return $this->providers[$name];
  204. }
  205. /**
  206. * {@inheritDoc}
  207. */
  208. protected function initialize()
  209. {
  210. parent::initialize();
  211. $repoData = $this->loadDataFromServer();
  212. foreach ($repoData as $package) {
  213. $this->addPackage($this->createPackage($package, 'Composer\Package\CompletePackage'));
  214. }
  215. }
  216. protected function loadRootServerFile()
  217. {
  218. if (null !== $this->rootData) {
  219. return $this->rootData;
  220. }
  221. if (!extension_loaded('openssl') && 'https' === substr($this->url, 0, 5)) {
  222. throw new \RuntimeException('You must enable the openssl extension in your php.ini to load information from '.$this->url);
  223. }
  224. $jsonUrlParts = parse_url($this->url);
  225. if (isset($jsonUrlParts['path']) && false !== strpos($jsonUrlParts['path'], '/packages.json')) {
  226. $jsonUrl = $this->url;
  227. } else {
  228. $jsonUrl = $this->url . '/packages.json';
  229. }
  230. $data = $this->fetchFile($jsonUrl, 'packages.json');
  231. if (!empty($data['notify'])) {
  232. if ('/' === $data['notify'][0]) {
  233. $this->notifyUrl = preg_replace('{(https?://[^/]+).*}i', '$1' . $data['notify'], $this->url);
  234. } else {
  235. $this->notifyUrl = $data['notify'];
  236. }
  237. }
  238. if (!empty($data['providers'])) {
  239. if ('/' === $data['providers'][0]) {
  240. $this->providersUrl = preg_replace('{(https?://[^/]+).*}i', '$1' . $data['providers'], $this->url);
  241. } else {
  242. $this->providersUrl = $data['providers'];
  243. }
  244. }
  245. return $this->rootData = $data;
  246. }
  247. protected function loadDataFromServer()
  248. {
  249. $data = $this->loadRootServerFile();
  250. return $this->loadIncludes($data);
  251. }
  252. protected function loadIncludes($data)
  253. {
  254. $packages = array();
  255. // legacy repo handling
  256. if (!isset($data['packages']) && !isset($data['includes'])) {
  257. foreach ($data as $pkg) {
  258. foreach ($pkg['versions'] as $metadata) {
  259. $packages[] = $metadata;
  260. }
  261. }
  262. return;
  263. }
  264. if (isset($data['packages'])) {
  265. foreach ($data['packages'] as $package => $versions) {
  266. foreach ($versions as $version => $metadata) {
  267. $packages[] = $metadata;
  268. }
  269. }
  270. }
  271. if (isset($data['includes'])) {
  272. foreach ($data['includes'] as $include => $metadata) {
  273. if ($this->cache->sha1($include) === $metadata['sha1']) {
  274. $includedData = json_decode($this->cache->read($include), true);
  275. } else {
  276. $includedData = $this->fetchFile($include);
  277. }
  278. $packages = array_merge($packages, $this->loadIncludes($includedData));
  279. }
  280. }
  281. return $packages;
  282. }
  283. protected function createPackage(array $data, $class)
  284. {
  285. try {
  286. return $this->loader->load($data, 'Composer\Package\CompletePackage');
  287. } catch (\Exception $e) {
  288. 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);
  289. }
  290. }
  291. protected function fetchFile($filename, $cacheKey = null)
  292. {
  293. if (!$cacheKey) {
  294. $cacheKey = $filename;
  295. $filename = $this->url.'/'.$filename;
  296. }
  297. $retries = 3;
  298. while ($retries--) {
  299. try {
  300. $json = new JsonFile($filename, new RemoteFilesystem($this->io, $this->options));
  301. $data = $json->read();
  302. $this->cache->write($cacheKey, json_encode($data));
  303. break;
  304. } catch (\Exception $e) {
  305. if ($contents = $this->cache->read($cacheKey)) {
  306. if (!$this->degradedMode) {
  307. $this->io->write('<warning>'.$e->getMessage().'</warning>');
  308. $this->io->write('<warning>'.$this->url.' could not be fully loaded, package information was loaded from the local cache and may be out of date</warning>');
  309. }
  310. $this->degradedMode = true;
  311. $data = json_decode($contents, true);
  312. break;
  313. } elseif (!$retries) {
  314. throw $e;
  315. }
  316. usleep(100);
  317. }
  318. }
  319. return $data;
  320. }
  321. }