From 9881d76216134404adbd8625ddf283bae0f15cfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A1draic=20Brady?= Date: Sat, 1 Mar 2014 14:32:07 +0000 Subject: [PATCH] Adds Composer\Factory::createRemoteFilesystem(): - Implemented in self-update command - Added to Composer\IO\BaseIO the getInputOption() and getInputArgument() getters to allow access to input - Fixed some minor bugs --- src/Composer/Command/ConfigCommand.php | 2 +- src/Composer/Command/SelfUpdateCommand.php | 30 +------------------- src/Composer/Config.php | 3 +- src/Composer/Factory.php | 33 ++++++++++++++++++++++ src/Composer/IO/BaseIO.php | 16 +++++++++++ src/Composer/Util/RemoteFilesystem.php | 7 ++++- 6 files changed, 59 insertions(+), 32 deletions(-) diff --git a/src/Composer/Command/ConfigCommand.php b/src/Composer/Command/ConfigCommand.php index c6e56e97f..63adbf2b3 100644 --- a/src/Composer/Command/ConfigCommand.php +++ b/src/Composer/Command/ConfigCommand.php @@ -287,7 +287,7 @@ EOT 'prepend-autoloader' => array($booleanValidator, $booleanNormalizer), 'disable-tls' => array($booleanValidator, $booleanNormalizer), 'cafile' => array( - function ($val) { return file_exists($val) && is_readable($val); } + function ($val) { return file_exists($val) && is_readable($val); }, function ($val) { return $val === 'null' ? null : $val; } ) ); diff --git a/src/Composer/Command/SelfUpdateCommand.php b/src/Composer/Command/SelfUpdateCommand.php index 75095b3ae..a76c567af 100644 --- a/src/Composer/Command/SelfUpdateCommand.php +++ b/src/Composer/Command/SelfUpdateCommand.php @@ -61,40 +61,12 @@ EOT { $config = Factory::createConfig(); - $disableTls = false; if($config->get('disable-tls') === true || $input->getOption('disable-tls')) { - $output->writeln('You are running Composer with SSL/TLS protection disabled.'); $baseUrl = 'http://' . self::HOMEPAGE; - $disableTls = true; - } elseif (!extension_loaded('openssl')) { - $output->writeln('The openssl extension is required for SSL/TLS protection but is not available.'); - $output->writeln('You can disable this error, at your own risk, by enabling the \'disable-tls\' option.'); - return 1; } else { $baseUrl = 'https://' . self::HOMEPAGE; } - - $remoteFilesystemOptions = array(); - if ($disableTls === false) { - if (!is_null($config->get('cafile'))) { - $remoteFilesystemOptions = array('ssl'=>array('cafile'=>$config->get('cafile'))); - } - if (!is_null($input->getOption('cafile'))) { - $remoteFilesystemOptions = array('ssl'=>array('cafile'=>$input->getOption('cafile'))); - } - } - try { - $remoteFilesystem = new RemoteFilesystem($this->getIO(), $remoteFilesystemOptions, $disableTls); - } catch (TransportException $e) { - if (preg_match('|cafile|', $e->getMessage())) { - $output->writeln('' . $e->getMessage() . ''); - $output->writeln('Unable to locate a valid CA certificate file. You must set a valid \'cafile\' option.'); - $output->writeln('You can alternatively disable this error, at your own risk, by enabling the \'disable-tls\' option.'); - return 1; - } else { - throw $e; - } - } + $remoteFilesystem = Factory::createRemoteFilesystem($this->getIO(), $config); $cacheDir = $config->get('cache-dir'); $rollbackDir = $config->get('home'); diff --git a/src/Composer/Config.php b/src/Composer/Config.php index 722bc5370..e5dc04084 100644 --- a/src/Composer/Config.php +++ b/src/Composer/Config.php @@ -142,6 +142,7 @@ class Config case 'cache-files-dir': case 'cache-repo-dir': case 'cache-vcs-dir': + case 'cafile': // convert foo-bar to COMPOSER_FOO_BAR and check if it exists since it overrides the local config $env = 'COMPOSER_' . strtoupper(strtr($key, '-', '_')); @@ -214,7 +215,7 @@ class Config return $this->config[$key]; case 'disable-tls': - return $this->config[$key]!== 'false' && (bool) $this->config[$key]; + return $this->config[$key] !== 'false' && (bool) $this->config[$key]; default: if (!isset($this->config[$key])) { diff --git a/src/Composer/Factory.php b/src/Composer/Factory.php index 91cb0b420..272e8365d 100644 --- a/src/Composer/Factory.php +++ b/src/Composer/Factory.php @@ -467,4 +467,37 @@ class Factory return $factory->createComposer($io, $config, $disablePlugins, $input); } + + public static function createRemoteFilesystem(IOInterface $io, Config $config, $options = array()) + { + $disableTls = false; + if($config->get('disable-tls') === true || $io->getInputOption('disable-tls')) { + $io->write('You are running Composer with SSL/TLS protection disabled.'); + $disableTls = true; + } elseif (!extension_loaded('openssl')) { + throw new \RuntimeException('The openssl extension is required for SSL/TLS protection but is not available. ' + . 'You can disable this error, at your own risk, by passing the \'--disable-tls\' option to this command.'); + } + $remoteFilesystemOptions = array(); + if ($disableTls === false) { + if (!empty($config->get('cafile'))) { + $remoteFilesystemOptions = array('ssl'=>array('cafile'=>$config->get('cafile'))); + } + if (!empty($io->getInputOption('cafile'))) { + $remoteFilesystemOptions = array('ssl'=>array('cafile'=>$io->getInputOption('cafile'))); + } + $remoteFilesystemOptions = array_merge_recursive($remoteFilesystemOptions, $options); + } + try { + $remoteFilesystem = new RemoteFilesystem($io, $remoteFilesystemOptions, $disableTls); + } catch (TransportException $e) { + if (preg_match('|cafile|', $e->getMessage())) { + $io->write('Unable to locate a valid CA certificate file. You must set a valid \'cafile\' option.'); + $io->write('A valid CA certificate file is required for SSL/TLS protection.'); + $io->write('You can disable this error, at your own risk, by passing the \'--disable-tls\' option to this command.'); + } + throw $e; + } + return $remoteFilesystem; + } } diff --git a/src/Composer/IO/BaseIO.php b/src/Composer/IO/BaseIO.php index 29cae4f07..688c24262 100644 --- a/src/Composer/IO/BaseIO.php +++ b/src/Composer/IO/BaseIO.php @@ -69,4 +69,20 @@ abstract class BaseIO implements IOInterface } } } + + /** + * {@inheritDoc} + */ + public function getInputOption($optionName) + { + return $this->input->getOption($optionName); + } + + /** + * {@inheritDoc} + */ + public function getInputArgument($ArgumentName) + { + return $this->input->getArgument($argumentName); + } } diff --git a/src/Composer/Util/RemoteFilesystem.php b/src/Composer/Util/RemoteFilesystem.php index 1bbdf2f83..9b1f429e7 100644 --- a/src/Composer/Util/RemoteFilesystem.php +++ b/src/Composer/Util/RemoteFilesystem.php @@ -32,7 +32,7 @@ class RemoteFilesystem private $retry; private $progress; private $lastProgress; - private $options; + private $options = array(); private $disableTls = false; private $retryTls = true; private $retryAuthFailure; @@ -107,6 +107,11 @@ class RemoteFilesystem return $this->options; } + public function isTlsDisabled() + { + return $this->disableTls === true; + } + /** * Get file content or copy action. *