1
0
Fork 0

Add home config key and use it to create the cache instance

pull/551/head
Jordi Boggiano 2012-04-09 16:36:06 +02:00
parent 28d0f4a7c8
commit fc29487a2a
7 changed files with 40 additions and 33 deletions

View File

@ -25,15 +25,10 @@ class Cache
private $root; private $root;
private $enabled = true; private $enabled = true;
public function __construct(IOInterface $io, $cacheKey = null) public function __construct(IOInterface $io, $cacheDir)
{ {
$this->io = $io; $this->io = $io;
$this->root = rtrim($cacheDir, '/\\') . '/';
if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
$this->root = getenv('APPDATA') . rtrim('/Composer/cache/' . $cacheKey, '/') . '/';
} else {
$this->root = getenv('HOME') . rtrim('/.composer/cache/' . $cacheKey, '/') . '/';
}
if (!is_dir($this->root)) { if (!is_dir($this->root)) {
if (!@mkdir($this->root, 0777, true)) { if (!@mkdir($this->root, 0777, true)) {

View File

@ -58,6 +58,9 @@ class Config
$env = 'COMPOSER_' . strtoupper(strtr($key, '-', '_')); $env = 'COMPOSER_' . strtoupper(strtr($key, '-', '_'));
return $this->process(getenv($env) ?: $this->config[$key]); return $this->process(getenv($env) ?: $this->config[$key]);
case 'home':
return rtrim($this->process($this->config[$key]), '/\\');
default: default:
return $this->process($this->config[$key]); return $this->process($this->config[$key]);
} }

View File

@ -32,19 +32,22 @@ class Factory
// load main Composer configuration // load main Composer configuration
if (!$home = getenv('COMPOSER_HOME')) { if (!$home = getenv('COMPOSER_HOME')) {
if (defined('PHP_WINDOWS_VERSION_MAJOR')) { if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
$home = getenv('APPDATA') . '/Composer/'; $home = getenv('APPDATA') . '/Composer';
} else { } else {
$home = getenv('HOME') . '/.composer/'; $home = getenv('HOME') . '/.composer';
} }
} }
$config = new Config(); $config = new Config();
$file = new JsonFile($home.'config.json'); $file = new JsonFile($home.'/config.json');
if ($file->exists()) { if ($file->exists()) {
$config->merge($file->read()); $config->merge($file->read());
} }
// add home dir to the config
$config->merge(array('config' => array('home' => $home)));
return $config; return $config;
} }
@ -91,7 +94,7 @@ class Factory
ProcessExecutor::setTimeout((int) $config->get('process-timeout')); ProcessExecutor::setTimeout((int) $config->get('process-timeout'));
// initialize repository manager // initialize repository manager
$rm = $this->createRepositoryManager($io); $rm = $this->createRepositoryManager($io, $config);
// load default repository unless it's explicitly disabled // load default repository unless it's explicitly disabled
$localConfig = $this->addPackagistRepository($localConfig); $localConfig = $this->addPackagistRepository($localConfig);
@ -130,9 +133,9 @@ class Factory
return $composer; return $composer;
} }
protected function createRepositoryManager(IOInterface $io) protected function createRepositoryManager(IOInterface $io, Config $config)
{ {
$rm = new RepositoryManager($io); $rm = new RepositoryManager($io, $config);
$rm->setRepositoryClass('composer', 'Composer\Repository\ComposerRepository'); $rm->setRepositoryClass('composer', 'Composer\Repository\ComposerRepository');
$rm->setRepositoryClass('vcs', 'Composer\Repository\VcsRepository'); $rm->setRepositoryClass('vcs', 'Composer\Repository\VcsRepository');
$rm->setRepositoryClass('package', 'Composer\Repository\PackageRepository'); $rm->setRepositoryClass('package', 'Composer\Repository\PackageRepository');

View File

@ -16,6 +16,7 @@ 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\Cache; use Composer\Cache;
use Composer\Config;
use Composer\IO\IOInterface; use Composer\IO\IOInterface;
use Composer\Util\RemoteFilesystem; use Composer\Util\RemoteFilesystem;
@ -29,20 +30,20 @@ class ComposerRepository extends ArrayRepository
protected $packages; protected $packages;
protected $cache; protected $cache;
public function __construct(array $config, IOInterface $io) public function __construct(array $repoConfig, IOInterface $io, Config $config)
{ {
if (!preg_match('{^\w+://}', $config['url'])) { if (!preg_match('{^\w+://}', $repoConfig['url'])) {
// assume http as the default protocol // assume http as the default protocol
$config['url'] = 'http://'.$config['url']; $repoConfig['url'] = 'http://'.$repoConfig['url'];
} }
$config['url'] = rtrim($config['url'], '/'); $repoConfig['url'] = rtrim($repoConfig['url'], '/');
if (function_exists('filter_var') && !filter_var($config['url'], FILTER_VALIDATE_URL)) { if (function_exists('filter_var') && !filter_var($repoConfig['url'], FILTER_VALIDATE_URL)) {
throw new \UnexpectedValueException('Invalid url given for Composer repository: '.$config['url']); throw new \UnexpectedValueException('Invalid url given for Composer repository: '.$repoConfig['url']);
} }
$this->url = $config['url']; $this->url = $repoConfig['url'];
$this->io = $io; $this->io = $io;
$this->cache = new Cache($io, preg_replace('{[^a-z0-9.]}', '-', $this->url)); $this->cache = new Cache($io, $config->get('home').'/cache/'.preg_replace('{[^a-z0-9.]}', '-', $this->url));
} }
protected function initialize() protected function initialize()

View File

@ -16,6 +16,7 @@ use Composer\IO\IOInterface;
use Composer\Package\Loader\ArrayLoader; use Composer\Package\Loader\ArrayLoader;
use Composer\Util\RemoteFilesystem; use Composer\Util\RemoteFilesystem;
use Composer\Json\JsonFile; use Composer\Json\JsonFile;
use Composer\Config;
use Composer\Downloader\TransportException; use Composer\Downloader\TransportException;
/** /**
@ -31,18 +32,18 @@ class PearRepository extends ArrayRepository
private $io; private $io;
private $rfs; private $rfs;
public function __construct(array $config, IOInterface $io, RemoteFilesystem $rfs = null) public function __construct(array $repoConfig, IOInterface $io, Config $config, RemoteFilesystem $rfs = null)
{ {
if (!preg_match('{^https?://}', $config['url'])) { if (!preg_match('{^https?://}', $repoConfig['url'])) {
$config['url'] = 'http://'.$config['url']; $repoConfig['url'] = 'http://'.$repoConfig['url'];
} }
if (function_exists('filter_var') && !filter_var($config['url'], FILTER_VALIDATE_URL)) { if (function_exists('filter_var') && !filter_var($repoConfig['url'], FILTER_VALIDATE_URL)) {
throw new \UnexpectedValueException('Invalid url given for PEAR repository: '.$config['url']); throw new \UnexpectedValueException('Invalid url given for PEAR repository: '.$repoConfig['url']);
} }
$this->url = rtrim($config['url'], '/'); $this->url = rtrim($repoConfig['url'], '/');
$this->channel = !empty($config['channel']) ? $config['channel'] : null; $this->channel = !empty($repoConfig['channel']) ? $repoConfig['channel'] : null;
$this->io = $io; $this->io = $io;
$this->rfs = $rfs ?: new RemoteFilesystem($this->io); $this->rfs = $rfs ?: new RemoteFilesystem($this->io);
} }

View File

@ -13,6 +13,7 @@
namespace Composer\Repository; namespace Composer\Repository;
use Composer\IO\IOInterface; use Composer\IO\IOInterface;
use Composer\Config;
/** /**
* Repositories manager. * Repositories manager.
@ -27,10 +28,12 @@ class RepositoryManager
private $repositories = array(); private $repositories = array();
private $repositoryClasses = array(); private $repositoryClasses = array();
private $io; private $io;
private $config;
public function __construct(IOInterface $io) public function __construct(IOInterface $io, Config $config)
{ {
$this->io = $io; $this->io = $io;
$this->config = $config;
} }
/** /**
@ -94,7 +97,7 @@ class RepositoryManager
} }
$class = $this->repositoryClasses[$type]; $class = $this->repositoryClasses[$type];
return new $class($config, $this->io); return new $class($config, $this->io, $this->config);
} }
/** /**

View File

@ -19,6 +19,7 @@ use Composer\Package\PackageInterface;
use Composer\Package\AliasPackage; use Composer\Package\AliasPackage;
use Composer\Package\Loader\ArrayLoader; use Composer\Package\Loader\ArrayLoader;
use Composer\IO\IOInterface; use Composer\IO\IOInterface;
use Composer\Config;
/** /**
* @author Jordi Boggiano <j.boggiano@seld.be> * @author Jordi Boggiano <j.boggiano@seld.be>
@ -32,7 +33,7 @@ class VcsRepository extends ArrayRepository
protected $versionParser; protected $versionParser;
protected $type; protected $type;
public function __construct(array $config, IOInterface $io, array $drivers = null) public function __construct(array $repoConfig, IOInterface $io, Config $config = null, array $drivers = null)
{ {
$this->drivers = $drivers ?: array( $this->drivers = $drivers ?: array(
'github' => 'Composer\Repository\Vcs\GitHubDriver', 'github' => 'Composer\Repository\Vcs\GitHubDriver',
@ -43,9 +44,9 @@ class VcsRepository extends ArrayRepository
'hg' => 'Composer\Repository\Vcs\HgDriver', 'hg' => 'Composer\Repository\Vcs\HgDriver',
); );
$this->url = $config['url']; $this->url = $repoConfig['url'];
$this->io = $io; $this->io = $io;
$this->type = isset($config['type']) ? $config['type'] : 'vcs'; $this->type = isset($repoConfig['type']) ? $repoConfig['type'] : 'vcs';
$this->verbose = $io->isVerbose(); $this->verbose = $io->isVerbose();
} }