2026-10-05 18:10:18 -04:00
<? php
declare ( strict_types = 1 );
namespace Pterodactyl\Actions\Extensions ;
use Illuminate\Support\Facades\DB ;
use Illuminate\Support\Facades\Event ;
use Pterodactyl\Contracts\Extensions\RemovesExtensions ;
use Pterodactyl\Events\Extensions\ExtensionRemoved ;
use Pterodactyl\Events\Extensions\ExtensionRemoving ;
use Pterodactyl\Exceptions\Extensions\InvalidExtensionException ;
use Pterodactyl\Models\Extension ;
2026-10-06 14:01:38 -04:00
use Pterodactyl\Models\ExtensionSetting ;
use Pterodactyl\Models\Subuser ;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Services\Extensions\ExtensionAssetPublisher ;
use Pterodactyl\Services\Extensions\ExtensionCompatibility ;
2026-10-09 14:15:42 -04:00
use Pterodactyl\Services\Extensions\ExtensionDirectories ;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Services\Extensions\ExtensionLock ;
use Pterodactyl\Services\Extensions\ExtensionManifest ;
2026-10-06 14:01:38 -04:00
use Pterodactyl\Services\Extensions\ExtensionPermissionRegistry ;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Services\Extensions\ExtensionRepository ;
use Pterodactyl\Services\Extensions\ExtensionRuntimeRefresher ;
use Pterodactyl\Services\Extensions\ExtensionSettingFiles ;
use Pterodactyl\Services\FilesystemChanges ;
2026-10-12 07:47:42 -04:00
use Pterodactyl\Services\Themes\ThemeRepository ;
2026-10-05 18:10:18 -04:00
final readonly class RemoveExtension implements RemovesExtensions
{
public function __construct (
private ExtensionRepository $extensions ,
private ExtensionAssetPublisher $assets ,
private ExtensionCompatibility $compatibility ,
private ExtensionLock $lock ,
private ExtensionRuntimeRefresher $runtime ,
private FilesystemChanges $files ,
private ExtensionSettingFiles $settingFiles ,
2026-10-09 14:15:42 -04:00
private ExtensionDirectories $directories ,
2026-10-12 07:47:42 -04:00
private ThemeRepository $themes ,
2026-10-05 18:10:18 -04:00
) {}
public function remove ( string $identifier ) : void
{
throw_unless ( preg_match ( ExtensionManifest :: ID_REGEX , $identifier ), InvalidExtensionException :: class , "Extension id \" { $identifier } \" must match " . ExtensionManifest :: ID_REGEX . '.' );
2026-10-09 14:15:42 -04:00
$this -> directories -> assertWritable ();
2026-10-05 18:10:18 -04:00
$this -> lock -> run ( $identifier , fn () => $this -> removePackage ( $identifier ));
}
private function removePackage ( string $identifier ) : void
{
$manifest = $this -> extensions -> discovered () -> get ( $identifier );
throw_if ( $manifest === null , InvalidExtensionException :: class , "Extension \" { $identifier } \" is not installed." );
2026-10-12 07:47:42 -04:00
$this -> themes -> assertNotActive ( $identifier );
2026-10-05 18:10:18 -04:00
$this -> compatibility -> assertCanDisable ( $identifier , $this -> extensions -> enabled ());
Event :: dispatch ( new ExtensionRemoving ( $manifest ));
$this -> files -> run ([
$this -> extensions -> directory () . DIRECTORY_SEPARATOR . $identifier => null ,
$this -> assets -> publishedPath ( $identifier ) => null ,
2026-10-06 14:01:38 -04:00
], function () use ( $identifier ) : void {
DB :: transaction ( function () use ( $identifier ) : void {
2026-10-05 18:10:18 -04:00
Extension :: query () -> where ( 'identifier' , $identifier ) -> delete ();
2026-10-06 14:01:38 -04:00
$this -> purgeState ( $identifier );
2026-10-05 18:10:18 -04:00
});
});
$this -> extensions -> flushDiscovery ();
DB :: afterCommit ( function () use ( $identifier ) : void {
rescue ( fn () => $this -> settingFiles -> purge ( $identifier ));
rescue ( fn () => $this -> runtime -> refresh ());
rescue ( fn () => Event :: dispatch ( new ExtensionRemoved ( $identifier )));
});
}
2026-10-06 14:01:38 -04:00
/**
* Forget what the extension stored outside its own tables: settings (secrets included) in
* every scope, and the `ext.<id>.*` permissions subusers were granted. Otherwise the next
* package installed with this id would inherit them.
*/
private function purgeState ( string $identifier ) : void
{
ExtensionSetting :: query () -> where ( 'extension' , $identifier ) -> delete ();
$prefix = ExtensionPermissionRegistry :: group ( $identifier ) . '.' ;
Subuser :: query () -> where ( 'permissions' , 'like' , '%"' . $prefix . '%' ) -> lazyById () -> each ( function ( Subuser $subuser ) use ( $prefix ) : void {
$permissions = array_values ( array_filter ( $subuser -> permissions , fn ( string $permission ) : bool => ! str_starts_with ( $permission , $prefix )));
if ( $permissions !== $subuser -> permissions ) {
$subuser -> update ([ 'permissions' => $permissions ]);
}
});
}
2026-10-05 18:10:18 -04:00
}