1
0
mirror of https://github.com/flarum/core.git synced 2025-07-16 14:26:25 +02:00

Auto append :* when installing an extension if not specifying a version

This commit is contained in:
SychO9
2021-11-09 20:06:03 +01:00
parent 594bbe4f34
commit 7f5f5687db
3 changed files with 53 additions and 4 deletions

View File

@ -66,12 +66,19 @@ class RequireExtensionHandler
throw new ExtensionAlreadyInstalledException($extension); throw new ExtensionAlreadyInstalledException($extension);
} }
$packageName = $command->package;
// Auto append :* if not requiring a specific version.
if (strpos($packageName, ':') === false) {
$packageName .= ":*";
}
$output = $this->composer->run( $output = $this->composer->run(
new StringInput("require $command->package") new StringInput("require $packageName")
); );
if ($output->getExitCode() !== 0) { if ($output->getExitCode() !== 0) {
throw new ComposerRequireFailedException($command->package, $output->getContents()); throw new ComposerRequireFailedException($packageName, $output->getContents());
} }
$this->events->dispatch( $this->events->dispatch(

View File

@ -13,7 +13,7 @@ use Flarum\Foundation\ErrorHandling\HandledError;
class ComposerCommandFailedExceptionHandler class ComposerCommandFailedExceptionHandler
{ {
protected const INCOMPATIBLE_REGEX = '/(?:(?: +- {PACKAGE_NAME} v[0-9A-z.-]+ requires flarum\/core)|(?:Could not find a version of package {PACKAGE_NAME} matching your minim))/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';
public function handle(ComposerCommandFailedException $e): HandledError public function handle(ComposerCommandFailedException $e): HandledError
{ {
@ -39,8 +39,10 @@ class ComposerCommandFailedExceptionHandler
protected function guessCause(ComposerCommandFailedException $e): ?string protected function guessCause(ComposerCommandFailedException $e): ?string
{ {
$rawPackageName = preg_replace('/^([A-z0-9-_\/]+)(?::.*|)$/i', '$1', $e->packageName);
if ($e instanceof ComposerRequireFailedException) { if ($e instanceof ComposerRequireFailedException) {
$hasMatches = preg_match(str_replace('{PACKAGE_NAME}', preg_quote($e->packageName, '/'), self::INCOMPATIBLE_REGEX), $e->getMessage(), $matches); $hasMatches = preg_match(str_replace('{PACKAGE_NAME}', preg_quote($rawPackageName, '/'), self::INCOMPATIBLE_REGEX), $e->getMessage(), $matches);
if ($hasMatches) { if ($hasMatches) {
return 'extension_incompatible_with_instance'; return 'extension_incompatible_with_instance';

View File

@ -63,6 +63,26 @@ class RequireExtensionTest extends TestCase
$this->assertExtensionExists('v17development-blog'); $this->assertExtensionExists('v17development-blog');
} }
/**
* @test
*/
public function requiring_a_compatible_extension_with_specific_version_works()
{
$response = $this->send(
$this->request('POST', '/api/package-manager/extensions', [
'authenticatedAs' => 1,
'json' => [
'data' => [
'package' => 'v17development/flarum-blog:0.4.0'
]
]
])
);
$this->assertEquals(200, $response->getStatusCode());
$this->assertExtensionExists('v17development-blog');
}
/** /**
* @test * @test
*/ */
@ -82,4 +102,24 @@ class RequireExtensionTest extends TestCase
$this->assertEquals(409, $response->getStatusCode()); $this->assertEquals(409, $response->getStatusCode());
$this->assertEquals('extension_incompatible_with_instance', $this->guessedCause($response)); $this->assertEquals('extension_incompatible_with_instance', $this->guessedCause($response));
} }
/**
* @test
*/
public function requiring_an_uncompatible_extension_with_specific_version_fails()
{
$response = $this->send(
$this->request('POST', '/api/package-manager/extensions', [
'authenticatedAs' => 1,
'json' => [
'data' => [
'package' => 'flarum/auth-github:0.1.0-beta.9'
]
]
])
);
$this->assertEquals(409, $response->getStatusCode());
$this->assertEquals('extension_incompatible_with_instance', $this->guessedCause($response));
}
} }