From ea30392105a769c18af62891a02d3e679502010d Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Tue, 23 Feb 2016 23:06:21 +0900 Subject: [PATCH 1/2] RepositoryManager::prependRepository() This method is useful for dynamically adding repositories with higher priority than Packagist, e.g. from a Composer plugin. --- src/Composer/Repository/RepositoryManager.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Composer/Repository/RepositoryManager.php b/src/Composer/Repository/RepositoryManager.php index 05dcc1b4a..649c66cd9 100644 --- a/src/Composer/Repository/RepositoryManager.php +++ b/src/Composer/Repository/RepositoryManager.php @@ -89,6 +89,18 @@ class RepositoryManager $this->repositories[] = $repository; } + /** + * Adds a repository to the beginning of the chain + * + * This is useful when injecting additional repositories that should trump Packagist, e.g. from a plugin. + * + * @param RepositoryInterface $repository repository instance + */ + public function prependRepository(RepositoryInterface $repository) + { + array_unshift($this->repositories, $repository); + } + /** * Returns a new repository for a specific installation type. * From f47c2c9ed7aca48473243bf1204261797d696aab Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Wed, 24 Feb 2016 00:57:10 +0900 Subject: [PATCH 2/2] Add test case for RepositoryManager::prependRepository method --- .../Test/Repository/RepositoryManagerTest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/Composer/Test/Repository/RepositoryManagerTest.php b/tests/Composer/Test/Repository/RepositoryManagerTest.php index 13e51fb18..bedd07f8e 100644 --- a/tests/Composer/Test/Repository/RepositoryManagerTest.php +++ b/tests/Composer/Test/Repository/RepositoryManagerTest.php @@ -32,6 +32,22 @@ class RepositoryManagerTest extends TestCase } } + public function testPrepend() + { + $rm = new RepositoryManager( + $this->getMock('Composer\IO\IOInterface'), + $this->getMock('Composer\Config'), + $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')->disableOriginalConstructor()->getMock() + ); + + $repository1 = $this->getMock('Composer\Repository\RepositoryInterface'); + $repository2 = $this->getMock('Composer\Repository\RepositoryInterface'); + $rm->addRepository($repository1); + $rm->prependRepository($repository2); + + $this->assertEquals(array($repository2, $repository1), $rm->getRepositories()); + } + /** * @dataProvider creationCases */