From 6529fabb245f678379d4030d27996a3a8f10a30e Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 23 Apr 2020 10:52:33 +0200 Subject: [PATCH] Add isFresh to InstalledRepositoryInterface and make sure local repo is always an InstalledRepositoryInterface --- src/Composer/Repository/FilesystemRepository.php | 2 +- src/Composer/Repository/InstalledArrayRepository.php | 10 ++++++++++ .../Repository/InstalledFilesystemRepository.php | 8 ++++++++ .../Repository/InstalledRepositoryInterface.php | 4 ++++ src/Composer/Repository/RepositoryManager.php | 6 +++--- 5 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/Composer/Repository/FilesystemRepository.php b/src/Composer/Repository/FilesystemRepository.php index dcd94f7b3..4bf4cb6ca 100644 --- a/src/Composer/Repository/FilesystemRepository.php +++ b/src/Composer/Repository/FilesystemRepository.php @@ -28,7 +28,7 @@ use Composer\Util\Filesystem; */ class FilesystemRepository extends WritableArrayRepository { - private $file; + protected $file; private $dumpVersions; private $rootPackage; diff --git a/src/Composer/Repository/InstalledArrayRepository.php b/src/Composer/Repository/InstalledArrayRepository.php index de1dd67d8..e8a5f8194 100644 --- a/src/Composer/Repository/InstalledArrayRepository.php +++ b/src/Composer/Repository/InstalledArrayRepository.php @@ -25,4 +25,14 @@ class InstalledArrayRepository extends WritableArrayRepository implements Instal { return 'installed '.parent::getRepoName(); } + + /** + * {@inheritDoc} + */ + public function isFresh() + { + // this is not a completely correct implementation but there is no way to + // distinguish an empty repo and a newly created one given this is all in-memory + return $this->count() === 0; + } } diff --git a/src/Composer/Repository/InstalledFilesystemRepository.php b/src/Composer/Repository/InstalledFilesystemRepository.php index bf81734d4..f526f31c2 100644 --- a/src/Composer/Repository/InstalledFilesystemRepository.php +++ b/src/Composer/Repository/InstalledFilesystemRepository.php @@ -23,4 +23,12 @@ class InstalledFilesystemRepository extends FilesystemRepository implements Inst { return 'installed '.parent::getRepoName(); } + + /** + * {@inheritDoc} + */ + public function isFresh() + { + return !$this->file->exists(); + } } diff --git a/src/Composer/Repository/InstalledRepositoryInterface.php b/src/Composer/Repository/InstalledRepositoryInterface.php index 19b095b2a..b5d8a264e 100644 --- a/src/Composer/Repository/InstalledRepositoryInterface.php +++ b/src/Composer/Repository/InstalledRepositoryInterface.php @@ -21,4 +21,8 @@ namespace Composer\Repository; */ interface InstalledRepositoryInterface extends WritableRepositoryInterface { + /** + * @return bool true if packages were never installed in this repository + */ + public function isFresh(); } diff --git a/src/Composer/Repository/RepositoryManager.php b/src/Composer/Repository/RepositoryManager.php index c5da49cdb..264115c10 100644 --- a/src/Composer/Repository/RepositoryManager.php +++ b/src/Composer/Repository/RepositoryManager.php @@ -163,9 +163,9 @@ class RepositoryManager /** * Sets local repository for the project. * - * @param WritableRepositoryInterface $repository repository instance + * @param InstalledRepositoryInterface $repository repository instance */ - public function setLocalRepository(WritableRepositoryInterface $repository) + public function setLocalRepository(InstalledRepositoryInterface $repository) { $this->localRepository = $repository; } @@ -173,7 +173,7 @@ class RepositoryManager /** * Returns local repository for the project. * - * @return WritableRepositoryInterface + * @return InstalledRepositoryInterface */ public function getLocalRepository() {