diff --git a/README.md b/README.md index fd5bcd6..6c833dc 100644 --- a/README.md +++ b/README.md @@ -3,11 +3,11 @@ Metadata and some supporting PHP logic for determining which branches of various GitHub repositories relate to which versions of Silverstripe CMS. > [!IMPORTANT] -> Only the `main` branch of this repository is maintained. +> Only the `main` branch of this repository is maintained. Stable tags must be manually created from the `main` branch. There is no auto tagging via a GitHub action. All branches other than `main` are legacy and should not be referenced going forward. You can fetch the JSON by simply fetching the raw copy of `repositories.json` file, e.g. . -If you've included this module as a compser dependency then you can use `SilverStripe\SupportedModules\MetaData::getAllRepositoryMetaData()` which will fetch the latest version of the JSON file from raw.githubusercontent.com. +If you've included this module as a composer dependency then you can use `SilverStripe\SupportedModules\MetaData::getAllRepositoryMetaData()` which will fetch the latest version of the JSON file from raw.githubusercontent.com. There is a local copy of `repositories.json` in the module, though it is not guaranteed to be up to date, so only use this if fetching a fresh copy of this file is not viable. ## Format diff --git a/src/MetaData.php b/src/MetaData.php index fa0e42e..39d5089 100644 --- a/src/MetaData.php +++ b/src/MetaData.php @@ -179,13 +179,30 @@ public static function removeReposNotInCmsMajor(array $metadata, string|int $cms } /** - * Get all metadata about all repositories we have information about. + * Get all metadata about all repositories we have information about. This will make a newtwork + * request to fetch the latest data from the supported-modules repository. * @param bool $categorised If true, output is grouped by category. */ public static function getAllRepositoryMetaData(bool $categorised = true): array + { + return MetaData::getRepositoryMetaDataInner($categorised, false); + } + + /** + * Get all metadata about all repositories we have information about from the local file. + * Use this if making a network request is not possible or desired, and the downside from the data + * being potentially out-of-date is acceptable. + * @param bool $categorised If true, output is grouped by category. + */ + public static function getAllRepositoryMetaDataFromLocal(bool $categorised = true): array + { + return MetaData::getRepositoryMetaDataInner($categorised, true); + } + + private static function getRepositoryMetaDataInner(bool $categorised = true, bool $doFetch = true): array { if (empty(self::$repositoryMetaData)) { - if (self::$isRunningUnitTests) { + if (self::$isRunningUnitTests || !$doFetch) { $rawJson = file_get_contents(__DIR__ . '/../repositories.json'); } else { // Dynamicallly fetch the latest data from the supported-modules repository diff --git a/tests/MetaDataTest.php b/tests/MetaDataTest.php index 429bc5f..4583362 100644 --- a/tests/MetaDataTest.php +++ b/tests/MetaDataTest.php @@ -190,6 +190,16 @@ public function testRemoveReposNotInCmsMajor(int|string $cmsMajor, bool $keepWil } } + public function testgetAllRepositoryMetaDataFromLocal(): void + { + $repos = MetaData::getAllRepositoryMetaDataFromLocal(); + $expected = []; + foreach ($repos as $repos) { + $expected = array_merge($expected, $repos); + } + $this->assertSame($expected, MetaData::getAllRepositoryMetaData(false)); + } + public function testGetAllRepositoryMetaDataNoCategories(): void { $withCategories = MetaData::getAllRepositoryMetaData(true);