From f5d90e134065ee6ff0efcf69edcd28c2cef48f3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Pluchino?= Date: Mon, 13 Feb 2012 17:40:54 +0100 Subject: [PATCH 1/6] Download Dist Package when the info is defined in root composer file --- src/Composer/Downloader/FileDownloader.php | 110 ++++++++++++++------- src/Composer/Repository/Vcs/VcsDriver.php | 2 +- 2 files changed, 77 insertions(+), 35 deletions(-) diff --git a/src/Composer/Downloader/FileDownloader.php b/src/Composer/Downloader/FileDownloader.php index ae2b199b2..ef6c13cc9 100644 --- a/src/Composer/Downloader/FileDownloader.php +++ b/src/Composer/Downloader/FileDownloader.php @@ -26,6 +26,10 @@ abstract class FileDownloader implements DownloaderInterface { protected $io; private $bytesMax; + private $firstCall; + private $url; + private $fileUrl; + private $fileName; /** * Constructor. @@ -50,6 +54,10 @@ abstract class FileDownloader implements DownloaderInterface */ public function download(PackageInterface $package, $path) { + $this->firstCall = true; + $this->url = $package->getSourceUrl(); + $this->fileUrl = $package->getDistUrl(); + // init the progress bar $this->bytesMax = 0; @@ -66,6 +74,7 @@ abstract class FileDownloader implements DownloaderInterface } $fileName = rtrim($path.'/'.md5(time().rand()).'.'.pathinfo($url, PATHINFO_EXTENSION), '.'); + $this->fileName = $fileName; $this->io->write(" - Package " . $package->getName() . " (" . $package->getPrettyVersion() . ")"); @@ -78,35 +87,8 @@ abstract class FileDownloader implements DownloaderInterface } } - // 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($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: connection...", false); - @copy($url, $fileName, $ctx); - $this->io->overwrite(" Downloading"); + $this->copy($this->url, $this->fileName, $this->fileUrl); + $this->io->write(''); if (!file_exists($fileName)) { throw new \UnexpectedValueException($url.' could not be saved to '.$fileName.', make sure the' @@ -170,12 +152,39 @@ abstract class FileDownloader implements DownloaderInterface protected function callbackGet($notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax) { switch ($notificationCode) { - case STREAM_NOTIFY_AUTH_REQUIRED: - throw new \LogicException("Authorization is required"); - break; + case STREAM_NOTIFY_AUTH_REQUIRED: + case STREAM_NOTIFY_FAILURE: + // 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); + } - case STREAM_NOTIFY_FAILURE: - throw new \LogicException("File not found"); + $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; 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: connection...", false); + @copy($fileUrl, $fileName, $ctx); + $this->io->overwrite(" Downloading", false); + } + /** * Extract file to directory * diff --git a/src/Composer/Repository/Vcs/VcsDriver.php b/src/Composer/Repository/Vcs/VcsDriver.php index 6addf26e7..5a25fc1be 100644 --- a/src/Composer/Repository/Vcs/VcsDriver.php +++ b/src/Composer/Repository/Vcs/VcsDriver.php @@ -136,7 +136,7 @@ abstract class VcsDriver throw new \RuntimeException($mess); } - $this->io->write("Authorization for " . $this->contentUrl . ":"); + $this->io->write(array('', "Authorization for " . $this->contentUrl . ":")); $username = $this->io->ask(' Username: '); $password = $this->io->askAndHideAnswer(' Password: '); $this->io->setAuthorization($this->url, $username, $password); From 9638247e4448470163693482c9bea058cc4fcd25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Pluchino?= Date: Tue, 14 Feb 2012 11:25:00 +0100 Subject: [PATCH 2/6] Move to Util Class --- src/Composer/Downloader/FileDownloader.php | 123 +--------------- src/Composer/Util/RemoteFilesystem.php | 157 +++++++++++++++++++++ 2 files changed, 160 insertions(+), 120 deletions(-) create mode 100644 src/Composer/Util/RemoteFilesystem.php diff --git a/src/Composer/Downloader/FileDownloader.php b/src/Composer/Downloader/FileDownloader.php index ef6c13cc9..dad74fbc4 100644 --- a/src/Composer/Downloader/FileDownloader.php +++ b/src/Composer/Downloader/FileDownloader.php @@ -11,6 +11,7 @@ namespace Composer\Downloader; +use Composer\Util\RemoteFilesystem; use Composer\IO\IOInterface; use Composer\Package\PackageInterface; use Composer\Util\Filesystem; @@ -25,11 +26,6 @@ use Composer\Util\Filesystem; abstract class FileDownloader implements DownloaderInterface { protected $io; - private $bytesMax; - private $firstCall; - private $url; - private $fileUrl; - private $fileName; /** * Constructor. @@ -54,13 +50,6 @@ abstract class FileDownloader implements DownloaderInterface */ public function download(PackageInterface $package, $path) { - $this->firstCall = true; - $this->url = $package->getSourceUrl(); - $this->fileUrl = $package->getDistUrl(); - - // init the progress bar - $this->bytesMax = 0; - $url = $package->getDistUrl(); $checksum = $package->getDistSha1Checksum(); @@ -74,7 +63,6 @@ abstract class FileDownloader implements DownloaderInterface } $fileName = rtrim($path.'/'.md5(time().rand()).'.'.pathinfo($url, PATHINFO_EXTENSION), '.'); - $this->fileName = $fileName; $this->io->write(" - Package " . $package->getName() . " (" . $package->getPrettyVersion() . ")"); @@ -87,7 +75,8 @@ abstract class FileDownloader implements DownloaderInterface } } - $this->copy($this->url, $this->fileName, $this->fileUrl); + $rfs = new RemoteFilesystem($this->io); + $rfs->copy($package->getSourceUrl(), $fileName, $url); $this->io->write(''); if (!file_exists($fileName)) { @@ -139,112 +128,6 @@ abstract class FileDownloader implements DownloaderInterface $fs->removeDirectory($path); } - /** - * Get notification action. - * - * @param integer $notificationCode The notification code - * @param integer $severity The severity level - * @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 callbackGet($notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax) - { - switch ($notificationCode) { - case STREAM_NOTIFY_AUTH_REQUIRED: - case STREAM_NOTIFY_FAILURE: - // 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; - - 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) { - $progression = round($bytesTransferred / $this->bytesMax * 100); - } - - if (0 === $progression % 5) { - $this->io->overwrite(" Downloading: $progression%", false); - } - } - break; - - default: - break; - } - } - - 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: connection...", false); - @copy($fileUrl, $fileName, $ctx); - $this->io->overwrite(" Downloading", false); - } - /** * Extract file to directory * diff --git a/src/Composer/Util/RemoteFilesystem.php b/src/Composer/Util/RemoteFilesystem.php new file mode 100644 index 000000000..d24d0a4ea --- /dev/null +++ b/src/Composer/Util/RemoteFilesystem.php @@ -0,0 +1,157 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Util; + +use Composer\IO\IOInterface; + +/** + * @author François Pluchino + */ +class RemoteFilesystem +{ + protected $io; + private $bytesMax; + private $originUrl; + private $fileUrl; + private $fileName; + + /** + * Constructor. + * + * @param IOInterface $io The IO instance + */ + public function __construct(IOInterface $io) + { + $this->io = $io; + } + + /** + * Copy the remote file in local. + * + * @param string $originUrl The origin URL + * @param string $fileName The local filename + * @param string $fileUrl The file URL + * + * @throws \RuntimeException When opensll extension is disabled + */ + public function copy($originUrl, $fileName, $fileUrl) + { + $this->firstCall = true; + $this->originUrl = $originUrl; + $this->fileName = $fileName; + $this->fileUrl = $fileUrl; + $this->bytesMax = 0; + + // 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($originUrl)) { + $auth = $this->io->getAuthorization($originUrl); + $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: connection...", false); + @copy($fileUrl, $fileName, $ctx); + $this->io->overwrite(" Downloading", false); + } + + /** + * Get notification action. + * + * @param integer $notificationCode The notification code + * @param integer $severity The severity level + * @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 callbackGet($notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax) + { + switch ($notificationCode) { + case STREAM_NOTIFY_AUTH_REQUIRED: + case STREAM_NOTIFY_FAILURE: + // for private repository returning 404 error when the authorization is incorrect + $auth = $this->io->getAuthorization($this->originUrl); + $ps = $this->firstCall && 404 === $messageCode + && 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->originUrl, $username, $password); + + $this->copy($this->originUrl, $this->fileName, $this->fileUrl); + } + 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) { + $progression = round($bytesTransferred / $this->bytesMax * 100); + } + + if (0 === $progression % 5) { + $this->io->overwrite(" Downloading: $progression%", false); + } + } + break; + + default: + break; + } + } +} \ No newline at end of file From 953f0992f71842b7453a1adf6ee86a4b35e7e0c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Pluchino?= Date: Wed, 15 Feb 2012 13:11:29 +0100 Subject: [PATCH 3/6] Merge callback method of FileDownloader and VcsDriver Required corrections added --- src/Composer/Downloader/FileDownloader.php | 4 +- src/Composer/Repository/Vcs/VcsDriver.php | 87 +---------------- src/Composer/Util/RemoteFilesystem.php | 108 +++++++++++++++++---- 3 files changed, 92 insertions(+), 107 deletions(-) diff --git a/src/Composer/Downloader/FileDownloader.php b/src/Composer/Downloader/FileDownloader.php index dad74fbc4..ef9ae8e6e 100644 --- a/src/Composer/Downloader/FileDownloader.php +++ b/src/Composer/Downloader/FileDownloader.php @@ -11,10 +11,10 @@ namespace Composer\Downloader; -use Composer\Util\RemoteFilesystem; use Composer\IO\IOInterface; use Composer\Package\PackageInterface; use Composer\Util\Filesystem; +use Composer\Util\RemoteFilesystem; /** * Base downloader for file packages @@ -76,7 +76,7 @@ abstract class FileDownloader implements DownloaderInterface } $rfs = new RemoteFilesystem($this->io); - $rfs->copy($package->getSourceUrl(), $fileName, $url); + $rfs->copy($package->getSourceUrl(), $url, $fileName); $this->io->write(''); if (!file_exists($fileName)) { diff --git a/src/Composer/Repository/Vcs/VcsDriver.php b/src/Composer/Repository/Vcs/VcsDriver.php index 5a25fc1be..7008f41be 100644 --- a/src/Composer/Repository/Vcs/VcsDriver.php +++ b/src/Composer/Repository/Vcs/VcsDriver.php @@ -14,6 +14,7 @@ namespace Composer\Repository\Vcs; use Composer\IO\IOInterface; use Composer\Util\ProcessExecutor; +use Composer\Util\RemoteFilesystem; /** * A driver implementation for driver with authorization interaction. @@ -25,9 +26,6 @@ abstract class VcsDriver protected $url; protected $io; protected $process; - private $firstCall; - private $contentUrl; - private $content; /** * Constructor. @@ -41,7 +39,6 @@ abstract class VcsDriver $this->url = $url; $this->io = $io; $this->process = $process ?: new ProcessExecutor; - $this->firstCall = true; } /** @@ -68,85 +65,7 @@ abstract class VcsDriver */ protected function getContents($url) { - $this->contentUrl = $url; - $auth = $this->io->getAuthorization($this->url); - $params = array(); - - // add authorization to curl options - if ($this->io->hasAuthorization($this->url)) { - $authStr = base64_encode($auth['username'] . ':' . $auth['password']); - $params['http'] = array('header' => "Authorization: Basic $authStr\r\n"); - } else if (null !== $this->io->getLastUsername()) { - $authStr = base64_encode($this->io->getLastUsername() . ':' . $this->io->getLastPassword()); - $params['http'] = array('header' => "Authorization: Basic $authStr\r\n"); - $this->io->setAuthorization($this->url, $this->io->getLastUsername(), $this->io->getLastPassword()); - } - - $ctx = stream_context_create($params); - stream_context_set_params($ctx, array("notification" => array($this, 'callbackGet'))); - - $content = @file_get_contents($url, false, $ctx); - - // content get after authorization - if (false === $content) { - $content = $this->content; - $this->content = null; - $this->contentUrl = null; - } - - return $content; - } - - /** - * Get notification action. - * - * @param integer $notificationCode The notification code - * @param integer $severity The severity level - * @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 callbackGet($notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax) - { - switch ($notificationCode) { - case STREAM_NOTIFY_AUTH_REQUIRED: - case STREAM_NOTIFY_FAILURE: - // 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->contentUrl . "' URL not found"); - } - - $this->firstCall = false; - - // get authorization informations - if (401 === $messageCode || $ps) { - if (!$this->io->isInteractive()) { - $mess = "The '" . $this->contentUrl . "' URL not found"; - - if (401 === $code || $ps) { - $mess = "The '" . $this->contentUrl . "' URL required the authorization.\nYou must be used the interactive console"; - } - - throw new \RuntimeException($mess); - } - - $this->io->write(array('', "Authorization for " . $this->contentUrl . ":")); - $username = $this->io->ask(' Username: '); - $password = $this->io->askAndHideAnswer(' Password: '); - $this->io->setAuthorization($this->url, $username, $password); - - $this->content = $this->getContents($this->contentUrl); - } - break; - - default: - break; - } + $rfs = new RemoteFilesystem($this->io); + return $rfs->getContents($this->url, $url, false); } } diff --git a/src/Composer/Util/RemoteFilesystem.php b/src/Composer/Util/RemoteFilesystem.php index d24d0a4ea..8fdc47b23 100644 --- a/src/Composer/Util/RemoteFilesystem.php +++ b/src/Composer/Util/RemoteFilesystem.php @@ -19,11 +19,14 @@ use Composer\IO\IOInterface; */ class RemoteFilesystem { - protected $io; + private $io; + private $firstCall; private $bytesMax; private $originUrl; private $fileUrl; private $fileName; + private $content; + private $progess; /** * Constructor. @@ -38,20 +41,52 @@ class RemoteFilesystem /** * Copy the remote file in local. * - * @param string $originUrl The origin URL - * @param string $fileName The local filename - * @param string $fileUrl The file URL - * - * @throws \RuntimeException When opensll extension is disabled + * @param string $originUrl The orgin URL + * @param string $fileUrl The file URL + * @param string $fileName the local filename + * @param boolean $progess Display the progression */ - public function copy($originUrl, $fileName, $fileUrl) + public function copy($originUrl, $fileUrl, $fileName, $progess = true) + { + $this->get($originUrl, $fileUrl, $fileName, $progess); + } + + /** + * Get the content. + * + * @param string $originUrl The orgin URL + * @param string $fileUrl The file URL + * @param boolean $progess Display the progression + * + * @return false|string The content + */ + public function getContents($originUrl, $fileUrl, $progess = true) + { + $this->get($originUrl, $fileUrl, null, $progess); + + return $this->content; + } + + /** + * Get file content or copy action. + * + * @param string $originUrl The orgin URL + * @param string $fileUrl The file URL + * @param string $fileName the local filename + * @param boolean $progess Display the progression + * + * @throws \RuntimeException When the openssl extension is disabled + */ + protected function get($originUrl, $fileUrl, $fileName = null, $progess = true) { $this->firstCall = true; - $this->originUrl = $originUrl; - $this->fileName = $fileName; - $this->fileUrl = $fileUrl; $this->bytesMax = 0; - + $this->content = null; + $this->originUrl = $originUrl; + $this->fileUrl = $fileUrl; + $this->fileName = $fileName; + $this->progress = $progess; + // Handle system proxy $params = array('http' => array()); @@ -69,18 +104,35 @@ class RemoteFilesystem ); } + // add authorization in context if ($this->io->hasAuthorization($originUrl)) { $auth = $this->io->getAuthorization($originUrl); $authStr = base64_encode($auth['username'] . ':' . $auth['password']); $params['http'] = array_merge($params['http'], array('header' => "Authorization: Basic $authStr\r\n")); + + } else if (null !== $this->io->getLastUsername()) { + $authStr = base64_encode($this->io->getLastUsername() . ':' . $this->io->getLastPassword()); + $params['http'] = array('header' => "Authorization: Basic $authStr\r\n"); + $this->io->setAuthorization($originUrl, $this->io->getLastUsername(), $this->io->getLastPassword()); } $ctx = stream_context_create($params); stream_context_set_params($ctx, array("notification" => array($this, 'callbackGet'))); - $this->io->overwrite(" Downloading: connection...", false); - @copy($fileUrl, $fileName, $ctx); - $this->io->overwrite(" Downloading", false); + if ($this->progress) { + $this->io->overwrite(" Downloading: connection...", false); + } + + if (null !== $fileName) { + @copy($fileUrl, $fileName, $ctx); + + } else { + $this->content = @file_get_contents($fileUrl, false, $ctx); + } + + if ($this->progress) { + $this->io->overwrite(" Downloading", false); + } } /** @@ -100,8 +152,7 @@ class RemoteFilesystem case STREAM_NOTIFY_FAILURE: // for private repository returning 404 error when the authorization is incorrect $auth = $this->io->getAuthorization($this->originUrl); - $ps = $this->firstCall && 404 === $messageCode - && null === $auth['username']; + $ps = $this->firstCall && 404 === $messageCode && null === $auth['username']; if (404 === $messageCode && !$this->firstCall) { throw new \RuntimeException("The '" . $this->fileUrl . "' URL not found"); @@ -112,21 +163,21 @@ class RemoteFilesystem // get authorization informations if (401 === $messageCode || $ps) { if (!$this->io->isInteractive()) { - $mess = "The '" . $this->fileUrl . "' URL not found"; + $mess = "The '" . $this->fileUrl . "' URL was not found"; if (401 === $code || $ps) { - $mess = "The '" . $this->fileUrl . "' URL required the authorization.\nYou must be used the interactive console"; + $mess = "The '" . $this->fileUrl . "' URL required the authorization.\nYou must be using the interactive console"; } throw new \RuntimeException($mess); } - $this->io->overwrite(' Authorization required:'); + $this->io->overwrite(' Authorization required (' .$this->getHostname($this->fileUrl).'):'); $username = $this->io->ask(' Username: '); $password = $this->io->askAndHideAnswer(' Password: '); $this->io->setAuthorization($this->originUrl, $username, $password); - $this->copy($this->originUrl, $this->fileName, $this->fileUrl); + $this->content = $this->get($this->originUrl, $this->fileUrl, $this->fileName, $this->progess); } break; @@ -137,7 +188,7 @@ class RemoteFilesystem break; case STREAM_NOTIFY_PROGRESS: - if ($this->bytesMax > 0) { + if ($this->bytesMax > 0 && $this->progress) { $progression = 0; if ($this->bytesMax > 0) { @@ -154,4 +205,19 @@ class RemoteFilesystem break; } } + + /** + * Get the hostname. + * + * @param string $url The file URL + * + * @return string The hostname + */ + protected function getHostname($url) + { + $host = substr($url, strpos($url, '://') + 3); + $host = substr($host, 0, strpos($host, '/')); + + return $host; + } } \ No newline at end of file From bc232af018e1dc688de8bc106ea8d9a09621ac33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Pluchino?= Date: Wed, 15 Feb 2012 14:16:42 +0100 Subject: [PATCH 4/6] Bug fix display downloading info --- src/Composer/Util/RemoteFilesystem.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Composer/Util/RemoteFilesystem.php b/src/Composer/Util/RemoteFilesystem.php index 8fdc47b23..54aca9356 100644 --- a/src/Composer/Util/RemoteFilesystem.php +++ b/src/Composer/Util/RemoteFilesystem.php @@ -177,7 +177,7 @@ class RemoteFilesystem $password = $this->io->askAndHideAnswer(' Password: '); $this->io->setAuthorization($this->originUrl, $username, $password); - $this->content = $this->get($this->originUrl, $this->fileUrl, $this->fileName, $this->progess); + $this->content = $this->get($this->originUrl, $this->fileUrl, $this->fileName, $this->progress); } break; From dbfbbab904bc549bc876e71f8c5e7b3b0e0ade6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Pluchino?= Date: Thu, 16 Feb 2012 23:41:26 +0100 Subject: [PATCH 5/6] Add changes requested --- src/Composer/Util/RemoteFilesystem.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Composer/Util/RemoteFilesystem.php b/src/Composer/Util/RemoteFilesystem.php index 036c0b2fa..2d4be57d6 100644 --- a/src/Composer/Util/RemoteFilesystem.php +++ b/src/Composer/Util/RemoteFilesystem.php @@ -58,7 +58,7 @@ class RemoteFilesystem * @param string $fileUrl The file URL * @param boolean $progess Display the progression * - * @return false|string The content + * @return string The content */ public function getContents($originUrl, $fileUrl, $progess = true) { @@ -75,7 +75,7 @@ class RemoteFilesystem * @param string $fileName the local filename * @param boolean $progess Display the progression * - * @throws \RuntimeException When the openssl extension is disabled + * @throws \RuntimeException When the file could not be downloaded */ protected function get($originUrl, $fileUrl, $fileName = null, $progess = true) { @@ -92,8 +92,7 @@ class RemoteFilesystem if ($this->io->hasAuthorization($originUrl)) { $auth = $this->io->getAuthorization($originUrl); $authStr = base64_encode($auth['username'] . ':' . $auth['password']); - $options['http']['header'] = "Authorization: Basic $authStr\r\n"; - + $options['http']['header'] = "Authorization: Basic $authStr\r\n"; } else if (null !== $this->io->getLastUsername()) { $authStr = base64_encode($this->io->getLastUsername() . ':' . $this->io->getLastPassword()); $options['http'] = array('header' => "Authorization: Basic $authStr\r\n"); @@ -107,15 +106,19 @@ class RemoteFilesystem } if (null !== $fileName) { - @copy($fileUrl, $fileName, $ctx); - + $result = @copy($fileUrl, $fileName, $ctx); } else { - $this->content = @file_get_contents($fileUrl, false, $ctx); + $result = @file_get_contents($fileUrl, false, $ctx); + $this->content = $result; } if ($this->progress) { $this->io->overwrite(" Downloading", false); } + + if (false === $result) { + throw new \RuntimeException("the '$fileUrl' file could not be downloaded"); + } } /** From 89b0c8b2e77818a6ceb8380c641745affd333989 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=E7ois=20Pluchino?= Date: Fri, 17 Feb 2012 10:28:33 +0100 Subject: [PATCH 6/6] Restore the original file mode --- bin/compile | 0 bin/composer | 0 2 files changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 bin/compile mode change 100644 => 100755 bin/composer diff --git a/bin/compile b/bin/compile old mode 100644 new mode 100755 diff --git a/bin/composer b/bin/composer old mode 100644 new mode 100755