Use RemoteFilesystem everywhere we do http requests

main
Jordi Boggiano 13 years ago
parent d291d65faf
commit 7f65dd7409

@ -23,6 +23,7 @@ use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Composer\Json\JsonFile; use Composer\Json\JsonFile;
use Composer\Util\RemoteFilesystem;
/** /**
* Install a package as new project into new directory. * Install a package as new project into new directory.
@ -86,7 +87,7 @@ EOT
if (null === $repositoryUrl) { if (null === $repositoryUrl) {
$sourceRepo = new ComposerRepository(array('url' => 'http://packagist.org')); $sourceRepo = new ComposerRepository(array('url' => 'http://packagist.org'));
} elseif (".json" === substr($repositoryUrl, -5)) { } elseif (".json" === substr($repositoryUrl, -5)) {
$sourceRepo = new FilesystemRepository(new JsonFile($repositoryUrl)); $sourceRepo = new FilesystemRepository(new JsonFile($repositoryUrl, new RemoteFilesystem($io)));
} elseif (0 === strpos($repositoryUrl, 'http')) { } elseif (0 === strpos($repositoryUrl, 'http')) {
$sourceRepo = new ComposerRepository(array('url' => $repositoryUrl)); $sourceRepo = new ComposerRepository(array('url' => $repositoryUrl));
} else { } else {

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

@ -17,6 +17,7 @@ use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Composer\Json\JsonFile; use Composer\Json\JsonFile;
use Composer\Json\JsonValidationException; use Composer\Json\JsonValidationException;
use Composer\Util\RemoteFilesystem;
/** /**
* @author Robert Schönthal <seroscho@googlemail.com> * @author Robert Schönthal <seroscho@googlemail.com>
@ -55,7 +56,7 @@ EOT
$laxValid = false; $laxValid = false;
try { try {
$json = new JsonFile($file); $json = new JsonFile($file, new RemoteFilesystem($this->getIO()));
$json->read(); $json->read();
$json->validateSchema(JsonFile::LAX_SCHEMA); $json->validateSchema(JsonFile::LAX_SCHEMA);

@ -16,6 +16,7 @@ use Composer\Json\JsonFile;
use Composer\IO\IOInterface; use Composer\IO\IOInterface;
use Composer\Repository\RepositoryManager; use Composer\Repository\RepositoryManager;
use Composer\Util\ProcessExecutor; use Composer\Util\ProcessExecutor;
use Composer\Util\RemoteFilesystem;
/** /**
* Creates an configured instance of composer. * Creates an configured instance of composer.
@ -38,7 +39,7 @@ class Factory
$composerFile = getenv('COMPOSER') ?: 'composer.json'; $composerFile = getenv('COMPOSER') ?: 'composer.json';
} }
$file = new JsonFile($composerFile); $file = new JsonFile($composerFile, new RemoteFilesystem($io));
if (!$file->exists()) { if (!$file->exists()) {
if ($composerFile === 'composer.json') { if ($composerFile === 'composer.json') {
$message = 'Composer could not find a composer.json file in '.getcwd(); $message = 'Composer could not find a composer.json file in '.getcwd();
@ -98,7 +99,7 @@ class Factory
// init locker // init locker
$lockFile = substr($composerFile, -5) === '.json' ? substr($composerFile, 0, -4).'lock' : $composerFile . '.lock'; $lockFile = substr($composerFile, -5) === '.json' ? substr($composerFile, 0, -4).'lock' : $composerFile . '.lock';
$locker = new Package\Locker(new JsonFile($lockFile), $rm, md5_file($composerFile)); $locker = new Package\Locker(new JsonFile($lockFile, new RemoteFilesystem($io)), $rm, md5_file($composerFile));
// initialize composer // initialize composer
$composer = new Composer(); $composer = new Composer();

@ -17,6 +17,7 @@ use Composer\Composer;
use JsonSchema\Validator; use JsonSchema\Validator;
use Seld\JsonLint\JsonParser; use Seld\JsonLint\JsonParser;
use Composer\Util\StreamContextFactory; use Composer\Util\StreamContextFactory;
use Composer\Util\RemoteFilesystem;
/** /**
* Reads/writes json files. * Reads/writes json files.
@ -34,15 +35,22 @@ class JsonFile
const JSON_UNESCAPED_UNICODE = 256; const JSON_UNESCAPED_UNICODE = 256;
private $path; private $path;
private $rfs;
/** /**
* Initializes json file reader/parser. * Initializes json file reader/parser.
* *
* @param string $lockFile path to a lockfile * @param string $lockFile path to a lockfile
* @param RemoteFilesystem $rfs required for loading http/https json files
*/ */
public function __construct($path) public function __construct($path, RemoteFilesystem $rfs = null)
{ {
$this->path = $path; $this->path = $path;
if (null === $rfs && preg_match('{^https?://}i', $path)) {
throw new \InvalidArgumentException('http urls require a RemoteFilesystem instance to be passed');
}
$this->rfs = $rfs;
} }
public function getPath() public function getPath()
@ -67,15 +75,14 @@ class JsonFile
*/ */
public function read() public function read()
{ {
$ctx = StreamContextFactory::getContext(array( try {
'http' => array( if ($this->rfs) {
'header' => 'User-Agent: Composer/'.Composer::VERSION."\r\n" $json = $this->rfs->getContents($this->path, $this->path, false);
) } else {
)); $json = file_get_contents($this->path);
}
$json = file_get_contents($this->path, false, $ctx); } catch (\Exception $e) {
if (!$json) { throw new \RuntimeException('Could not read '.$this->path.', you are probably offline ('.$e->getMessage().')');
throw new \RuntimeException('Could not read '.$this->path.', you are probably offline');
} }
return static::parseJson($json); return static::parseJson($json);

@ -15,6 +15,8 @@ namespace Composer\Repository;
use Composer\Package\Loader\ArrayLoader; use Composer\Package\Loader\ArrayLoader;
use Composer\Package\LinkConstraint\VersionConstraint; use Composer\Package\LinkConstraint\VersionConstraint;
use Composer\Json\JsonFile; use Composer\Json\JsonFile;
use Composer\IO\IOInterface;
use Composer\Util\RemoteFilesystem;
/** /**
* @author Jordi Boggiano <j.boggiano@seld.be> * @author Jordi Boggiano <j.boggiano@seld.be>
@ -22,9 +24,10 @@ use Composer\Json\JsonFile;
class ComposerRepository extends ArrayRepository class ComposerRepository extends ArrayRepository
{ {
protected $url; protected $url;
protected $io;
protected $packages; protected $packages;
public function __construct(array $config) public function __construct(array $config, IOInterface $io)
{ {
if (!preg_match('{^\w+://}', $config['url'])) { if (!preg_match('{^\w+://}', $config['url'])) {
// assume http as the default protocol // assume http as the default protocol
@ -36,12 +39,13 @@ class ComposerRepository extends ArrayRepository
} }
$this->url = $config['url']; $this->url = $config['url'];
$this->io = $io;
} }
protected function initialize() protected function initialize()
{ {
parent::initialize(); parent::initialize();
$json = new JsonFile($this->url.'/packages.json'); $json = new JsonFile($this->url.'/packages.json', new RemoteFilesystem($this->io));
$packages = $json->read(); $packages = $json->read();
if (!$packages) { if (!$packages) {
throw new \UnexpectedValueException('Could not parse package list from the '.$this->url.' repository'); throw new \UnexpectedValueException('Could not parse package list from the '.$this->url.' repository');

@ -13,7 +13,7 @@
namespace Composer\Repository; namespace Composer\Repository;
use Composer\Package\Loader\ArrayLoader; use Composer\Package\Loader\ArrayLoader;
use Composer\Util\StreamContextFactory; use Composer\Util\RemoteFilesystem;
/** /**
* @author Benjamin Eberlei <kontakt@beberlei.de> * @author Benjamin Eberlei <kontakt@beberlei.de>
@ -23,9 +23,9 @@ class PearRepository extends ArrayRepository
{ {
private $url; private $url;
private $channel; private $channel;
private $streamContext; private $rfs;
public function __construct(array $config) public function __construct(array $config, IOInterface $io, RemoteFilesystem $rfs = null)
{ {
if (!preg_match('{^https?://}', $config['url'])) { if (!preg_match('{^https?://}', $config['url'])) {
$config['url'] = 'http://'.$config['url']; $config['url'] = 'http://'.$config['url'];
@ -36,8 +36,8 @@ class PearRepository extends ArrayRepository
} }
$this->url = rtrim($config['url'], '/'); $this->url = rtrim($config['url'], '/');
$this->channel = !empty($config['channel']) ? $config['channel'] : null; $this->channel = !empty($config['channel']) ? $config['channel'] : null;
$this->rfs = $rfs ?: new RemoteFilesystem($io);
} }
protected function initialize() protected function initialize()
@ -47,7 +47,6 @@ class PearRepository extends ArrayRepository
set_error_handler(function($severity, $message, $file, $line) { set_error_handler(function($severity, $message, $file, $line) {
throw new \ErrorException($message, $severity, $severity, $file, $line); throw new \ErrorException($message, $severity, $severity, $file, $line);
}); });
$this->streamContext = StreamContextFactory::getContext();
$this->fetchFromServer(); $this->fetchFromServer();
restore_error_handler(); restore_error_handler();
} }
@ -120,7 +119,7 @@ class PearRepository extends ArrayRepository
); );
try { try {
$deps = file_get_contents($releaseLink . "/deps.".$pearVersion.".txt", false, $this->streamContext); $deps = $this->rfs->getContents($this->url, $releaseLink . "/deps.".$pearVersion.".txt", false);
} catch (\ErrorException $e) { } catch (\ErrorException $e) {
if (strpos($e->getMessage(), '404')) { if (strpos($e->getMessage(), '404')) {
continue; continue;
@ -289,7 +288,7 @@ class PearRepository extends ArrayRepository
*/ */
private function requestXml($url) private function requestXml($url)
{ {
$content = file_get_contents($url, false, $this->streamContext); $content = $this->rfs->getContents($this->url, $url, false);
if (!$content) { if (!$content) {
throw new \UnexpectedValueException('The PEAR channel at '.$url.' did not respond.'); throw new \UnexpectedValueException('The PEAR channel at '.$url.' did not respond.');
} }

Loading…
Cancel
Save