Skip to content

Commit

Permalink
add support for package based install type preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
sbuzonas committed Feb 26, 2015
1 parent e5985a9 commit bf08b6e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
4 changes: 2 additions & 2 deletions res/composer-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@
"description": "If true, the Composer autoloader will also look for classes in the PHP include path."
},
"preferred-install": {
"type": "string",
"description": "The install method Composer will prefer to use, defaults to auto and can be any of source, dist or auto."
"type": ["string", "object"],
"description": "The install method Composer will prefer to use, defaults to auto and can be any of source, dist, auto, or a hash of {\"pattern\": \"preference\"}."
},
"notify-on-install": {
"type": "boolean",
Expand Down
14 changes: 14 additions & 0 deletions src/Composer/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,20 @@ public function merge($config)
foreach ($config['config'] as $key => $val) {
if (in_array($key, array('github-oauth', 'http-basic')) && isset($this->config[$key])) {
$this->config[$key] = array_merge($this->config[$key], $val);
} elseif ('preferred-install' === $key && isset($this->config[$key])) {
if (is_string($val)) {
$val = array('*' => $val);
}
if (is_string($this->config[$key])) {
$this->config[$key] = array('*' => $this->config[$key]);
}
$this->config[$key] = array_merge($this->config[$key], $val);
// the full match pattern needs to be last
if (isset($this->config[$key]['*'])) {
$wildcard = $this->config[$key]['*'];
unset($this->config[$key]['*']);
$this->config[$key]['*'] = $wildcard;
}
} else {
$this->config[$key] = $val;
}
Expand Down
26 changes: 25 additions & 1 deletion src/Composer/Downloader/DownloadManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class DownloadManager
private $io;
private $preferDist = false;
private $preferSource = false;
private $packagePreferences = array();
private $filesystem;
private $downloaders = array();

Expand Down Expand Up @@ -69,6 +70,19 @@ public function setPreferDist($preferDist)
return $this;
}

/**
* Sets fine tuned preference settings for package level source/dist selection.
*
* @param array $preferences array of preferences by package patterns
* @return DownloadManager
*/
public function setPreferences(array $preferences)
{
$this->packagePreferences = $preferences;

return $this;
}

/**
* Sets whether to output download progress information for all registered
* downloaders
Expand Down Expand Up @@ -184,7 +198,17 @@ public function download(PackageInterface $package, $targetDir, $preferSource =
throw new \InvalidArgumentException('Package '.$package.' must have a source or dist specified');
}

if ((!$package->isDev() || $this->preferDist) && !$preferSource) {
if (!$this->preferDist && !$preferSource) {
foreach ($this->packagePreferences as $pattern => $preference) {
$pattern = '{^'.str_replace('*', '.*', $pattern).'$}i';
if (preg_match($pattern, $package->getName())) {
if ('dist' === $preference || (!$package->isDev() && 'auto' === $preference)) {
$sources = array_reverse($sources);
}
break;
}
}
} elseif ((!$package->isDev() || $this->preferDist) && !$preferSource) {
$sources = array_reverse($sources);
}

Expand Down
6 changes: 5 additions & 1 deletion src/Composer/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ public function createDownloadManager(IOInterface $io, Config $config, EventDisp
}

$dm = new Downloader\DownloadManager($io);
switch ($config->get('preferred-install')) {
switch ($preferred = $config->get('preferred-install')) {
case 'dist':
$dm->setPreferDist(true);
break;
Expand All @@ -396,6 +396,10 @@ public function createDownloadManager(IOInterface $io, Config $config, EventDisp
break;
}

if (is_array($preferred)) {
$dm->setPreferences($preferred);
}

$dm->setDownloader('git', new Downloader\GitDownloader($io, $config));
$dm->setDownloader('svn', new Downloader\SvnDownloader($io, $config));
$dm->setDownloader('hg', new Downloader\HgDownloader($io, $config));
Expand Down

0 comments on commit bf08b6e

Please sign in to comment.