Skip to content

Commit

Permalink
chore: guess package not found error
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 committed Dec 8, 2023
1 parent 323cc1a commit 5b1ddf4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions extensions/package-manager/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 5b1ddf4

Please sign in to comment.