Browse Source

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

Pádraic Brady 11 years ago
parent
commit
9881d76216

+ 1 - 1
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; }
             )
         );

+ 1 - 29
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('<warning>You are running Composer with SSL/TLS protection disabled.</warning>');
             $baseUrl = 'http://' . self::HOMEPAGE;
-            $disableTls = true;
-        } elseif (!extension_loaded('openssl')) {
-            $output->writeln('<error>The openssl extension is required for SSL/TLS protection but is not available.</error>');
-            $output->writeln('<error>You can disable this error, at your own risk, by enabling the \'disable-tls\' option.</error>');
-            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('<error>' . $e->getMessage() . '</error>');
-                $output->writeln('<error>Unable to locate a valid CA certificate file. You must set a valid \'cafile\' option.</error>');
-                $output->writeln('<error>You can alternatively disable this error, at your own risk, by enabling the \'disable-tls\' option.</error>');
-                return 1;
-            } else {
-                throw $e;
-            }
-        }
+        $remoteFilesystem = Factory::createRemoteFilesystem($this->getIO(), $config);
 
         $cacheDir = $config->get('cache-dir');
         $rollbackDir = $config->get('home');

+ 2 - 1
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])) {

+ 33 - 0
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('<warning>You are running Composer with SSL/TLS protection disabled.</warning>');
+            $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('<error>Unable to locate a valid CA certificate file. You must set a valid \'cafile\' option.</error>');
+                $io->write('<error>A valid CA certificate file is required for SSL/TLS protection.</error>');
+                $io->write('<error>You can disable this error, at your own risk, by passing the \'--disable-tls\' option to this command.</error>');
+            }
+            throw $e;
+        }
+        return $remoteFilesystem;
+    }
 }

+ 16 - 0
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);
+    }
 }

+ 6 - 1
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.
      *