1
0
Fork 0

Download Dist Package when the info is defined in root composer file

pull/293/head
François Pluchino 2012-02-13 17:40:54 +01:00
parent edae6d5eab
commit f5d90e1340
2 changed files with 77 additions and 35 deletions

View File

@ -26,6 +26,10 @@ abstract class FileDownloader implements DownloaderInterface
{ {
protected $io; protected $io;
private $bytesMax; private $bytesMax;
private $firstCall;
private $url;
private $fileUrl;
private $fileName;
/** /**
* Constructor. * Constructor.
@ -50,6 +54,10 @@ abstract class FileDownloader implements DownloaderInterface
*/ */
public function download(PackageInterface $package, $path) public function download(PackageInterface $package, $path)
{ {
$this->firstCall = true;
$this->url = $package->getSourceUrl();
$this->fileUrl = $package->getDistUrl();
// init the progress bar // init the progress bar
$this->bytesMax = 0; $this->bytesMax = 0;
@ -66,6 +74,7 @@ abstract class FileDownloader implements DownloaderInterface
} }
$fileName = rtrim($path.'/'.md5(time().rand()).'.'.pathinfo($url, PATHINFO_EXTENSION), '.'); $fileName = rtrim($path.'/'.md5(time().rand()).'.'.pathinfo($url, PATHINFO_EXTENSION), '.');
$this->fileName = $fileName;
$this->io->write(" - Package <info>" . $package->getName() . "</info> (<comment>" . $package->getPrettyVersion() . "</comment>)"); $this->io->write(" - Package <info>" . $package->getName() . "</info> (<comment>" . $package->getPrettyVersion() . "</comment>)");
@ -78,35 +87,8 @@ abstract class FileDownloader implements DownloaderInterface
} }
} }
// Handle system proxy $this->copy($this->url, $this->fileName, $this->fileUrl);
$params = array('http' => array()); $this->io->write('');
if (isset($_SERVER['HTTP_PROXY'])) {
// http(s):// is not supported in proxy
$proxy = str_replace(array('http://', 'https://'), array('tcp://', 'ssl://'), $_SERVER['HTTP_PROXY']);
if (0 === strpos($proxy, 'ssl:') && !extension_loaded('openssl')) {
throw new \RuntimeException('You must enable the openssl extension to use a proxy over https');
}
$params['http'] = array(
'proxy' => $proxy,
'request_fulluri' => true,
);
}
if ($this->io->hasAuthorization($package->getSourceUrl())) {
$auth = $this->io->getAuthorization($package->getSourceUrl());
$authStr = base64_encode($auth['username'] . ':' . $auth['password']);
$params['http'] = array_merge($params['http'], array('header' => "Authorization: Basic $authStr\r\n"));
}
$ctx = stream_context_create($params);
stream_context_set_params($ctx, array("notification" => array($this, 'callbackGet')));
$this->io->overwrite(" Downloading: <comment>connection...</comment>", false);
@copy($url, $fileName, $ctx);
$this->io->overwrite(" Downloading");
if (!file_exists($fileName)) { if (!file_exists($fileName)) {
throw new \UnexpectedValueException($url.' could not be saved to '.$fileName.', make sure the' throw new \UnexpectedValueException($url.' could not be saved to '.$fileName.', make sure the'
@ -171,11 +153,38 @@ abstract class FileDownloader implements DownloaderInterface
{ {
switch ($notificationCode) { switch ($notificationCode) {
case STREAM_NOTIFY_AUTH_REQUIRED: case STREAM_NOTIFY_AUTH_REQUIRED:
throw new \LogicException("Authorization is required");
break;
case STREAM_NOTIFY_FAILURE: case STREAM_NOTIFY_FAILURE:
throw new \LogicException("File not found"); // for private repository returning 404 error when the authorization is incorrect
$auth = $this->io->getAuthorization($this->url);
$ps = $this->firstCall && 404 === $messageCode
&& null === $this->io->getLastUsername()
&& null === $auth['username'];
if (404 === $messageCode && !$this->firstCall) {
throw new \RuntimeException("The '" . $this->fileUrl . "' URL not found");
}
$this->firstCall = false;
// get authorization informations
if (401 === $messageCode || $ps) {
if (!$this->io->isInteractive()) {
$mess = "The '" . $this->fileUrl . "' URL not found";
if (401 === $code || $ps) {
$mess = "The '" . $this->fileUrl . "' URL required the authorization.\nYou must be used the interactive console";
}
throw new \RuntimeException($mess);
}
$this->io->overwrite(' Authorization required:');
$username = $this->io->ask(' Username: ');
$password = $this->io->askAndHideAnswer(' Password: ');
$this->io->setAuthorization($this->url, $username, $password);
$this->copy($this->url, $this->fileName, $this->fileUrl);
}
break; break;
case STREAM_NOTIFY_FILE_SIZE_IS: case STREAM_NOTIFY_FILE_SIZE_IS:
@ -203,6 +212,39 @@ abstract class FileDownloader implements DownloaderInterface
} }
} }
protected function copy($url, $fileName, $fileUrl)
{
// Handle system proxy
$params = array('http' => array());
if (isset($_SERVER['HTTP_PROXY'])) {
// http(s):// is not supported in proxy
$proxy = str_replace(array('http://', 'https://'), array('tcp://', 'ssl://'), $_SERVER['HTTP_PROXY']);
if (0 === strpos($proxy, 'ssl:') && !extension_loaded('openssl')) {
throw new \RuntimeException('You must enable the openssl extension to use a proxy over https');
}
$params['http'] = array(
'proxy' => $proxy,
'request_fulluri' => true,
);
}
if ($this->io->hasAuthorization($url)) {
$auth = $this->io->getAuthorization($url);
$authStr = base64_encode($auth['username'] . ':' . $auth['password']);
$params['http'] = array_merge($params['http'], array('header' => "Authorization: Basic $authStr\r\n"));
}
$ctx = stream_context_create($params);
stream_context_set_params($ctx, array("notification" => array($this, 'callbackGet')));
$this->io->overwrite(" Downloading: <comment>connection...</comment>", false);
@copy($fileUrl, $fileName, $ctx);
$this->io->overwrite(" Downloading", false);
}
/** /**
* Extract file to directory * Extract file to directory
* *

View File

@ -136,7 +136,7 @@ abstract class VcsDriver
throw new \RuntimeException($mess); throw new \RuntimeException($mess);
} }
$this->io->write("Authorization for <info>" . $this->contentUrl . "</info>:"); $this->io->write(array('', "Authorization for <info>" . $this->contentUrl . "</info>:"));
$username = $this->io->ask(' Username: '); $username = $this->io->ask(' Username: ');
$password = $this->io->askAndHideAnswer(' Password: '); $password = $this->io->askAndHideAnswer(' Password: ');
$this->io->setAuthorization($this->url, $username, $password); $this->io->setAuthorization($this->url, $username, $password);