1
0
Fork 0

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

pull/224/head
Jordan Alliot 2012-01-18 19:15:13 +01:00
parent 9c27e38654
commit 054faef5eb
3 changed files with 15 additions and 17 deletions

View File

@ -78,15 +78,14 @@ abstract class FileDownloader implements DownloaderInterface
} }
} }
$ctx = StreamContextFactory::getContext(); $options = array();
if ($this->io->hasAuthorization($package->getSourceUrl())) { if ($this->io->hasAuthorization($package->getSourceUrl())) {
$auth = $this->io->getAuthorization($package->getSourceUrl()); $auth = $this->io->getAuthorization($package->getSourceUrl());
$authStr = base64_encode($auth['username'] . ':' . $auth['password']); $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); $this->io->overwrite(" Downloading: <comment>connection...</comment>", false);
@copy($url, $fileName, $ctx); @copy($url, $fileName, $ctx);

View File

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

View File

@ -1,4 +1,5 @@
<?php <?php
/* /*
* This file is part of Composer. * This file is part of Composer.
* *
@ -18,23 +19,19 @@ namespace Composer\Util;
*/ */
final class StreamContextFactory final class StreamContextFactory
{ {
private static $context;
/** /**
* Creates a context supporting HTTP proxies * 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 * @return resource Default context
* @throws \RuntimeException if https proxy required and OpenSSL uninstalled * @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) { $options = array_merge(array('http' => array()), $options);
return self::$context;
}
// Handle system proxy // Handle system proxy
$params = array('http' => array());
if (isset($_SERVER['HTTP_PROXY']) || isset($_SERVER['http_proxy'])) { if (isset($_SERVER['HTTP_PROXY']) || isset($_SERVER['http_proxy'])) {
// Some systems seem to rely on a lowercased version instead... // Some systems seem to rely on a lowercased version instead...
$proxy = isset($_SERVER['HTTP_PROXY']) ? $_SERVER['HTTP_PROXY'] : $_SERVER['http_proxy']; $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'); throw new \RuntimeException('You must enable the openssl extension to use a proxy over https');
} }
$params['http'] = array( $options['http'] = array(
'proxy' => $proxy, 'proxy' => $proxy,
'request_fulluri' => true, 'request_fulluri' => true,
); );
} }
return self::$context = stream_context_create($params); return stream_context_create($options, $params);
} }
} }