Skip to content

Commit

Permalink
Always prepend repositories
Browse files Browse the repository at this point in the history
Follow up to 9645a93 that makes the use of
RepositoryManager::prependRepository() the default behavior for all
repository merges. This ensures that repos added by the
included/required config files to have a higher priority than the repos
added in the root composer.json and most importantly higher than the
default packagist repo.

Closes #95
  • Loading branch information
bd808 committed Jul 14, 2016
1 parent f6e7123 commit 55f354f
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 49 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -24,6 +24,8 @@ extensions which may be managed via Composer.
Installation
------------

Composer Merge Plugin requires Composer 1.0.0 or newer.

```
$ composer require wikimedia/composer-merge-plugin
```
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -16,7 +16,7 @@
"php": ">=5.3.2"
},
"require-dev": {
"composer/composer": "1.0.*@dev",
"composer/composer": "~1.0.0",
"jakub-onderka/php-parallel-lint": "~0.8",
"phpunit/phpunit": "~4.8|~5.0",
"squizlabs/php_codesniffer": "~2.1.0"
Expand Down
32 changes: 8 additions & 24 deletions src/Merge/ExtraPackage.php
Expand Up @@ -152,7 +152,7 @@ protected function loadPackage(array $json)
*/
public function mergeInto(RootPackageInterface $root, PluginState $state)
{
$this->addRepositories($root, $state->shouldPrependRepositories());
$this->prependRepositories($root);

$this->mergeRequires('require', $root, $state);

Expand Down Expand Up @@ -191,9 +191,8 @@ public function mergeDevInto(RootPackageInterface $root, PluginState $state)
* to the given package and the global repository manager.
*
* @param RootPackageInterface $root
* @param bool $prepend Should repositories be prepended to repository manager.
*/
protected function addRepositories(RootPackageInterface $root, $prepend)
protected function prependRepositories(RootPackageInterface $root)
{
if (!isset($this->json['repositories'])) {
return;
Expand All @@ -205,35 +204,20 @@ protected function addRepositories(RootPackageInterface $root, $prepend)
if (!isset($repoJson['type'])) {
continue;
}
if ($prepend) {
$this->logger->info("Prepending {$repoJson['type']} repository");
} else {
$this->logger->info("Adding {$repoJson['type']} repository");
}
$this->logger->info("Prepending {$repoJson['type']} repository");
$repo = $repoManager->createRepository(
$repoJson['type'],
$repoJson
);
if ($prepend) {
$repoManager->prependRepository($repo);
} else {
$repoManager->addRepository($repo);
}
$repoManager->prependRepository($repo);
$newRepos[] = $repo;
}

$unwrapped = self::unwrapIfNeeded($root, 'setRepositories');
if ($prepend) {
$unwrapped->setRepositories(array_merge(
$newRepos,
$root->getRepositories()
));
} else {
$unwrapped->setRepositories(array_merge(
$root->getRepositories(),
$newRepos
));
}
$unwrapped->setRepositories(array_merge(
$newRepos,
$root->getRepositories()
));
}

/**
Expand Down
19 changes: 0 additions & 19 deletions src/Merge/PluginState.php
Expand Up @@ -74,13 +74,6 @@ class PluginState
*/
protected $mergeExtra = false;

/**
* Whether to prepend repositories to repository manager.
*
* @var bool $prependRepositories
*/
protected $prependRepositories = false;

/**
* Whether to merge the extra section in a deep / recursive way.
*
Expand Down Expand Up @@ -139,7 +132,6 @@ public function loadSettings()
'require' => array(),
'recurse' => true,
'replace' => false,
'prepend-repositories' => false,
'merge-dev' => true,
'merge-extra' => false,
'merge-extra-deep' => false,
Expand All @@ -153,7 +145,6 @@ public function loadSettings()
$config['require'] : array($config['require']);
$this->recurse = (bool)$config['recurse'];
$this->replace = (bool)$config['replace'];
$this->prependRepositories = (bool)$config['prepend-repositories'];
$this->mergeDev = (bool)$config['merge-dev'];
$this->mergeExtra = (bool)$config['merge-extra'];
$this->mergeExtraDeep = (bool)$config['merge-extra-deep'];
Expand Down Expand Up @@ -353,16 +344,6 @@ public function shouldMergeExtra()
return $this->mergeExtra;
}

/**
* Should the merger prepend repositories to repository manager (instead of adding them to end of the list).
*
* @return bool
*/
public function shouldPrependRepositories()
{
return $this->prependRepositories;
}

/**
* Should the extra section be merged deep / recursively?
*
Expand Down
5 changes: 2 additions & 3 deletions tests/phpunit/MergePluginTest.php
Expand Up @@ -348,7 +348,7 @@ function ($args) use ($that, $io) {
);
}
);
$repoManager->addRepository(Argument::any())->will(
$repoManager->prependRepository(Argument::any())->will(
function ($args) use ($that) {
$that->assertInstanceOf(
'Composer\Repository\VcsRepository',
Expand Down Expand Up @@ -396,7 +396,6 @@ function ($args) use ($that) {

/**
* Given a root package with an extra section
* and prepend-repositories mode is active
* and a composer.local.json with a repository
* When the plugin is run
* Then the repository from composer.local.json should be prepended to root package repository list
Expand Down Expand Up @@ -857,7 +856,7 @@ public function testHasBranchAlias($fireInit)
new \Composer\Config()
);
});
$repoManager->addRepository(Argument::any())->shouldBeCalled();
$repoManager->prependRepository(Argument::any())->shouldBeCalled();
$this->composer->getRepositoryManager()->will(
function () use ($repoManager) {
return $repoManager->reveal();
Expand Down
3 changes: 1 addition & 2 deletions tests/phpunit/fixtures/testPrependRepositories/composer.json
Expand Up @@ -10,8 +10,7 @@
},
"extra": {
"merge-plugin": {
"include": "composer.local.json",
"prepend-repositories": true
"include": "composer.local.json"
}
}
}

0 comments on commit 55f354f

Please sign in to comment.