From 3bd2dd4813b9e0fe5d1834963e2c2f453c835036 Mon Sep 17 00:00:00 2001 From: SychO9 Date: Tue, 9 Nov 2021 20:32:09 +0100 Subject: [PATCH] Move exception cause guessing out of handler --- .../ComposerCommandFailedException.php | 10 ++++++++++ .../ComposerCommandFailedExceptionHandler.php | 14 +------------- .../ComposerRequireFailedException.php | 17 ++++++++++++++++- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/extensions/package-manager/src/Exception/ComposerCommandFailedException.php b/extensions/package-manager/src/Exception/ComposerCommandFailedException.php index 452fcd03f..601ac859b 100755 --- a/extensions/package-manager/src/Exception/ComposerCommandFailedException.php +++ b/extensions/package-manager/src/Exception/ComposerCommandFailedException.php @@ -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); + } } diff --git a/extensions/package-manager/src/Exception/ComposerCommandFailedExceptionHandler.php b/extensions/package-manager/src/Exception/ComposerCommandFailedExceptionHandler.php index 01e5ec558..13cd5e100 100755 --- a/extensions/package-manager/src/Exception/ComposerCommandFailedExceptionHandler.php +++ b/extensions/package-manager/src/Exception/ComposerCommandFailedExceptionHandler.php @@ -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(); } } diff --git a/extensions/package-manager/src/Exception/ComposerRequireFailedException.php b/extensions/package-manager/src/Exception/ComposerRequireFailedException.php index 135e4abc8..8046ee204 100755 --- a/extensions/package-manager/src/Exception/ComposerRequireFailedException.php +++ b/extensions/package-manager/src/Exception/ComposerRequireFailedException.php @@ -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; + } }