Move exception cause guessing out of handler

This commit is contained in:
SychO9 2021-11-09 20:32:09 +01:00
parent 7f5f5687db
commit 3bd2dd4813
3 changed files with 27 additions and 14 deletions

View File

@ -24,4 +24,14 @@ class ComposerCommandFailedException extends Exception
parent::__construct($output);
}
public function guessCause(): ?string
{
}
protected function getRawPackageName(): string
{
return preg_replace('/^([A-z0-9-_\/]+)(?::.*|)$/i', '$1', $this->packageName);
}
}

View File

@ -13,8 +13,6 @@ use Flarum\Foundation\ErrorHandling\HandledError;
class ComposerCommandFailedExceptionHandler
{
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';
public function handle(ComposerCommandFailedException $e): HandledError
{
return (new HandledError(
@ -39,16 +37,6 @@ class ComposerCommandFailedExceptionHandler
protected function guessCause(ComposerCommandFailedException $e): ?string
{
$rawPackageName = preg_replace('/^([A-z0-9-_\/]+)(?::.*|)$/i', '$1', $e->packageName);
if ($e instanceof ComposerRequireFailedException) {
$hasMatches = preg_match(str_replace('{PACKAGE_NAME}', preg_quote($rawPackageName, '/'), self::INCOMPATIBLE_REGEX), $e->getMessage(), $matches);
if ($hasMatches) {
return 'extension_incompatible_with_instance';
}
}
return null;
return $e->guessCause();
}
}

View File

@ -11,5 +11,20 @@ namespace Flarum\PackageManager\Exception;
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';
public function guessCause(): ?string
{
$hasMatches = preg_match(
str_replace('{PACKAGE_NAME}', preg_quote($this->getRawPackageName(), '/'), self::INCOMPATIBLE_REGEX),
$this->getMessage(),
$matches
);
if ($hasMatches) {
return 'extension_incompatible_with_instance';
}
return null;
}
}