From 5b1ddf4806d1e6e33618c683a0163462fe32bc66 Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Fri, 8 Dec 2023 20:18:01 +0100 Subject: [PATCH] chore: guess package not found error --- extensions/package-manager/locale/en.yml | 1 + .../ComposerRequireFailedException.php | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/extensions/package-manager/locale/en.yml b/extensions/package-manager/locale/en.yml index e9232721c5..86e69182cc 100755 --- a/extensions/package-manager/locale/en.yml +++ b/extensions/package-manager/locale/en.yml @@ -18,6 +18,7 @@ flarum-package-manager: guessed_cause: extension_incompatible_with_instance: The extension is most likely incompatible with your current Flarum instance. + extension_not_found: The extension was not found or does not exist. extensions_incompatible_with_new_major: > Some installed extensions are not compatible with the newest major release. Please wait until the extensions are updated to be compatible by the authors, or remove them before proceeding. diff --git a/extensions/package-manager/src/Exception/ComposerRequireFailedException.php b/extensions/package-manager/src/Exception/ComposerRequireFailedException.php index 8046ee2047..1b323d79d2 100755 --- a/extensions/package-manager/src/Exception/ComposerRequireFailedException.php +++ b/extensions/package-manager/src/Exception/ComposerRequireFailedException.php @@ -11,20 +11,31 @@ class ComposerRequireFailedException extends ComposerCommandFailedException { - protected const INCOMPATIBLE_REGEX = '/(?:(?: +- {PACKAGE_NAME}(?: v[0-9A-z.-]+ requires|\[[^\[\]]+\] require) flarum\/core)|(?:Could not find a version of package {PACKAGE_NAME} matching your minim)|(?: +- Root composer.json requires {PACKAGE_NAME} [^,]+, found {PACKAGE_NAME}\[[^\[\]]+\]+ but it does not match your minimum-stability))/m'; + protected const INCOMPATIBLE_REGEX = '/(?:(?: +- {PACKAGE_NAME}(?: v[0-9A-z.-]+ requires|\[[^\[\]]+\] require) flarum\/core)|(?:Could not find a version of package {PACKAGE_NAME} matching your minim)|(?: +- Root composer\.json requires {PACKAGE_NAME} [^,]+, found {PACKAGE_NAME}\[[^\[\]]+\]+ but it does not match your minimum-stability))/m'; + protected const NOT_FOUND_REGEX = '/(?:(?: +- Root composer\.json requires {PACKAGE_NAME}, it could not be found in any version, there may be a typo in the package name.))/m'; public function guessCause(): ?string { - $hasMatches = preg_match( + $hasIncompatibleMatches = preg_match( str_replace('{PACKAGE_NAME}', preg_quote($this->getRawPackageName(), '/'), self::INCOMPATIBLE_REGEX), $this->getMessage(), $matches ); - if ($hasMatches) { + if ($hasIncompatibleMatches) { return 'extension_incompatible_with_instance'; } + $hasNotFoundMatches = preg_match( + str_replace('{PACKAGE_NAME}', preg_quote($this->getRawPackageName(), '/'), self::NOT_FOUND_REGEX), + $this->getMessage(), + $matches + ); + + if ($hasNotFoundMatches) { + return 'extension_not_found'; + } + return null; } }