From e5ac92b4102cb658d827268c38e5bd2b81b7a090 Mon Sep 17 00:00:00 2001 From: Soner Sayakci Date: Wed, 6 Nov 2024 17:03:14 +0100 Subject: [PATCH] feat: allow optional path repository --- res/composer-schema.json | 3 ++- src/Composer/Repository/PathRepository.php | 6 +++++- tests/Composer/Test/Repository/PathRepositoryTest.php | 7 +++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/res/composer-schema.json b/res/composer-schema.json index 9f76bc4b7..90b3671ba 100644 --- a/res/composer-schema.json +++ b/res/composer-schema.json @@ -930,7 +930,8 @@ "options": { "type": "object", "properties": { - "symlink": { "type": ["boolean", "null"] } + "symlink": { "type": ["boolean", "null"] }, + "optional": { "type": "boolean" } }, "additionalProperties": true } diff --git a/src/Composer/Repository/PathRepository.php b/src/Composer/Repository/PathRepository.php index 0b8d99236..eb7916328 100644 --- a/src/Composer/Repository/PathRepository.php +++ b/src/Composer/Repository/PathRepository.php @@ -105,7 +105,7 @@ class PathRepository extends ArrayRepository implements ConfigurableRepositoryIn /** * Initializes path repository. * - * @param array{url?: string, options?: array{symlink?: bool, reference?: string, relative?: bool, versions?: array}} $repoConfig + * @param array{url?: string, options?: array{symlink?: bool, reference?: string, relative?: bool, versions?: array, optional?: bool}} $repoConfig */ public function __construct(array $repoConfig, IOInterface $io, Config $config, ?HttpDownloader $httpDownloader = null, ?EventDispatcher $dispatcher = null, ?ProcessExecutor $process = null) { @@ -160,6 +160,10 @@ class PathRepository extends ArrayRepository implements ConfigurableRepositoryIn } } + if (array_key_exists('optional', $this->options) && $this->options['optional'] === true) { + return; + } + throw new \RuntimeException('The `url` supplied for the path (' . $this->url . ') repository does not exist'); } diff --git a/tests/Composer/Test/Repository/PathRepositoryTest.php b/tests/Composer/Test/Repository/PathRepositoryTest.php index d5deb664b..a8b515aef 100644 --- a/tests/Composer/Test/Repository/PathRepositoryTest.php +++ b/tests/Composer/Test/Repository/PathRepositoryTest.php @@ -161,6 +161,13 @@ class PathRepositoryTest extends TestCase } } + public function testLoadPackageFromFileSystemWithIncorrectPathButOptional(): void + { + $repositoryUrl = implode(DIRECTORY_SEPARATOR, [__DIR__, 'Fixtures', 'path', 'missing']); + $repository = $this->createPathRepo(['url' => $repositoryUrl, 'options' => ['optional' => true]]); + self::assertEmpty($repository->getPackages()); + } + /** * @param array $options */