123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <?php
- namespace Composer\Downloader;
- use Composer\Config;
- use Composer\IO\IOInterface;
- use Composer\Package\PackageInterface;
- use Composer\Package\Version\VersionParser;
- use Composer\Util\Filesystem;
- use Composer\Util\GitHub;
- use Composer\Util\RemoteFilesystem;
- class FileDownloader implements DownloaderInterface
- {
- protected $io;
- protected $config;
- protected $rfs;
- protected $filesystem;
-
- public function __construct(IOInterface $io, Config $config, RemoteFilesystem $rfs = null, Filesystem $filesystem = null)
- {
- $this->io = $io;
- $this->config = $config;
- $this->rfs = $rfs ?: new RemoteFilesystem($io);
- $this->filesystem = $filesystem ?: new Filesystem();
- }
-
- public function getInstallationSource()
- {
- return 'dist';
- }
-
- public function download(PackageInterface $package, $path)
- {
- $url = $package->getDistUrl();
- if (!$url) {
- throw new \InvalidArgumentException('The given package is missing url information');
- }
- $this->filesystem->ensureDirectoryExists($path);
- $fileName = $this->getFileName($package, $path);
- $this->io->write(" - Installing <info>" . $package->getName() . "</info> (<comment>" . VersionParser::formatVersion($package) . "</comment>)");
- $processUrl = $this->processUrl($package, $url);
- try {
- try {
- $this->rfs->copy(parse_url($processUrl, PHP_URL_HOST), $processUrl, $fileName);
- } catch (TransportException $e) {
- if (404 === $e->getCode() && 'github.com' === parse_url($processUrl, PHP_URL_HOST)) {
- $message = "\n".'Could not fetch '.$processUrl.', enter your GitHub credentials to access private repos';
- $gitHubUtil = new GitHub($this->io, $this->config, null, $this->rfs);
- if (!$gitHubUtil->authorizeOAuth('github.com')
- && (!$this->io->isInteractive() || !$gitHubUtil->authorizeOAuthInteractively('github.com', $message))
- ) {
- throw $e;
- }
- $this->rfs->copy(parse_url($processUrl, PHP_URL_HOST), $processUrl, $fileName);
- } else {
- throw $e;
- }
- }
- if (!file_exists($fileName)) {
- throw new \UnexpectedValueException($url.' could not be saved to '.$fileName.', make sure the'
- .' directory is writable and you have internet connectivity');
- }
- $checksum = $package->getDistSha1Checksum();
- if ($checksum && hash_file('sha1', $fileName) !== $checksum) {
- throw new \UnexpectedValueException('The checksum verification of the file failed (downloaded from '.$url.')');
- }
- } catch (\Exception $e) {
-
- $this->filesystem->removeDirectory($path);
- throw $e;
- }
- }
-
- public function update(PackageInterface $initial, PackageInterface $target, $path)
- {
- $this->remove($initial, $path);
- $this->download($target, $path);
- }
-
- public function remove(PackageInterface $package, $path)
- {
- $this->io->write(" - Removing <info>" . $package->getName() . "</info> (<comment>" . VersionParser::formatVersion($package) . "</comment>)");
- if (!$this->filesystem->removeDirectory($path)) {
- throw new \RuntimeException('Could not completely delete '.$path.', aborting.');
- }
- }
-
- protected function getFileName(PackageInterface $package, $path)
- {
- return $path.'/'.pathinfo(parse_url($package->getDistUrl(), PHP_URL_PATH), PATHINFO_BASENAME);
- }
-
- protected function processUrl(PackageInterface $package, $url)
- {
- if (!extension_loaded('openssl') && 0 === strpos($url, 'https:')) {
- throw new \RuntimeException('You must enable the openssl extension to download files via https');
- }
- return $url;
- }
- }
|