Pārlūkot izejas kodu

Moved proxy handling to a new class so that it can be reused in other parts of Composer

Jordan Alliot 13 gadi atpakaļ
vecāks
revīzija
9c27e38654

+ 5 - 2
src/Composer/Command/SelfUpdateCommand.php

@@ -13,6 +13,7 @@
 namespace Composer\Command;
 
 use Composer\Composer;
+use Composer\Util\StreamContextFactory;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Output\OutputInterface;
 
@@ -39,7 +40,9 @@ EOT
 
     protected function execute(InputInterface $input, OutputInterface $output)
     {
-        $latest = trim(file_get_contents('http://getcomposer.org/version'));
+        $ctx = StreamContextFactory::getContext();
+
+        $latest = trim(file_get_contents('http://getcomposer.org/version'), false, $ctx);
 
         if (Composer::VERSION !== $latest) {
             $output->writeln(sprintf("Updating to version <info>%s</info>.", $latest));
@@ -47,7 +50,7 @@ EOT
             $remoteFilename = 'http://getcomposer.org/composer.phar';
             $localFilename = $_SERVER['argv'][0];
 
-            file_put_contents($localFilename, file_get_contents($remoteFilename));
+            copy($remoteFilename, $localFilename, $ctx);
         } else {
             $output->writeln("<info>You are using the latest composer version.</info>");
         }

+ 3 - 18
src/Composer/Downloader/FileDownloader.php

@@ -13,6 +13,7 @@ namespace Composer\Downloader;
 
 use Composer\IO\IOInterface;
 use Composer\Package\PackageInterface;
+use Composer\Util\StreamContextFactory;
 
 /**
  * Base downloader for file packages
@@ -77,30 +78,14 @@ 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,
-            );
-        }
+        $ctx = StreamContextFactory::getContext();
 
         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"));
+            stream_context_set_option($ctx, 'http', '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);

+ 4 - 4
src/Composer/Json/JsonFile.php

@@ -14,6 +14,7 @@ namespace Composer\Json;
 
 use Composer\Repository\RepositoryManager;
 use Composer\Composer;
+use Composer\Util\StreamContextFactory;
 
 /**
  * Reads/writes json files.
@@ -59,11 +60,10 @@ class JsonFile
      */
     public function read()
     {
-        $context = stream_context_create(array(
-            'http' => array('header' => 'User-Agent: Composer/'.Composer::VERSION."\r\n")
-        ));
+        $ctx = StreamContextFactory::getContext();
+        stream_context_set_option($ctx, 'http', 'header', 'User-Agent: Composer/'.Composer::VERSION."\r\n");
 
-        $json = file_get_contents($this->path, false, $context);
+        $json = file_get_contents($this->path, false, $ctx);
         if (!$json) {
             throw new \RuntimeException('Could not read '.$this->path.', you are probably offline');
         }

+ 5 - 2
src/Composer/Repository/PearRepository.php

@@ -13,6 +13,7 @@
 namespace Composer\Repository;
 
 use Composer\Package\Loader\ArrayLoader;
+use Composer\Util\StreamContextFactory;
 
 /**
  * @author Benjamin Eberlei <kontakt@beberlei.de>
@@ -87,7 +88,8 @@ class PearRepository extends ArrayRepository
                     );
 
                     try {
-                        $deps = file_get_contents($releaseLink . "/deps.".$pearVersion.".txt");
+                        $ctx = StreamContextFactory::getContext();
+                        $deps = file_get_contents($releaseLink.'/deps.'.$pearVersion.'.txt', false, $ctx);
                     } catch (\ErrorException $e) {
                         if (strpos($e->getMessage(), '404')) {
                             continue;
@@ -132,7 +134,8 @@ class PearRepository extends ArrayRepository
      */
     private function requestXml($url)
     {
-        $content = file_get_contents($url);
+        $ctx = StreamContextFactory::getContext();
+        $content = file_get_contents($url, false, $ctx);
         if (!$content) {
             throw new \UnexpectedValueException('The PEAR channel at '.$url.' did not respond.');
         }

+ 57 - 0
src/Composer/Util/StreamContextFactory.php

@@ -0,0 +1,57 @@
+<?php
+/*
+ * This file is part of Composer.
+ *
+ * (c) Nils Adermann <naderman@naderman.de>
+ *     Jordi Boggiano <j.boggiano@seld.be>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Composer\Util;
+
+/**
+ * Allows the creation of a basic context supporting http proxy
+ *
+ * @author Jordan Alliot <jordan.alliot@gmail.com>
+ */
+final class StreamContextFactory
+{
+    private static $context;
+
+    /**
+     * Creates a context supporting HTTP proxies
+     *
+     * @return resource Default context
+     * @throws \RuntimeException if https proxy required and OpenSSL uninstalled
+     */
+    public static function getContext()
+    {
+        if (null !== self::$context) {
+            return self::$context;
+        }
+        
+        // Handle system proxy
+        $params = array('http' => array());
+
+        if (isset($_SERVER['HTTP_PROXY']) || isset($_SERVER['http_proxy'])) {
+            // Some systems seem to rely on a lowercased version instead...
+            $proxy = isset($_SERVER['HTTP_PROXY']) ? $_SERVER['HTTP_PROXY'] : $_SERVER['http_proxy'];
+            
+            // http(s):// is not supported in proxy
+            $proxy = str_replace(array('http://', 'https://'), array('tcp://', 'ssl://'), $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,
+            );
+        }
+        
+        return self::$context = stream_context_create($params);
+    }
+}