Add Authentification in FileDownloader
parent
a10f92aafe
commit
7888ec5313
|
@ -111,7 +111,7 @@ EOT
|
||||||
$request->install($package->getName(), $constraint);
|
$request->install($package->getName(), $constraint);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$output->writeln('<info>Installing dependencies.</info>');
|
$output->writeln('<info>Installing dependencies</info>');
|
||||||
|
|
||||||
$links = $this->collectLinks($input, $composer->getPackage());
|
$links = $this->collectLinks($input, $composer->getPackage());
|
||||||
|
|
||||||
|
|
|
@ -99,11 +99,11 @@ class Application extends BaseApplication
|
||||||
$file = new JsonFile($composerFile);
|
$file = new JsonFile($composerFile);
|
||||||
if (!$file->exists()) {
|
if (!$file->exists()) {
|
||||||
if ($composerFile === 'composer.json') {
|
if ($composerFile === 'composer.json') {
|
||||||
echo 'Composer could not find a composer.json file in '.getcwd().PHP_EOL;
|
$this->io->writeln('Composer could not find a composer.json file in '.getcwd());
|
||||||
} else {
|
} else {
|
||||||
echo 'Composer could not find the config file: '.$composerFile.PHP_EOL;
|
$this->io->writeln('Composer could not find the config file: '.$composerFile);
|
||||||
}
|
}
|
||||||
echo 'To initialize a project, please create a composer.json file as described on the http://packagist.org/ "Getting Started" section'.PHP_EOL;
|
$this->io->writeln('To initialize a project, please create a composer.json file as described on the http://packagist.org/ "Getting Started" section');
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -77,29 +77,19 @@ abstract class FileDownloader implements DownloaderInterface
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle system proxy
|
$auth = $this->io->getAuthentification($package->getSourceUrl());
|
||||||
$ctx = stream_context_create();
|
|
||||||
|
|
||||||
if (isset($_SERVER['HTTP_PROXY'])) {
|
if (isset($_SERVER['HTTP_PROXY'])) {
|
||||||
// http(s):// is not supported in proxy
|
// http(s):// is not supported in proxy
|
||||||
$proxy = str_replace(array('http://', 'https://'), array('tcp://', 'ssl://'), $_SERVER['HTTP_PROXY']);
|
$proxy = str_replace(array('http://', 'https://'), array('tcp://', 'ssl://'), $_SERVER['HTTP_PROXY']);
|
||||||
|
|
||||||
if (0 === strpos($proxy, 'ssl:') && !extension_loaded('openssl')) {
|
if (0 === strpos($proxy, 'ssl:') && !extension_loaded('openssl')) {
|
||||||
throw new \RuntimeException('You must enable the openssl extension to use a proxy over https');
|
throw new \RuntimeException('You must enable the openssl extension to use a proxy over https');
|
||||||
}
|
}
|
||||||
|
|
||||||
$ctx = stream_context_create(array(
|
|
||||||
'http' => array(
|
|
||||||
'proxy' => $proxy,
|
|
||||||
'request_fulluri' => true,
|
|
||||||
),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
stream_context_set_params($ctx, array("notification" => array($this, 'callbackDownload')));
|
|
||||||
|
|
||||||
$this->io->overwrite(" Downloading: <comment>connection...</comment>", 80);
|
$this->io->overwrite(" Downloading: <comment>connection...</comment>", 80);
|
||||||
copy($url, $fileName, $ctx);
|
$this->copy($url, $fileName, $auth['username'], $auth['password']);
|
||||||
$this->io->overwriteln(" Downloading", 80);
|
$this->io->overwriteln(" Downloading", 80);
|
||||||
|
|
||||||
if (!file_exists($fileName)) {
|
if (!file_exists($fileName)) {
|
||||||
|
@ -156,54 +146,90 @@ abstract class FileDownloader implements DownloaderInterface
|
||||||
/**
|
/**
|
||||||
* Download notification action.
|
* Download notification action.
|
||||||
*
|
*
|
||||||
* @param integer $notificationCode The notification code
|
* @param integer $sizeTotal The total size
|
||||||
* @param integer $severity The severity level
|
* @param integer $sizeLoaded The loaded size
|
||||||
* @param string $message The message
|
|
||||||
* @param integer $messageCode The message code
|
|
||||||
* @param integer $bytesTransferred The loaded size
|
|
||||||
* @param integer $bytesMax The total size
|
|
||||||
*/
|
*/
|
||||||
protected function callbackDownload($notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax)
|
protected function callbackDownload($sizeTotal, $sizeLoaded)
|
||||||
{
|
{
|
||||||
switch ($notificationCode) {
|
if ($sizeTotal > 1024) {
|
||||||
case STREAM_NOTIFY_AUTH_REQUIRED:
|
$progression = 0;
|
||||||
break;
|
|
||||||
|
|
||||||
case STREAM_NOTIFY_FILE_SIZE_IS:
|
|
||||||
if ($this->bytesMax < $bytesMax) {
|
|
||||||
$this->bytesMax = $bytesMax;
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
case STREAM_NOTIFY_PROGRESS:
|
|
||||||
if ($this->bytesMax > 0) {
|
|
||||||
$progression = 0;
|
|
||||||
|
|
||||||
if ($this->bytesMax > 0) {
|
if ($sizeTotal > 0) {
|
||||||
$progression = ($bytesTransferred / $this->bytesMax * 100);
|
$progression = ($sizeLoaded / $sizeTotal * 100);
|
||||||
}
|
}
|
||||||
|
|
||||||
$levels = array(0, 5, 10, 15, 20, 25, 30, 35, 40, 35, 50, 55, 60,
|
$levels = array(0, 5, 10, 15, 20, 25, 30, 35, 40, 35, 50, 55, 60,
|
||||||
65, 70, 75, 80, 85, 90, 95, 100);
|
65, 70, 75, 80, 85, 90, 95, 100);
|
||||||
|
|
||||||
$progression = round($progression, 0);
|
$progression = round($progression, 0);
|
||||||
|
|
||||||
if (in_array($progression, $levels)) {
|
if (in_array($progression, $levels)) {
|
||||||
$this->io->overwrite(" Downloading: <comment>$progression%</comment>", 80);
|
$this->io->overwrite(" Downloading: <comment>$progression%</comment>", 80);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
case STREAM_NOTIFY_AUTH_RESULT:
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copy the content in file directory.
|
||||||
|
*
|
||||||
|
* @param string $url The file URL
|
||||||
|
* @param string $filename The local path
|
||||||
|
* @param string $username The username
|
||||||
|
* @param string $password The password
|
||||||
|
*/
|
||||||
|
protected function copy($url, $filename, $username = null, $password = null)
|
||||||
|
{
|
||||||
|
// create directory
|
||||||
|
if (!file_exists(dirname($filename))) {
|
||||||
|
mkdir(dirname($filename), 0777, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
$fh = fopen($filename, 'c+');
|
||||||
|
|
||||||
|
// curl options
|
||||||
|
$defaults = array(
|
||||||
|
CURLOPT_RETURNTRANSFER => true,
|
||||||
|
CURLOPT_BINARYTRANSFER => true,
|
||||||
|
CURLOPT_BUFFERSIZE => 128000,
|
||||||
|
CURLOPT_FOLLOWLOCATION => true,
|
||||||
|
CURLOPT_NOPROGRESS => false,
|
||||||
|
CURLOPT_PROGRESSFUNCTION => array($this, 'callbackDownload'),
|
||||||
|
CURLOPT_URL => $url,
|
||||||
|
CURLOPT_HTTPGET => true,
|
||||||
|
CURLOPT_SSL_VERIFYPEER => false,
|
||||||
|
CURLOPT_FILE => $fh,
|
||||||
|
);
|
||||||
|
|
||||||
|
// add authorization to curl options
|
||||||
|
if (null !== $username && null !== $password) {
|
||||||
|
$defaults[CURLOPT_USERPWD] = $username . ':' . $password;
|
||||||
|
}
|
||||||
|
|
||||||
|
// init curl
|
||||||
|
$ch = curl_init();
|
||||||
|
|
||||||
|
// curl options
|
||||||
|
curl_setopt_array($ch, $defaults);
|
||||||
|
|
||||||
|
// run curl
|
||||||
|
$curl_result = curl_exec($ch);
|
||||||
|
$curl_info = curl_getinfo($ch);
|
||||||
|
$curl_errorCode = curl_errno($ch);
|
||||||
|
$curl_error = curl_error($ch);
|
||||||
|
$code = $curl_info['http_code'];
|
||||||
|
$code = null ? 0 : $code;
|
||||||
|
|
||||||
|
//close streams
|
||||||
|
curl_close($ch);
|
||||||
|
fclose($fh);
|
||||||
|
|
||||||
|
if (200 !== $code) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extract file to directory
|
* Extract file to directory
|
||||||
*
|
*
|
||||||
|
|
|
@ -23,6 +23,7 @@ abstract class VcsDriver
|
||||||
{
|
{
|
||||||
protected $url;
|
protected $url;
|
||||||
protected $io;
|
protected $io;
|
||||||
|
private $firstCall;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
|
@ -34,6 +35,7 @@ abstract class VcsDriver
|
||||||
{
|
{
|
||||||
$this->url = $url;
|
$this->url = $url;
|
||||||
$this->io = $io;
|
$this->io = $io;
|
||||||
|
$this->firstCall = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -96,8 +98,13 @@ abstract class VcsDriver
|
||||||
curl_close($ch);
|
curl_close($ch);
|
||||||
|
|
||||||
// for private repository returning 404 error when the authentification is incorrect
|
// for private repository returning 404 error when the authentification is incorrect
|
||||||
$ps = 404 === $code && null === $this->io->getLastUsername() && null === $auth['username'];
|
$ps = $this->firstCall && 404 === $code && null === $this->io->getLastUsername() && null === $auth['username'];
|
||||||
|
|
||||||
|
if ($this->firstCall) {
|
||||||
|
$this->firstCall = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// auth required
|
||||||
if (401 === $code || $ps) {
|
if (401 === $code || $ps) {
|
||||||
if (!$this->io->isInteractive()) {
|
if (!$this->io->isInteractive()) {
|
||||||
$mess = "The '$url' URL not found";
|
$mess = "The '$url' URL not found";
|
||||||
|
|
|
@ -80,6 +80,7 @@ class VcsRepository extends ArrayRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($driver->getTags() as $tag => $identifier) {
|
foreach ($driver->getTags() as $tag => $identifier) {
|
||||||
|
$this->io->overwrite('Get composer of <info>' . $this->packageName . '</info> (<comment>' . $tag . '</comment>)');
|
||||||
$parsedTag = $this->validateTag($versionParser, $tag);
|
$parsedTag = $this->validateTag($versionParser, $tag);
|
||||||
if ($parsedTag && $driver->hasComposerFile($identifier)) {
|
if ($parsedTag && $driver->hasComposerFile($identifier)) {
|
||||||
try {
|
try {
|
||||||
|
@ -87,7 +88,7 @@ class VcsRepository extends ArrayRepository
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
if (strpos($e->getMessage(), 'JSON Parse Error') !== false) {
|
if (strpos($e->getMessage(), 'JSON Parse Error') !== false) {
|
||||||
if ($debug) {
|
if ($debug) {
|
||||||
echo 'Skipped tag '.$tag.', '.$e->getMessage().PHP_EOL;
|
$this->io->writeln('Skipped tag '.$tag.', '.$e->getMessage());
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
|
@ -111,22 +112,25 @@ class VcsRepository extends ArrayRepository
|
||||||
// broken package, version doesn't match tag
|
// broken package, version doesn't match tag
|
||||||
if ($data['version_normalized'] !== $parsedTag) {
|
if ($data['version_normalized'] !== $parsedTag) {
|
||||||
if ($debug) {
|
if ($debug) {
|
||||||
echo 'Skipped tag '.$tag.', tag ('.$parsedTag.') does not match version ('.$data['version_normalized'].') in composer.json'.PHP_EOL;
|
$this->io->writeln('Skipped tag '.$tag.', tag ('.$parsedTag.') does not match version ('.$data['version_normalized'].') in composer.json');
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($debug) {
|
if ($debug) {
|
||||||
echo 'Importing tag '.$tag.' ('.$data['version_normalized'].')'.PHP_EOL;
|
$this->io->writeln('Importing tag '.$tag.' ('.$data['version_normalized'].')');
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->addPackage($loader->load($this->preProcess($driver, $data, $identifier)));
|
$this->addPackage($loader->load($this->preProcess($driver, $data, $identifier)));
|
||||||
} elseif ($debug) {
|
} elseif ($debug) {
|
||||||
echo 'Skipped tag '.$tag.', '.($parsedTag ? 'no composer file was found' : 'invalid name').PHP_EOL;
|
$this->io->writeln('Skipped tag '.$tag.', '.($parsedTag ? 'no composer file was found' : 'invalid name'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->io->overwrite('');
|
||||||
|
|
||||||
foreach ($driver->getBranches() as $branch => $identifier) {
|
foreach ($driver->getBranches() as $branch => $identifier) {
|
||||||
|
$this->io->overwrite('Get composer of <info>' . $this->packageName . '</info> (<comment>' . $branch . '</comment>)');
|
||||||
$parsedBranch = $this->validateBranch($versionParser, $branch);
|
$parsedBranch = $this->validateBranch($versionParser, $branch);
|
||||||
if ($driver->hasComposerFile($identifier)) {
|
if ($driver->hasComposerFile($identifier)) {
|
||||||
$data = $driver->getComposerInformation($identifier);
|
$data = $driver->getComposerInformation($identifier);
|
||||||
|
@ -140,7 +144,7 @@ class VcsRepository extends ArrayRepository
|
||||||
$data['version_normalized'] = $parsedBranch;
|
$data['version_normalized'] = $parsedBranch;
|
||||||
} else {
|
} else {
|
||||||
if ($debug) {
|
if ($debug) {
|
||||||
echo 'Skipped branch '.$branch.', invalid name and no composer file was found'.PHP_EOL;
|
$this->io->writeln('Skipped branch '.$branch.', invalid name and no composer file was found');
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -154,7 +158,7 @@ class VcsRepository extends ArrayRepository
|
||||||
foreach ($this->getPackages() as $package) {
|
foreach ($this->getPackages() as $package) {
|
||||||
if ($normalizedStableVersion === $package->getVersion()) {
|
if ($normalizedStableVersion === $package->getVersion()) {
|
||||||
if ($debug) {
|
if ($debug) {
|
||||||
echo 'Skipped branch '.$branch.', already tagged'.PHP_EOL;
|
$this->io->writeln('Skipped branch '.$branch.', already tagged');
|
||||||
}
|
}
|
||||||
|
|
||||||
continue 2;
|
continue 2;
|
||||||
|
@ -162,14 +166,16 @@ class VcsRepository extends ArrayRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($debug) {
|
if ($debug) {
|
||||||
echo 'Importing branch '.$branch.' ('.$data['version_normalized'].')'.PHP_EOL;
|
$this->io->writeln('Importing branch '.$branch.' ('.$data['version_normalized'].')');
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->addPackage($loader->load($this->preProcess($driver, $data, $identifier)));
|
$this->addPackage($loader->load($this->preProcess($driver, $data, $identifier)));
|
||||||
} elseif ($debug) {
|
} elseif ($debug) {
|
||||||
echo 'Skipped branch '.$branch.', no composer file was found'.PHP_EOL;
|
$this->io->writeln('Skipped branch '.$branch.', no composer file was found');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->io->overwrite('');
|
||||||
}
|
}
|
||||||
|
|
||||||
private function preProcess(VcsDriverInterface $driver, array $data, $identifier)
|
private function preProcess(VcsDriverInterface $driver, array $data, $identifier)
|
||||||
|
|
Loading…
Reference in New Issue