فهرست منبع

New context at each call and possibility to add more options and params to the context

Jordan Alliot 13 سال پیش
والد
کامیت
054faef5eb
3فایلهای تغییر یافته به همراه15 افزوده شده و 17 حذف شده
  1. 3 4
      src/Composer/Downloader/FileDownloader.php
  2. 4 2
      src/Composer/Json/JsonFile.php
  3. 8 11
      src/Composer/Util/StreamContextFactory.php

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

@@ -78,15 +78,14 @@ abstract class FileDownloader implements DownloaderInterface
             }
         }
 
-        $ctx = StreamContextFactory::getContext();
-
+        $options = array();
         if ($this->io->hasAuthorization($package->getSourceUrl())) {
             $auth = $this->io->getAuthorization($package->getSourceUrl());
             $authStr = base64_encode($auth['username'] . ':' . $auth['password']);
-            stream_context_set_option($ctx, 'http', 'header', "Authorization: Basic $authStr\r\n");
+            $options['http']['header'] = "Authorization: Basic $authStr\r\n";
         }
 
-        stream_context_set_params($ctx, array("notification" => array($this, 'callbackGet')));
+        $ctx = StreamContextFactory::getContext($options, array('notification' => array($this, 'callbackGet')));
 
         $this->io->overwrite("    Downloading: <comment>connection...</comment>", false);
         @copy($url, $fileName, $ctx);

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

@@ -60,8 +60,10 @@ class JsonFile
      */
     public function read()
     {
-        $ctx = StreamContextFactory::getContext();
-        stream_context_set_option($ctx, 'http', 'header', 'User-Agent: Composer/'.Composer::VERSION."\r\n");
+        $ctx = StreamContextFactory::getContext(array(
+            'http' => array(
+                'header' => 'User-Agent: Composer/'.Composer::VERSION."\r\n"
+        )));
 
         $json = file_get_contents($this->path, false, $ctx);
         if (!$json) {

+ 8 - 11
src/Composer/Util/StreamContextFactory.php

@@ -1,4 +1,5 @@
 <?php
+
 /*
  * This file is part of Composer.
  *
@@ -18,23 +19,19 @@ namespace Composer\Util;
  */
 final class StreamContextFactory
 {
-    private static $context;
-
     /**
      * Creates a context supporting HTTP proxies
      *
+     * @param array $options Options to merge with the default
+     * @param array $params  Parameters to specify on the context
      * @return resource Default context
      * @throws \RuntimeException if https proxy required and OpenSSL uninstalled
      */
-    public static function getContext()
+    static public function getContext(array $options = array(), array $params = array())
     {
-        if (null !== self::$context) {
-            return self::$context;
-        }
-        
-        // Handle system proxy
-        $params = array('http' => array());
+        $options = array_merge(array('http' => array()), $options);
 
+        // Handle system proxy
         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'];
@@ -46,12 +43,12 @@ final class StreamContextFactory
                 throw new \RuntimeException('You must enable the openssl extension to use a proxy over https');
             }
             
-            $params['http'] = array(
+            $options['http'] = array(
                 'proxy'           => $proxy,
                 'request_fulluri' => true,
             );
         }
         
-        return self::$context = stream_context_create($params);
+        return stream_context_create($options, $params);
     }
 }