From 054faef5eb60fba87d21b18d586be89a787b5a17 Mon Sep 17 00:00:00 2001 From: Jordan Alliot Date: Wed, 18 Jan 2012 19:15:13 +0100 Subject: [PATCH] New context at each call and possibility to add more options and params to the context --- src/Composer/Downloader/FileDownloader.php | 7 +++---- src/Composer/Json/JsonFile.php | 6 ++++-- src/Composer/Util/StreamContextFactory.php | 19 ++++++++----------- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/Composer/Downloader/FileDownloader.php b/src/Composer/Downloader/FileDownloader.php index 10e63806b..189e76f59 100644 --- a/src/Composer/Downloader/FileDownloader.php +++ b/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: connection...", false); @copy($url, $fileName, $ctx); diff --git a/src/Composer/Json/JsonFile.php b/src/Composer/Json/JsonFile.php index 00baf5f40..0c585216d 100644 --- a/src/Composer/Json/JsonFile.php +++ b/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) { diff --git a/src/Composer/Util/StreamContextFactory.php b/src/Composer/Util/StreamContextFactory.php index d32236a83..8b452482d 100644 --- a/src/Composer/Util/StreamContextFactory.php +++ b/src/Composer/Util/StreamContextFactory.php @@ -1,4 +1,5 @@ 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); } } \ No newline at end of file