Files
pterodactyl-panel/app/Console/Commands/Extensions/InstallCommand.php
T

80 lines
3.5 KiB
PHP
Raw Normal View History

2026-10-05 18:10:18 -04:00
<?php
declare(strict_types=1);
namespace Pterodactyl\Console\Commands\Extensions;
use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Command;
2026-10-06 23:31:44 -05:00
use Illuminate\Support\Facades\File;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Contracts\Extensions\InstallsExtensions;
2026-10-06 14:01:38 -04:00
use Pterodactyl\Exceptions\Extensions\ExtensionAlreadyInstalledException;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Exceptions\Extensions\InvalidExtensionException;
2026-10-06 14:01:38 -04:00
use Pterodactyl\Services\Extensions\ExtensionManifest;
2026-10-06 23:31:44 -05:00
use Pterodactyl\Services\Extensions\ExtensionUrlDownloader;
2026-10-05 18:10:18 -04:00
#[Description('Install an extension package into the extensions directory.')]
#[Signature('p:extension:install
2026-10-06 23:31:44 -05:00
{path : Path to a .pteroext/.zip archive or an unpacked extension directory, or a signed install URL.}
2026-10-06 14:01:38 -04:00
{--enable : Enable the extension (and run its migrations) after installing.}
{--replace : Replace an installed extension with the same id without asking.}')]
2026-10-05 18:10:18 -04:00
class InstallCommand extends Command
{
2026-10-06 23:31:44 -05:00
public function handle(InstallsExtensions $installer, ExtensionUrlDownloader $urls): int
2026-10-05 18:10:18 -04:00
{
2026-10-06 23:31:44 -05:00
$download = null;
2026-10-05 18:10:18 -04:00
try {
2026-10-06 23:31:44 -05:00
$path = $this->argument('path');
// A URL is only ever a signed install URL: its signature is verified before
// anything is downloaded, so the panel never installs from an arbitrary address.
if ($urls->isUrl($path)) {
$this->components->info('Verifying the install URL and downloading the extension.');
$path = $urls->download($path);
$download = $path;
2026-10-06 23:31:44 -05:00
}
$manifest = $this->install($installer, $path);
2026-10-05 18:10:18 -04:00
} catch (InvalidExtensionException $invalidExtensionException) {
$this->components->error($invalidExtensionException->getMessage());
return self::FAILURE;
2026-10-06 23:31:44 -05:00
} finally {
if ($download !== null) {
rescue(fn () => File::delete($download));
}
2026-10-05 18:10:18 -04:00
}
$this->components->info(sprintf(
'Installed %s (%s) v%s%s.',
$manifest->name,
$manifest->id,
$manifest->version,
$this->option('enable') ? ' - enabled' : ' - enable it with p:extension:enable '.$manifest->id,
));
return self::SUCCESS;
}
2026-10-06 14:01:38 -04:00
/** Replacing an installed extension needs --replace, or a yes when run interactively. */
2026-10-06 23:31:44 -05:00
private function install(InstallsExtensions $installer, string $path): ExtensionManifest
2026-10-06 14:01:38 -04:00
{
$enable = (bool) $this->option('enable');
try {
return $installer->install($path, $enable, (bool) $this->option('replace'));
} catch (ExtensionAlreadyInstalledException $extensionAlreadyInstalledException) {
$installed = $extensionAlreadyInstalledException->installedVersion === null ? 'the installed copy' : 'v'.$extensionAlreadyInstalledException->installedVersion;
$kept = $extensionAlreadyInstalledException->enabled ? ' It stays enabled and its migrations run.' : '';
throw_unless(
$this->input->isInteractive() && $this->components->confirm("Extension \"{$extensionAlreadyInstalledException->identifier}\" is already installed. Replace {$installed} with v{$extensionAlreadyInstalledException->version}?{$kept}"),
InvalidExtensionException::class,
$extensionAlreadyInstalledException->getMessage().' Pass --replace to replace it.',
);
return $installer->install($path, $enable, replace: true);
}
}
2026-10-05 18:10:18 -04:00
}